I have a list of integers.
Also, I have a function which returns True when the element is non-zero, and returns False when the element is zero (let’s call it FUNCTION1).
So, how to define another function (using FUNCTION1), which will give us a new list and only include the non-empty elements?
For example, we have [1,2,3,0,6,0], and we want [1,2,3,6]
1
What you’re looking for is the filter
function.
To filter out 0 from a list, you would say:
filter (x -> not (x==0)) [1,2,3,0,6,0]
-- or better yet
filter (not . (==0)) [1,2,3,0,6,0]
-- or best of all
filter (/=0) [1,2,3,0,6,0]
If you want, you can substitute the anonymous function with your named function. It should work the same:
filter Function1 [1,2,3,0,6,0]
2