POV-Ray : Newsgroups : povray.off-topic : An explanation of what next_permutation does : Re: An explanation of what next_permutation does Server Time
4 Sep 2024 17:18:44 EDT (-0400)
  Re: An explanation of what next_permutation does  
From: Invisible
Date: 3 Dec 2009 09:08:38
Message: <4b17c666$1@news.povray.org>
Darren New wrote:

> Interesting. :-)

"while (!(*i < *--j)) {}"

Um, yeah, "interesting" would be the term I'd use. WTF? o_O



Personally, I'd do it this way:

nextP :: Ord x => [x] -> Maybe [x]
nextP []  = Nothing
nextP [x] = Nothing
nextP xs  =
   case split (reverse xs) of
     ([] , _  ) -> Nothing
     (ss', ps') ->
       let
         p0         = head ps'
         (ss0, ss1) = span (p0 >=) ss'
         s0         = head ss1
         ps         = reverse (s0 : tail ps')
         ss         = ss0 ++ [p0] ++ tail ss1
       in Just (ps ++ ss)

split :: Ord x => [x] -> ([x], [x])
split [] = ([], [])
split (x:xs) = work x xs
   where
     work x0 [] = ([x0], [])
     work x0 (x:xs)
       | x >= x0   = let (ys,zs) = work x xs in (x0:ys, zs)
       | otherwise = ([x0], x:xs)


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.