Skip to content

Instantly share code, notes, and snippets.

@mitsuji
Created January 14, 2015 08:31
Show Gist options
  • Select an option

  • Save mitsuji/59e242e5536a1c881729 to your computer and use it in GitHub Desktop.

Select an option

Save mitsuji/59e242e5536a1c881729 to your computer and use it in GitHub Desktop.
currying
-- n個の引数を持つ関数が、「1個の引数を持つ、n-1個の引数を持つ関数を返す関数」でもあること
-- haskell ではデフォルトで関数は「関数を返す関数」になっている
-- non curried function ( ordinary in another languages )
foo:: (Int, Int, Int, Int) -> Int
foo (a, j, q, k ) = a + j + q + k
-- curried function ( ordinary in haskell )
bar :: Int -> Int -> Int -> Int -> Int
bar a j q k = a + j + q + k
-- 部分適用
foo' :: (Int, Int, Int) -> Int
foo' = \(j', q', k') -> foo (2, j', q', k')
foo'' :: (Int, Int) -> Int
foo''= \(q', k') -> foo (2, 3, q', k')
foo''' :: (Int, Int) -> Int
foo''' = \(q', k') -> foo' (3, q', k')
-- カリー化
bar' :: Int -> Int -> Int -> Int
bar' = bar 2
bar'' :: Int -> Int -> Int
bar'' = bar 2 3
hf1:: ( Int-> Int -> Int ) -> Int -> Int
hf1 f n = (f n 4) * n
hf2:: ( Int -> Int ) -> Int -> Int
hf2 f n = (f n) * n
main = do
print $ foo ( 2, 3, 4, 5 )
print $ foo' ( 3, 4, 5 )
print $ foo'' ( 4, 5 )
print $ foo''' ( 4, 5 )
print $ (\(j, q, k) -> foo (2, j, q, k)) ( 3, 4, 5 )
print $ (\(q, k) -> foo (2, 3, q, k)) ( 4, 5 )
print $ (\(q, k) -> (\(j', q', k') -> foo (2, j', q', k')) (3, q, k)) ( 4, 5 )
print $ bar 2 3 4 5
print $ bar' 3 4 5
print $ bar'' 4 5
print $ (bar 2) 3 4 5
print $ (bar 2 3) 4 5
print $ hf1 ( bar 2 3 ) 3
print $ hf2 ( bar 2 3 4 ) 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment