
16 Apr
2012
16 Apr
'12
4:52 a.m.
Hi all. If i implement take using foldr take' :: Int -> [a] -> [a] take' n = foldr (\x z -> if fst x <= n then snd x : z else []) [] . zip [1..] it'll work fine with infinite lists. But if i implement splitAt similarly splitAt' :: Int -> [a] -> ([a], [a]) splitAt' n = foldr (\x (z1, z2) -> if fst x <= n then (snd x : z1, z2) else ([], snd x : z2)) ([], []) . zip [1..] and call it like this *Main> fst $ splitAt' 4 [1..] ^CInterrupted. it will hang. I don't understand, why it tries to compute second list (z2), if it only needs first?