5

我正在尝试移植以下 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]

如果有可能在 Elm 中使这个工作点免费,有什么想法吗?:)

多比

4

2 回答 2

4

您可以尝试摆脱<|并使用前缀表示法中的组合函数。它将首先创建一个带有参数的函数,并让函数 compose 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]
于 2013-10-31T07:57:56.137 回答
3

在 Haskell 中,以下表达式都是等价的:

(f .) . g
(.) f . g
(.) ((.) f) g
((.).(.)) f g
(\x y -> f (g x y))

这意味着它f是一个一元函数,g是一个二元函数,2个参数被传递给g,其结果被传递给f。阅读有关“owl”运算符的更多信息以了解其工作原理。

因此,如果您想将类似的表达式转换为 Elm,您可以执行以下操作之一:

(.:) = (<<)<<(<<) -- owl operator definition
(<<) f << g
(<<) ((<<) f) g
f .: g

例如:

((<<) negate << (+)) 3 4 -- returns -7
(negate .: (+)) 3 4      -- returns -7

注意:在Elm 0.13 (.)中被替换为(<<), 而(>>)现在与flip (<<)

于 2015-07-09T17:03:27.110 回答