[View] [Edit] [Attachments] [History] [Home] [Changes] [Search] [Help]
Haskell 自習
-- 演習 2000-10-17 課題3
fill :: Int -> String -> String
fill num str
| length str >= num = str
| True = ' ' : fill (num - 1) str
fact :: Int -> Int
fact 0 = 1
fact n = n * fact(n - 1)
factArray = [ fact n | n <- [0 ..] ]
factTable :: Int -> String
factTable n = concat [factFill 2 20 n | n <- [0 .. n]]
where
factFill :: Int -> Int -> Int -> String
factFill n m x = (fill n (show x)) ++ (fill m (show(fact x))) ++ "\n"
-- 演習 2000-10-30
sumSquares m n
= sqM + sqN
where
sqM = m * m
sqN = n * n
addPairwise :: [Int] -> [Int] -> [Int]
addPairwise [] [] = []
addPairwise x [] = []
addPairwise [] y = []
addPairwise (x:xs) (y:ys) = (x + y):(addPairwise xs ys)
-- $$ は最後に計算した結果を表す
isOdd n
| n <= 0 = False
| otherwise = isEven (n-1)
isEven n
| n < 0 = False
| n == 0 = True
| otherwise = isOdd (n-1)
-- 2000-10-30 課題1
maxOccursList :: [Int] -> (Int, Int)
maxOccursList x = (count (maximum x) x, maximum x)
where
count :: Int -> [Int] -> Int
count n [] = 0
count n (x:xs) | n==x = (count n xs) + 1
| True = count n xs
-- 2000-10-30 課題2
unique :: [Int] -> [Int]
unique xs = [x | x <- xs, count x xs == 1]
where
count n [] = 0
count n (x:xs) | n==x = (count n xs) + 1
| True = count n xs
--2000-10-31 課題1
sublist :: Eq a => [a] -> [a] -> Bool
sublist [] _ = True
sublist _ [] = False
sublist (x:xs) (y:ys)
| x == y = sublist xs ys
| True = sublist (x:xs) ys
subseq :: Eq a => [a] -> [a] -> Bool
subseq [] _ = True
subseq (x:xs) (y:ys)
| x == y = _subseq xs ys
| True = _subseq (x:xs) ys
where
_subseq [] _ = True
_subseq (x:xs) (y:ys)
| x == y = _subseq xs ys
| True = False
--2000-11-17 演習
--1. 整数のリストを引数とする,次の関数を定義せよ(mapまたはfilterを使うこと).
--a) 各要素の二乗のリストを返す関数.
doubleList :: [Int] -> [Int]
doubleList xs = map (2 *) xs
--b) 全ての要素が0より大きい場合にTrueを,そうでない場合にFalseを返す関数.
Link to this Page
- Haskell last edited on 19 March 2005 at 2:06:28 pm by 192.168.0.8