だんだんエレガントにしようとするところが関数型言語スレらしいな。 ############################# import Data.Maybe -- 再帰で書いてみた f n xs = catMaybes $ f' n xs where f' n [] = [] f' n xxs@(x:xs) = (takeJust n xxs):f' n xs
-- AKA myTake and takeMaybe takeJust 0 xs = Just [] takeJust n [] = Nothing takeJust n (x:xs) = takeJust (n - 1) xs >>= \xs -> Just (x:xs)