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

Eugene Kirpichov ekirpichov at gmail.com
Thu Jan 15 07:27:59 EST 2009


Well, your program is not equivalent to the C++ version, since it
doesn't bail on incorrect input.

2009/1/15 Apfelmus, Heinrich <apfelmus at quantentunnel.de>:
> 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
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>


More information about the Haskell-Cafe mailing list