Question

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

Was it helpful?

Solution

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]

OTHER TIPS

In Haskell, the following expressions are all equivalent:

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

This means that f is an unary function, g is a binary function, 2 parameters are passed to g, whose result is passed to f. Read more about “owl” operator to understand how it works.

So if you want to convert a similar expression to Elm, you would one of the following:

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

For example:

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

Note: in Elm 0.13 (.) is replaced by (<<), while (>>) is now the same as flip (<<)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top