Created
September 27, 2012 15:34
-
-
Save ruthenium/3794658 to your computer and use it in GitHub Desktop.
Little uncurry example.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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