3 ms·
Haskell has Hoogle[1], which allows you to search for functions using a type signature. This is surprisingly effective. Let's say you want that `contains` fun
by zingermc 11y ago
Haskell has Hoogle[1], which allows you to search for functions using a type signature. This is surprisingly effective.
Let's say you want that `contains` function from the original post. You'd search for `(Eq a) => a -> [a] -> Bool`, which describes a function that takes two parameters and returns a boolean. The first result[2] is the `elem` function, which is exactly what we wanted!
This is a bit of a contrived example, but I have honestly been surprised by how effective searching by type signature is in Haskell. I wonder if it is possible for a language with a weaker type system, like JavaScript.
[1] https://www.haskell.org/hoogle/ https://www.haskell.org/hoogle/
[2] https://www.haskell.org/hoogle/?hoogle=%28Eq+a%29+%3D%3E+a+-%3E+%5Ba%5D+-%3E+Bool https://www.haskell.org/hoogle/?hoogle=%28Eq+a%29+%3D%3E+a+-...
- Fargren 11y agoDoes the order of the parameters matter? It's been a while since I've touched Haskell, so maybe it's obvious that this isn't [a] -> a -> Bool for some idiomatic reason. I guess that would be `isContained` instead of `contains`, so it probably wouldn't be the first thing I search for, but there's at least some potential for ambiguity.
- zingermc 11y agoOrder doesn't matter! And it still returns similar results without the `Eq` typeclass constraint.
- cormacrelf 11y agoIt's almost a convention in Haskell. The idea is, since currying functions is easy and common, you stick the argument that you'd want the least in a curried version last. So if you wanted to find out if x was in ten lists, you'd just: map (elem x) [list1, list2, ..., list10] or, for folds, the function is the one least likely to change, so you put it first, and the list is most likely to change, so you put it last: foldl (+) 0 [1,2,3] map (foldl (-) 100) [[1,2],[2,3],[4,19]] Of course, it's always debatable if you can find some situation where you wanted to curry in a different order, but generally you pick in order to reduce forced named lambda parameters. As far as I can tell, that's how it's done. Edit: if you saw the sneaky edit, you'll know that this particular convention isn't easy to follow!
- eru 11y agoThere are a bunch of rules of thumbs that help you decide faster. Like `needle before haystack'.
- thedufer 11y agoAs you can see here[1], changing the argument order does not prevent `elem` from showing up in the first position. If there are multiple close matches, ordering of arguments can change which is first, but the one you're looking for is almost always in the first few. [1] https://www.haskell.org/hoogle/?hoogle=%28Eq+a%29+%3D%3E+%5Ba%5D+-%3E+a+-%3E+Bool https://www.haskell.org/hoogle/?hoogle=%28Eq+a%29+%3D%3E+%5B...
- girvo 11y agoPurescript's equivalent is Pursuit: https://pursuit.purescript.org/search?q=a+-%3E+f+a+-%3E+Boolean https://pursuit.purescript.org/search?q=a+-%3E+f+a+-%3E+Bool...