带有多个函数参数的点自由表示法(point free notation with multiple function parameters)

编程入门 行业动态 更新时间:2024-10-11 13:25:48
带有多个函数参数的点自由表示法(point free notation with multiple function parameters)

我试图移植以下Haskell代码( http://codepad.org/MMydRCxo )

foo :: Int -> Int -> Int -> Maybe Bool foo a b c = if a == 1 then Just True else Nothing bar :: Int -> Int -> Bool bar b c = maybe False id $ foo 1 b c -- point free bar' :: Int -> Int -> Bool bar' = ((maybe False id $) .) . foo 1 main = do print $ bar 2 3 print $ bar' 2 3

到榆树,但还没有运气。 ( http://share-elm.com/sprout/5271f160e4b03cf6e675bc97 )

foo : Int -> Int -> Int -> Maybe Bool foo a b c = if a == 1 then Just True else Nothing bar : Int -> Int -> Bool bar b c = maybe False id <| foo 1 b c -- point free bar' : Int -> Int -> Bool bar' = ((maybe False id <|) .) . foo 1 main = flow down [ asText <| bar 2 3 , asText <| bar' 2 3]

任何想法,如果有可能使这项工作在榆树免费? :)

DOBI

I am trying to port the following Haskell code (http://codepad.org/MMydRCxo)

foo :: Int -> Int -> Int -> Maybe Bool foo a b c = if a == 1 then Just True else Nothing bar :: Int -> Int -> Bool bar b c = maybe False id $ foo 1 b c -- point free bar' :: Int -> Int -> Bool bar' = ((maybe False id $) .) . foo 1 main = do print $ bar 2 3 print $ bar' 2 3

to Elm, but had no luck yet. (http://share-elm.com/sprout/5271f160e4b03cf6e675bc97)

foo : Int -> Int -> Int -> Maybe Bool foo a b c = if a == 1 then Just True else Nothing bar : Int -> Int -> Bool bar b c = maybe False id <| foo 1 b c -- point free bar' : Int -> Int -> Bool bar' = ((maybe False id <|) .) . foo 1 main = flow down [ asText <| bar 2 3 , asText <| bar' 2 3]

Any Ideas if there is a possibility to make this work point free in Elm? :)

Dobi

最满意答案

你可以尝试摆脱<| 而是使用前缀表示法中的组合函数。 它将首先创建一个带参数的函数,并使函数组成foo 1 。

这样,调用bar' 2将返回采用最后一个参数的组合函数。 即( http://share-elm.com/sprout/52720bc5e4b03cf6e675bcc8 ):

foo : Int -> Int -> Int -> Maybe Bool foo a b c = if a == 1 then Just True else Nothing bar : Int -> Int -> Bool bar b c = maybe False id <| foo 1 b c bar' : Int -> Int -> Bool bar' = (.) (maybe False id) . foo 1 -- the explicit evaluation precedence being: ((.) (maybe False id)) . (foo 1) main = flow down [ asText <| bar 2 3 , asText <| bar' 2 3]

You can try getting rid of the <| and using the composition function in prefix notation instead. It'll first create a function that takes an argument, and have that function compose foo 1.

That way, calling bar' 2 will return the composed function that takes the last argument. I.e. (http://share-elm.com/sprout/52720bc5e4b03cf6e675bcc8):

foo : Int -> Int -> Int -> Maybe Bool foo a b c = if a == 1 then Just True else Nothing bar : Int -> Int -> Bool bar b c = maybe False id <| foo 1 b c bar' : Int -> Int -> Bool bar' = (.) (maybe False id) . foo 1 -- the explicit evaluation precedence being: ((.) (maybe False id)) . (foo 1) main = flow down [ asText <| bar 2 3 , asText <| bar' 2 3]

更多推荐

本文发布于:2023-07-14 19:03:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1106728.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   函数   参数   自由   point

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!