Working in F# has lead me to learn about Haskell. I'm currently on chapter 7 of this tutorial which I HIGHLY recommend. Quick question though. Maybe I'm getting ahead of myself and I'll find the answer in future chapters but, is there a way to reverse the position of the function and it's arguments if the function only takes one argument like in F#. So, for example, in F# if you had a function called digitToInt (as you do in Haskell), you could do the following:
3 |> digitToInt
I know about using back ticks, but that's sp开发者_StackOverflow社区ecifically for binary functions. Is there anything similar for unary functions?
One way to do that could be defining an infix function (|>)
that take a value and a function and calls the function passing it the value like this:
(|>) :: a -> (a -> b) -> b
(|>) x f = f x
Then you can use it exactly like in your example:
3 |> digitToInt
You can do what peoro suggests and it works fine. However, I'd advise against it. The normal way to "read" Haskell is like a sentence -- left to right, with verbs at the beginning and the subject at the end. Typically you get a chained composition like f . g. q . r $ x
. With monadic operators you get f =<< g =<< x
(or (f <=< g) =<< x
) and with applicatives you get f <$> x <*> y <*> z
. So reversing the normal order of things is not necessarily idiomatic.
By the way, the reason, so I've heard, that F# chose the opposite operator is that it works very very nicely with visualstudio autocomplete -- if you've typed in the value you're operating on, then its type can determine choices for what can operate on it. That sort of tooling support is awesome, and it would be a good argument for adopting an alternate style.
I don't think that Haskell has a standard operator for this (I may be wrong), but you can certainly define one. A somewhat related concept is the $
operator that allows you to get rid of parentheses when writing a series of applications (which is one reason for using |>
in F#). The difference is that $
doesn't reverse the order:
f $ g $ h x = f (g (h x))
BTW: A fantastic way to find out things like this in Haskell is to use Hoogle. For example:
- http://haskell.org/hoogle/?hoogle=a+-%3E+%28a+-%3E+b%29+-%3E+b
精彩评论