[Haskell-cafe] Re: Haskell and C++ program

Apfelmus, Heinrich apfelmus at quantentunnel.de
Thu Jan 15 07:17:23 EST 2009


Jonathan Cast wrote:
>   reverseDouble =
>         unlines
>       . intro
>       . map show
>       . reverse
>       . map (read :: String -> Double)
>       . takeWhile (/= "end")
>       . words
>     where
>       intro l =
>         ("read " ++ show (length l) ++ " elements") :
>         "elements in reversed order" :
>         l

This can be simplified to

  ... . map show . reverse . map read . ...

 = { map f . reverse = reverse . map f }

  ... . reverse . map show . map read . ...

 = { map f . map g = map (f . g) }

  ... . reverse . map (show . read) . ...

 = { show . read = id }

  ... . reverse . map id . ...

 = { map id = id }

  ... . reverse . ...

In other words,

  reverseDouble =
    unlines. intro . reverse . takeWhile (/= "end") . words
    where
      intro xs =
        ("read " ++ show (length xs) ++ " elements") :
        "elements in reversed order" :
        xs

And the doubles disappeared completely. :)


Regards,
H. Apfelmus



More information about the Haskell-Cafe mailing list