[Haskell-cafe] warning - Euler problem spoiler enclosed

Ivan Lazar Miljenovic ivan.miljenovic at gmail.com
Wed May 4 15:21:33 CEST 2011


On 4 May 2011 13:13, Barbara Shirtcliff <barcs at gmx.com> wrote:
> Hi,
>
> In the following solution to problem 24, why is nub ignored?
> I.e. if you do lexOrder of "0012," you get twice as many permutations as with "012," even though I have used nub.
>
> [snip]
>
> lexOrder :: [Char] -> [[Char]]
> lexOrder s
>  | length s == 1    = [s]
>  | length s == 2    = z : [reverse z]
>  | otherwise        = concat $ map (\n -> h n) [0..((length s) - 1)]
>                    where z = sort $ nub s -- why is the nub ignored here?
>                          h :: Int -> [String]
>                          h n = map (z!!n :) $ lexOrder $ filter (\c -> lexI c z /= n) z

As a guess, I think it's from the usage of length on the right-hand size.

Also, note that "lexOrder s@[_] = [s]" is nicer than "lexOrder s |
length s == 1 = [s]".

-- 
Ivan Lazar Miljenovic
Ivan.Miljenovic at gmail.com
IvanMiljenovic.wordpress.com



More information about the Haskell-Cafe mailing list