Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter list items by length in Haskell

Tags:

haskell

I have a list like ["a","ab","abc", "abcd"]

How to get a list that only has the items which have a length > 2.

Means the result is ["abc","abcd"].

like image 856
Xie Avatar asked Jan 28 '14 15:01

Xie


2 Answers

Natalie's answer is perfectly correct, but as an alternate form you could also write it as

filter ((> 2) . length) ["a", "ab", "abc", "abcd"]

Or with list comprehension as

[str | str <- ["a", "ab", "abc", "abcd"], length str > 2]

All three are equivalent

like image 181
bheklilr Avatar answered Sep 28 '22 01:09

bheklilr


filter (\x -> length x > 2) ["a","ab","abc", "abcd"]
like image 37
Natalie Chouinard Avatar answered Sep 28 '22 00:09

Natalie Chouinard