Renaud K.D. Tripathi
Renaud K.D. Tripathi

Reputation: 21

Why does the bind operator used with an incomplete lambda function work?

From Graham Hutton's 'programming haskell':

data Expr = Val Int | Div Expr Expr

safediv :: Int -> Int -> Maybe Int
safediv _ 0 = Nothing
safediv x y = Just (n `div` m)

{- |
(>>=) :: Maybe a -> (a -> Maybe b) -> Maybe b
mx >>= f = case mx of
             Nothing -> Nothing
             Just x -> f x
-}

eval :: Expr -> Maybe Int
eval (Val n)   = Just n
eval (Div x y) = eval x >>= \n ->
                 eval y >>= \m ->
                 safediv n m

I understand that currying is involved in the 'eval' function declaration. I have tried to reproduce this type in declaration in ghci with no success. For example

map (\x ->) [1..3]

What are then the usage boundaries of the incomplete lambda form declaration?

Upvotes: 1

Views: 103

Answers (1)

mschmidt
mschmidt

Reputation: 2800

As mentioned by @RobinZigmond, there is nothing incomplete. You are probably confused by the line breaks. You can write your eval function alternatively as:

eval (Div x y) = eval x >>= \n -> eval y >>= \m -> safediv n m

..and if you add parentheses it should be visible, that you simply have nested lambdas:

eval (Div x y) = (eval x) >>= (\n -> (eval y) >>= (\m -> safediv n m))

Upvotes: 3

Related Questions