S combinator in Haskell

拟墨画扇 提交于 2019-11-29 03:00:04

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!