Preludeつづき
今日は,cycleから.
cycle
配列をぐるぐると,無限数列を作る.cycle [0,1,0]でぐるぐる回すと,綺麗.
Hugs.Base> take 10 $ cycle $ take 2 "hoge" "hohohohoho"
まぁ簡単に書くと,こうか.
cycle' :: [a] -> [a] cycle' [] = error "empty list" cycle' (x:xs) = [x] ++ cycle' (xs ++ [x])
divMod
割り算の結果と余りが一緒に貰える,スグレモノ.
Hugs.Base> 10 `divMod` 3(3,1) Hugs.Base> 9 `divMod` 2 (4,1) Hugs.Base> 3 `divMod` 3 (1,0)
一瞬で出来た.
divMod' :: Integral a => a -> a -> (a,a) divMod' a b = (div a b , mod a b)
drop
Intの数だけ,前から順に落とす.
コレも一瞬で出来た.
drop' :: Int -> [a] -> [a] drop' 0 xs = xs drop' _ [] = [] drop' a (x:xs) = drop' (a - 1) xs
dropWhile
与えられた条件が成立しなくなるまで,前から順に落とす.dropよりも使えそう.
dropWhile' :: (a -> Bool) -> [a] -> [a] dropWhile' _ [] = [] dropWhile' f (x:xs) | f x = dropWhile' f xs | otherwise = x:xs
elem
与えられた要素が与えられたリストの中にあるかどうか.
elem' :: Eq a => a -> [a] -> Bool elem' _ [] = False elem' a (x:xs) | a == x = True | otherwise = elem' a xs
enumFrom
enum -> enumerate -> 数え上げ,ということで,与えられた文字なり数なりを数え上げてリストにする.
enumFrom' :: Enum a => a -> [a] enumFrom' a = a:enumFrom (succ a)
enumFromThen
うお,何か凄いぞ.今日の自作関数はコイツにしよう.
それじゃ今日はもう寝るか.