-- Kontrolltöö 1 (näidis): lahendused %default total -- Ülesanne 1: listifunktsioon foldr-ga -- Algväärtus False vastab tühjale listile. Iga paari puhul piisab -- sellest, et selle komponendid on võrdsed või ülejäänud listis -- leidub juba sobiv paar. Funktsioon ei kasuta otsest rekursiooni. yl1 : List (Int, Int) -> Bool yl1 = foldr (\ (a, b), rest => if a == b then True else rest) False -- Main> yl1 [(1,2),(2,3),(4,4)] -- True -- Main> yl1 [(1,2),(2,3),(4,5),(1,3)] -- False -- Main> yl1 [] -- False -- Ülesanne 2: puu data BinTree a = Leaf | Branch (BinTree a) a (BinTree a) tree1 : BinTree (Int, Double) tree1 = Branch (Branch Leaf (1,0.5) Leaf) (5,1.5) (Branch Leaf (7,2.5) Leaf) -- Tühja puu summa on 0. Sõlme puhul liidame mõlema alampuu -- summad ning sõlme teise komponendi, kui predikaat on tõene. sumTreeIf : (a -> Bool) -> BinTree (a, Double) -> Double sumTreeIf p Leaf = 0.0 sumTreeIf p (Branch left (x, value) right) = sumTreeIf p left + (if p x then value else 0.0) + sumTreeIf p right -- Main> sumTreeIf (/=3) tree1 -- 4.5 -- Main> sumTreeIf (/=5) tree1 -- 3.0 -- Main> sumTreeIf (\ _ => False) tree1 -- 0.0 -- Main> sumTreeIf (\ _ => True) tree1 -- 4.5 -- Main> sumTreeIf (\ _ => True) (the (BinTree (Int, Double)) Leaf) -- 0.0