On Wednesday 04 May 2011 15:13:07, Barbara Shirtcliff wrote:
> Hi,
> 
> In the following solution to problem 24, why is nub ignored?

It isn't:

*LexOrder> lexOrder "00"
["0","0"]
*LexOrder> lexOrder "001"
["01","10","*** Exception: Prelude.(!!): index too large


> 
> lexI :: Char -> String -> Int
> lexI c s = maybe 1 (id) $ elemIndex c s
> 
> 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

Your problem is (well, the one I see immediately) that you check for the 
length of s, where you should check for the length of z.

_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

Reply via email to