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


(1)
  foo2 []     = []
  foo2 (x:xs) = if x > 0 then x : foo2 xs else foo2 xs

  foo1 []     = 0
  foo1 [x]    = x
  foo1 (x:xs) = x `min` foo1 xs

  foo xs = foo1 (foo2 xs)

-- 別解
  foo []  = 0
  foo (x:xs) = let rest = foo xs 
                 in if x < 0 then rest
                      else if rest == 0 then x 
                             else x `min` rest
(2)
  bar n = [(i,j) | i<-[0..n], j<-[0..n], (2*i+j) `mod` 3 == 0 ]

(1) [1,4,16,25,49]
(2) [(2,4),(3,4),(3,7),(4,4),(4,7),(4,10)]