[Haskell-cafe] A small puzzle: inTwain as function of foldr

Martijn van Steenbergen martijn at van.steenbergen.nl
Fri Jun 5 07:21:22 EDT 2009


Thomas ten Cate wrote:
> Possible, yes.
> 
> Efficient, not really.
> 
>> inTwain = foldr (\x (ls, rs) -> if length ls == length rs then (x:ls, rs) else (x:(init ls), (last ls):rs)) ([], [])

But this uses length and init and last all of which are recursive 
functions. I consider that cheating: only foldr may do the recursion.

Martijn.



More information about the Haskell-Cafe mailing list