Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Point-free pattern matching possible in Haskell?

Given:

data TwoInts = TwoInts Int Int 

add'em :: TwoInts -> Int
add'em (TwoInts a b) = a+b

is it possible to write add'em without having to name a and b. Something like:

 add'em TwoInts = (+) -- (Note: Fails to type check)
like image 822
John F. Miller Avatar asked May 06 '11 19:05

John F. Miller


People also ask

Does Haskell have pattern matching?

Overview. We use pattern matching in Haskell to simplify our codes by identifying specific types of expression. We can also use if-else as an alternative to pattern matching. Pattern matching can also be seen as a kind of dynamic polymorphism where, based on the parameter list, different methods can be executed.

How does pattern matching work Haskell?

Pattern matching consists of specifying patterns to which some data should conform and then checking to see if it does and deconstructing the data according to those patterns. When defining functions, you can define separate function bodies for different patterns.


1 Answers

Via analogy to tuples,

data TwoInts = TwoInts { fst', snd' :: Int }

we can define an operation for lifting functions of two arguments onto a TwoInt

uncurry' f p =  f (fst' p) (snd' p)

Giving us the nice notation:

add'em = uncurry' (+)
like image 187
Don Stewart Avatar answered Sep 22 '22 11:09

Don Stewart