Extract values via pattern matching in Elm/Haskell

Haskell


Suppose you have a simple data definition for a direction and want to add a delta of type Int to it:

data Direction = Direction Int deriving Show
let d = Direction 7
 

Obviously, this doesn't work:

d + 5

because the types are not compatible. The solution is pattern matching:

let 
  (Direction value) = d
in
  d + 5


Elm


type Direction = Direction Int

update :: Direction -> Int -> Direction
update dir delta =
  let 
    (Direction value) = dir
  in
    Direction (value + delta)


289 Words

2015-12-06T03:14:00.003-08:00