问题
Can an analog of the S combinator be expressed in Haskell using only standard functions (without defining it by equation) and without using lambda (anonymous function)? I expect it to by of type (a -> b -> c) -> (a -> b) -> a -> c
.
For example, an analog of the K combinator is just const
.
In fact i am trying to express the function \f x -> f x x
using standard functions, but cannot think of any standard non-linear function to start with (that is a function that uses its argument more than once).
回答1:
s = (<*>)
for the ((->) r)
Applicative instance.
回答2:
Although it doesn't look like it at first, ap is the S combinator (and join is the combinator you're really after).
回答3:
It can also be used (=<<), (>>=)
.
And they are included in Prelude
instance Monad ((->) r) where
return = const
f >>= k = \ r -> k (f r) r
来源:https://stackoverflow.com/questions/23095224/s-combinator-in-haskell