[Haskell-cafe] warning - Euler problem spoiler enclosed

Chris Smith cdsmith at gmail.com
Wed May 4 15:18:40 CEST 2011


On Wed, 2011-05-04 at 07:13 -0600, Barbara Shirtcliff wrote:
> 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.

> 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

You are using (length s) in the otherwise case.  If you want the results
to be identical with duplicates, perhaps you meant to say (length z)?

-- 
Chris




More information about the Haskell-Cafe mailing list