I'm new to Haskell, and trying to do the following:
takeWhile (length < 3) [[1],[1,2],[1..3],[1..4]]
. But this gives an error which I believe is because takeWhile
would test length < 3 [1]
instead of length [1] < 3
, which is what would work. Do I to make [[1],[1,2],[1..3],[1..4]]
into a list of lengths and then do a takeWhile (< 3)
for that list of lengths? Or is there a way to do it by directly testing lengths?
You can compose length
and (< 3)
to achieve what you want:
takeWhile ((< 3) . length) [[1],[1,2],[1..3],[1..4]]