Lösningsförslag till KS 2010 i funktionell programmeing (Haskell)

Uppgift 1 a)

Uppgift 2 c)

Uppgift 3 d)

Uppgift 4
fkn :: [a] -> [a] -> [a]
fkn [] _ = []
fkn _ [] = []
fkn (x:xs) (y:ys) = (x:y:fkn xs ys)

*Main> fkn [1,3,5] [2,4,6]
[1,2,3,4,5,6]

*Main> fkn [1,3,5] [2]
[1,2]
Uppgift 5
truefalse :: [Bool]
truefalse = True : False : truefalse
Uppgift 6
vartannat [] = []
vartannat [x] = [x]
vartannat (x1:x2:xs) = x1:(vartannat xs)
Uppgift 7
data TMap a = Leaf a
            | Children [TMap a]
              deriving Show

t1 = Children [Children [Leaf 1, Leaf 2], Leaf 3]

tmap funk (Leaf a) = Leaf (funk a)
tmap funk (Children [a]) = Children [tmap funk a]
tmap funk (Children [a,b]) = Children [tmap funk a, tmap funk b]
Uppgift 8
both f1 f2 xs = filter f1 (filter f2 xs)
Uppgift 9
data HElem = IE Int
           | FE Float
           | RE Rational
     deriving (Show, Eq)

tal = [IE 17, FE 1.0, RE 1.2, IE 23]

baraheltal [] = []
baraheltal ((IE tal):xs) =  tal : (baraheltal xs)
baraheltal (_:xs) = baraheltal xs

red_ut tallista = ordning tallista ([],[],[])

ordning [] svar = svar
ordning ((IE tal):xs) (a,b,c) = ordning xs (a ++ [tal], b,c)
ordning ((FE tal):xs) (a,b,c) = ordning xs (a, b ++ [tal],c)
ordning ((RE tal):xs) (a,b,c) = ordning xs (a, b, c ++ [tal])