Reputation: 37
In Haskell, I've defined a filter function that takes two arguments and returns a Bool, ie
myFilter :: a -> b -> Bool
I've defined a function that takes one argument and returns a list, ie
myFunction :: a -> [b]
The argument myFunction takes is the same as the first argument that myFilter takes, and the items of the list output by myFunction are the same type as the second argument of myFilter, ie
myFunction a = [b]
myFilter a b = Bool
I want to define another function, myFilteredFunction,
myFilteredFunction :: a -> [b]
This takes one argument a
, the one that both myFilter and myFunction that, and returns a subset of [b]
that includes only items that satisfy the predicate set by myFilter, ie that filters the output of myFunction using myFilter. This seems like such a simple thing to do and yet I've not been able to make it work (I'm new to Haskell)!
Some of the options I've tried, which obviously don't work:
myFilteredFunction = myFilter . myFunction
myFilteredFunction' = filter myFilter . myFunction
myFilteredFunction'' = myFilter . (map myFunction)
I see the problem being that myFilter takes two arguments and the other two functions take just the one...help much appreciated.
Upvotes: 0
Views: 500
Reputation: 18249
Your question isn't 100% clear, but I think you might mean this?
myFilteredFunction :: a -> [b]
myFilteredFunction a = filter (\b -> myFilter a b) (myFunction a)
That is, you apply myFunction
to get a list, and then filter it according to whether myFilter
gives True
when applied to the original element and the one being tested.
Note that the function applied to filter
here can be simplified using partial application:
myFilteredFunction a = filter (myFilter a) (myFunction a)
Upvotes: 4