Tuple and functional composition

Is there a better way to express (\(a, b) -> a < b) using function composition? I feel like I'm missing something, and the curry experiment only confused me.

+6
haskell tuples function-composition
Jan 24 2018-12-12T00:
source share
2 answers

curry is the wrong thing to use here; it turns a function acting on tuples into a curry function. You want the opposite, which is uncurry :

 uncurry :: (a -> b -> c) -> (a, b) -> c 

In this case, it is uncurry (<) .

(Another useful source for combinators, useful when writing functions on tuples, is Control.Arrow , since (->) is an instance of Arrow , you can read abc as b -> c .)

+12
Jan 24 2018-12-12T00:
source share

Looking at types is the best way in Haskell to get the first idea what any function does:

 curry :: ((a, b) -> c) -> a -> b -> c uncurry :: (a -> b -> c) -> (a, b) -> c 

curry : pair function -> curried function (it curries function).

uncurry : curried function -> pair function.

The currying Haskell Wiki page contains small exercises at the end of the page:

  • Simplify curry id
  • Simplify uncurry const
  • Express snd with curry or uncurry and other core Prelude functions and without lambdas
  • Write function \(x,y) -> (y,x) without lambda and only with Prelude functions

Try to solve these exercises right now, they will give you an extensive idea of ​​the system and functionality of an application like Haskell.

There are some interesting uncurry applications, try passing the various arguments to the functions below and see what they do:

 uncurry (.) :: (b -> c, a -> b) -> a -> c uncurry (flip .) :: (b -> a -> b1 -> c, b) -> b1 -> a -> c uncurry (flip (.)) :: (a -> b, b -> c) -> a -> c uncurry ($) :: (b -> c, b) -> c uncurry (flip ($)) :: (a, a -> c) -> c -- uncurry (,) is an identity function for pairs uncurry (,) :: (a, b) -> (a, b) uncurry (,) (1,2) -- returns (1,2) uncurry uncurry :: (a -> b -> c, (a, b)) -> c uncurry uncurry ((+), (2, 3)) -- returns 5 -- curry . uncurry and uncurry . curry are identity functions curry . uncurry :: (a -> b -> c) -> (a -> b -> c) (curry . uncurry) (+) 2 3 -- returns 5 uncurry . curry :: ((a, b) -> c) -> ((a, b) -> c) (uncurry . curry) fst (2,3) -- returns 2 -- pair -> triple uncurry (,,) :: (a, b) -> c -> (a, b, c) uncurry (,,) (1,2) 3 -- returns (1,2,3) 
+1
Feb 19 '15 at 10:50
source share



All Articles