haskellhoogle

How to build 'missing' complex functions in Haskell


How should I aprroach situations when I know type of needed function but it is not defined yet? I suspect function I'm looking for is easily composable from others, but I don't know where to start looking.

Prelude> :hoogle (b->b->c)->(a->b)->(a->b)->a->a->c
No results found

To be more specific, I have (b->b->c) function at hand but I need to process (lift maybe?) the arguments first. Can hoogle find functions with arguments in different order than specified?

This is close to what I need, but I want to apply different function to each argument.

Prelude Data.Function> :t on
on :: (b -> b -> c) -> (a -> b) -> a -> a -> c

Solution

  • Seeing as your question asked how this function can be composed from other functions, I would like to point out that you can write it as:

    import Control.Arrow
    on2 :: (b -> b -> c) -> (a -> b) -> (a -> b) -> a -> a -> c
    on2 f g h = curry (uncurry f . (g *** h))
    

    However, the straightforward definition suggested by bheklilr is obviously just as concise and much more readable for people who are not used to working with Arrows.

    And, yes, as far as I know, Hoogle does also search for functions in which the order of argument types you specified is permuted.