ababup1192
7/20/2016 - 3:04 AM

# H会演習

## 1. 以下に適切なコードを埋めよ

``````import Data.List
wordNums :: String -> [(String, Int)]
wordNums = map (\ws -> (head ws, length ws)) . group . sort . words
``````

## 2. 以下に適切なコードを埋めよ

``````import Data.List
isIn :: (Eq a) => [a] -> [a] -> Bool
isIn xs ys = any (xs `isPrefixOf`) (tails ys)
``````

## 3. 以下に適切なコードを埋めよ

1,2 に出てきた関数を自力で実装せよ

``````tails' :: [a] -> [[a]]
tails' xs =  reverse \$ foldl (\acc x -> drop (snd x) xs : acc) [xs] xsi
where
xsi = zip xs [1..]

isPrefixOf' :: (Eq a) => [a] -> [a] -> Bool
isPrefixOf' [] _ = True
isPrefixOf' _ [] = False
isPrefixOf' (x:xs) (y:ys) = if x == y then isPrefixOf' xs ys else False

group' :: (Eq a) => [a] -> [[a]]
group' [] = []
group' [x] = [[x]]
group' (x:y:xs) = if x == y then (x : y : []) : (group' xs) else (x : []) : group' (y:xs)

any' :: (a -> Bool) -> [a] -> Bool
any' p xs = foldl (\acc x -> if acc then True else p x) False xs

``````

## 4. 以下に適切なコードを埋めよ

Maybeとは何か、Maybeを利用した関数を何か一つ実装せよ

``````getEven :: Int -> Maybe Int
getEven x = if x `mod` 2 == 0 then Just x else Nothing
``````