Created
March 31, 2013 14:56
-
-
Save taka011239/5280883 to your computer and use it in GitHub Desktop.
再帰ドリル
http://d.hatena.ne.jp/kazu-yamamoto/20130330/1364610328
http://d.hatena.ne.jp/kazu-yamamoto/20130331
This file contains 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
plus :: Int -> Int -> Int | |
plus m 0 = m | |
plus m n | |
| n > 0 = plus m (n - 1) + 1 | |
| n < 0 = plus m (n + 1) - 1 | |
minus :: Int -> Int -> Int | |
minus m 0 = m | |
minus m n | |
| n > 0 = minus m (n - 1) - 1 | |
| n < 0 = minus m (n + 1) + 1 | |
power :: Float -> Float -> Float | |
power m 0.0 = 1 | |
power m 1.0 = m | |
power m n | |
| n > 0.0 = power m (n - 1) * m | |
| n < 0.0 = power m (n + 1) / m | |
-- tail call optimization | |
plus' :: Int -> Int -> Int | |
plus' m n = plus'' m n m | |
plus'' :: Int -> Int -> Int -> Int | |
plus'' m 0 acc = acc | |
plus'' m n acc | |
| n > 0 = plus'' m (n - 1) (acc + 1) | |
| n < 0 = plus'' m (n + 1) (acc - 1) | |
minus' :: Int -> Int -> Int | |
minus' m n = minus'' m n m | |
minus'' :: Int -> Int -> Int -> Int | |
minus'' m 0 acc = acc | |
minus'' m n acc | |
| n > 0 = minus'' m (n - 1) (acc - 1) | |
| n < 0 = minus'' m (n + 1) (acc + 1) | |
power' :: Float -> Float -> Float | |
power' m n | |
| n > 0.0 = power'' m n m | |
| n <= 0.0 = power'' m n 1.0 | |
power'' :: Float -> Float -> Float -> Float | |
power'' m 0.0 acc = acc | |
power'' m n acc | |
| n > 0.0 = power'' m (n - 1.0) (acc * m) | |
| n < 0.0 = power'' m (n + 1.0) (acc / m) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment