Haskell第1回 関数型プログラミングの世界へようこそ

  • 処理の繰り返しのHaskell的表記とその使用
let repeated f n = \x -> (iterate f x)!!n
repeated (+2) 3 5
repeated (+3) 3 5
3+2+2+2+2+2
5+2+2+2
5+3+3+3
let repeated f n = \x -> (iterate f x)!!n
    • let repeated : "repeated"という名前の関数を作る
    • f,n x の3引数を取る関数で
    • fは関数(xを受け取って、何かを返す)、xは値
    • fという関数にxを渡して、何かを返させ、返ったものをfに渡して、という繰り返しをする関数である
    • "repeated"関数では、繰り返し回数をn回とした結果を返せ
repeated (+2) 3 5
    • (+2):(値を受け取って)2を加えて、その結果を返すという関数
    • 3: 繰返し回数
    • 5: 初期値
Prelude> let repeated f n = \x -> (iterate f x)!!n
Prelude> repeated (+2) 3 5
11
Prelude> repeated (+3) 3 5
14
Prelude> 3+2+2+2+2+2
13
Prelude> 5+2+2+2
11
Prelude> 5+3+3+3
14
Prelude> 

  • ファイル"hk20120215.hs"という名前のファイルに
repeated2 f n = \x -> (iterate f x)!!n 
  • と書いておいて、それを読み込んで(WinGHCiならツールバー→File→load)やると
Prelude> :load "hk20120215.hs"
[1 of 1] Compiling Main             ( hk20120215.hs, interpreted )
Ok, modules loaded: Main.
*Main> repeated2 (+2) 3 5
11
*Main> 
  • というように、読み込んだ関数"repeated2"が同じ処理をしてくれる
  • Haskellはいわゆる数式をコード化するのが得意なので
    • S_n = a + a x + a x^2 + ... + a x^n=\sum_{i=0}^n a x^i = a\frac{1-x^{n+1}}{1-x}をやってみよう
-- mygp :: Int -> Int -> Int -> Int
mygp m n x | n > 0  = mygp m (n-1) x + m * x^n
           | n == 0 = m
           | otherwise = undefined
*Main> :load "mygp.hs"
[1 of 1] Compiling Main             ( mygp.hs, interpreted )
Ok, modules loaded: Main.
*Main> :type mygp
mygp :: (Num a, Integral a1) => a -> a1 -> a -> a
*Main> mygp 2 30 3
617673396283946
*Main> :type mygp
mygp :: (Num a, Integral a1) => a -> a1 -> a -> a
*Main> 2*(1-3^(30+1))/(1-3)
6.17673396283946e14