プログラミング・パラダイム(2021 年度)テスト解答例


(1)
foo []     = True
foo (x:xs) = (x `mod` 2 == 0 || x > 0) && foo xs
(2)
bar n = [(x,y) | x <- [0..n], y <- [x .. (3 * x)], (x * x + y * y) `mod` 3 == 1 ]
Ⅱ(非プログラミング問題)

(1)  [1,6,9,27,54,135,297]

(2)  [(3,3),(3,6),(4,5),(4,8),(5,7),(6,6)]

Ⅱ(プログラミング問題)
(1)
baz xs = sum (map fst (filter (\ (x,y) -> x >= y) xs))
(2)
unit :: a -> [a]
unit a = a : []

bind :: [a] -> (a -> [b]) -> [b]
bind []     _ = []
bind (x:xs) f = (f x) ++ (bind xs f)

qux n = bind [0..n] (\ x -> bind [x..n] (\ y -> if (x + y) `mod` 2 == 0 then unit (x,y) else []))