Created
February 10, 2012 06:21
-
-
Save totem3/1787157 to your computer and use it in GitHub Desktop.
プログラミングHaskell第6章練習問題 むむむむむ
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
--1 | |
power :: Int -> Int -> Int | |
power m n | n < 0 = 0 | |
| n == 0 = 1 | |
| otherwise = m * power m (n-1) | |
{- | |
power 2 3 | |
2 * ( power 2 2 ) | |
2 * 2 * ( power 2 1) | |
2 * 2 * 2 * ( power 2 0) | |
2 * 2 * 2 * 1 | |
8 | |
-} | |
--2 | |
{- 簡約しなイカ? | |
length [1,2,3] | |
1 + ( length [2,3] ) | |
1 + 1 + ( length [3] ) | |
1 + 1 + 1 + (length []) | |
1 + 1 + 1 + 0 | |
3 | |
drop 3 [1,2,3,4,5] | |
drop 2 [2,3,4,5] | |
drop 1 [3,4,5] | |
drop 0 [4,5] | |
[4,5] | |
init [1,2,3] | |
1 : ( init [2,3] ) | |
1 : 2 : ( init [3] ) | |
1 : 2 : [] | |
[1,2] | |
-} | |
--3 | |
myAnd :: [Bool] -> Bool | |
myAnd b | b == [True] = True | |
| b == [False] = False | |
myAnd (b:bs) | b == True = and bs | |
| b == False = False | |
myConcat :: [[a]] -> [a] | |
myConcat [[]] = [] | |
myConcat ([]:xss) = myConcat xss | |
myConcat ((x:xs):xss) = x : myConcat (xs : xss) | |
myReplicate :: Int -> a -> [a] | |
myReplicate 0 x = [] | |
myReplicate n x = x : myReplicate (n-1) x | |
doubleExclamation :: [a] -> Int -> a | |
doubleExclamation (x:[]) n = x | |
doubleExclamation (x:xs) 0 = x | |
doubleExclamation (x:xs) n = doubleExclamation xs (n-1) | |
myElem :: Eq a => a -> [a] -> Bool | |
myElem x [] = False | |
--4 | |
merge :: Ord a => [a] -> [a] -> [a] | |
merge xs [] = xs | |
merge [] ys = ys | |
merge (x:xs) (y:ys) = if x<=y then x : (merge xs (y:ys)) else y : (merge (x:xs) ys) | |
--5 | |
msort :: Ord a => [a] -> [a] | |
msort (x:[]) = (x:[]) | |
msort xs = merge (msort (fst pair)) (msort(snd pair)) | |
where pair = halve xs | |
halve :: [a] -> ([a],[a]) | |
halve xs = (take len xs, drop len xs) | |
where len = (length xs) `div` 2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment