-- CSE 341, 1999su -- Assignment 3 -- sample solutions -- Ken Yasuhara-- July, 1999 module A3 where ---------------------------------------------------------------------- -- no. 1 squareList :: Num a => [a] -> [a] squareList = map (\x -> x * x) -- also: map (^2) squareList2 [] = [] squareList2 (x:xs) = (x * x):(squareList2 xs) -- also: -- squareList2 l = [x^2 | x <- l] ---------------------------------------------------------------------- -- no. 2 filterGE k l = [ x | x <- l, x >= k ] -- also: -- filterGE k [] = [] -- filterGE k (x:xs) | x >= k = x:(filterGE k xs) -- | otherwise = filterGE k xs ---------------------------------------------------------------------- -- nos. 3, 4 factors k = [x | x <- [2..k], mod k x == 0] -- also: k `mod` x == 0 primes = [x | x <- [2..], factors x == [x]] ---------------------------------------------------------------------- -- no. 5 isMember _ [] = False isMember k (x:xs) | x == k = True | otherwise = isMember k xs union s1 s2 = s1 ++ [x | x <- s2, not (isMember x s1)] -- also: -- union [] [] = [] -- union [] xs = xs -- union xs [] = xs -- union (x:xs) ys | isMember x ys = union xs (ys) -- | otherwise = union xs (x:ys) intersection s1 s2 = [ x | x <- s1, isMember x s2 ] -- also: -- intersection [] [] = [] -- intersection [] xs = [] -- intersection (x:xs) ys | isMember x ys = x:(intersection xs ys) -- | otherwise = intersection xs ys difference s1 s2 = [ x | x <- s1, not (isMember x s2) ] -- also: -- difference [] xs = [] -- difference (x:xs) ys | not (isMember x ys) = x:(difference xs ys) -- | otherwise = difference xs ys -- end of file