use*_*996 -12 haskell functional-programming pointfree tacit-programming
如何以无点样式重写以下函数,x完全从定义中删除参数(另外两个可能保留):
between min max x = (min < x) && (x < max)
Run Code Online (Sandbox Code Playgroud)
这不是一项任务,只是一个问题.我不知道该怎么办.我可以把它变成一个lambda函数
between min max = \x -> (min < x) && (x < max)
Run Code Online (Sandbox Code Playgroud)
但这不是没有点的,因为x它仍然存在.请帮忙.
可以使用以下Reader应用程序完成:
between min max = \x. (min < x) && (x < max)
{ Convert infix comparisons to sections }
= \x. ((min <) x) && ((< max) x)
{ Move infix (&&) to applicative style }
= \x. (&&) ((min <) x) ((< max) x)
{ Lift to applicative style using the applicative instance of `(->) a` }
= \x. (pure (&&) <*> (min <) <*> (< max)) x
{ Eta-reduce }
= pure (&&) <*> (min <) <*> (< max)
{ Optionally simplify for idiomatic style }
= (&&) <$> (min <) <*> (< max)
Run Code Online (Sandbox Code Playgroud)
另一种解决方案(需要导入Control.Applicative):
between min max = liftA2 (&&) (min <) (max >)
Run Code Online (Sandbox Code Playgroud)