Skip to content

Instantly share code, notes, and snippets.

@ruthenium
Created September 27, 2012 15:34
Show Gist options
  • Save ruthenium/3794658 to your computer and use it in GitHub Desktop.
Save ruthenium/3794658 to your computer and use it in GitHub Desktop.
Little uncurry example.
module Main where
------------------------------------------------------------------------------
{- |
First, define some example functions.
-}
------------------------------------------------------------------------------
foo2 :: Int -> Int -> Int
foo2 a b = a + b
------------------------------------------------------------------------------
foo3 :: Int -> Int -> Int -> Int
foo3 a b c = a + b + c
------------------------------------------------------------------------------
foo4 :: Int -> Int -> Int -> Int -> Int
foo4 a b c d = a + b + c + d
------------------------------------------------------------------------------
------------------------------------------------------------------------------
{- |
Then, utilities.
-}
------------------------------------------------------------------------------
args :: [a] -> (a, a)
args (x:xs) = (x, xs !! 0)
------------------------------------------------------------------------------
{-| 3 arguments: -}
uncurry3 :: (a -> b -> c -> d) -> (a, b, c) -> d
uncurry3 f (x,y,z) = f x y z
------------------------------------------------------------------------------
args3 :: [a] -> (a, a, a)
args3 (x:y:xs) = (x, y, xs !! 0)
------------------------------------------------------------------------------
------------------------------------------------------------------------------
{-| 4 arguments: -}
uncurry4 :: (a -> b -> c -> d -> e) -> (a, b, c, d) -> e
uncurry4 f (a,b,c,d) = f a b c d
------------------------------------------------------------------------------
args4 :: [a] -> (a, a, a, a)
args4 (x:y:z:xs) = (x, y, z, xs !! 0)
------------------------------------------------------------------------------
------------------------------------------------------------------------------
{- |
Finally, demonstrate!
-}
main = do
print $ uncurry foo2 (args [1,2])
print $ uncurry3 foo3 (args3 [1, 2, 3])
print $ uncurry4 foo4 (args4 [1, 2, 3, 4])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment