RWH 4.5.5 部分リストを使う
myTake :: Int -> [a] -> [a] myTake _ [] = [] myTake n (x:xs) | n > 0 = x : (myTake (n - 1) xs) | otherwise = [] myDrop :: Int -> [a] -> [a] myDrop _ [] = [] myDrop n (x:xs) | n > 0 = myDrop (n - 1) xs | otherwise = x:xs mySplitAt :: Int -> [a] -> ([a], [a]) mySplitAt n xs = (myTake n xs, myDrop n xs) myTakeWhile :: (a -> Bool) -> [a] -> [a] myTakeWhile _ [] = [] myTakeWhile f (x:xs) | f x == True = x:(myTakeWhile f xs) | otherwise = [] myDropWhile :: (a -> Bool) -> [a] -> [a] myDropWhile _ [] = [] myDropWhile f (x:xs) | f x == True = myDropWhile f xs | otherwise = x:xs mySpan :: (a -> Bool) -> [a] -> ([a], [a]) mySpan f xs = (myTakeWhile f xs, myDropWhile f xs) myBreak :: (a -> Bool) -> [a] -> ([a], [a]) myBreak f xs = (myTakeWhile f' xs, myDropWhile f' xs) where f' = not . f
もっとエレガントな定義があるんじゃないかと思うけど、でもとりあえずひたすら自力で書いてみる。