Title | Haskell Tests |
---|---|
Course | Lenguajes de programación |
Institution | Instituto Tecnológico y de Estudios Superiores de Monterrey |
Pages | 1 |
File Size | 30.5 KB |
File Type | |
Total Downloads | 10 |
Total Views | 156 |
Data Structure problems using haskell...
module HaskelTests where -- Haskell -- Problem 1 - Matrix Multiplication matMult :: [[Int]] -> [[Int]] -> [[Int]] matMult [x] y = [rowMult x y] matMult (x:xs) y = rowMult x y : matMult xs y rowMult :: [Int] -> [[Int]] -> [Int] rowMult [] _ = [] rowMult _ ([]:_) = [] rowMult x y = colMult x (map head y) : rowMult x (map tail y) colMult :: [Int] -> [Int] -> Int colMult [x] y = x * head y colMult x [y] = head x * y colMult (x:xs) (y:ys) = x * y + colMult xs ys -- Problem 2 - Table summarization -- The input table has to be a list of pairs, containing the gender and the height -- [("Male",175),("Female",154),("Female",152)] -> Result is -> [("Male",175),("Female",153)] -- Using doubles to be able to do average sumarizeHeights :: [(String,Double)] -> [(String,Double)] sumarizeHeights [] = error "Empty list" sumarizeHeights x = ("Female",auxSum (map snd (filter (\(x,_) -> x == "Female") x))) : [("Male",auxSum (map snd (filter (\(x,_)-> x == "Male") x)))] auxSum :: [Double] -> Double auxSum x = sum x / fromIntegral (length x) -- Problem 3 - List comprehension -- Using doubles again even if not needed to use the same structure than before heightThreshold :: [(String,Double)] -> Double -> [(String,Double)] heightThreshold x threshold = filter (\(_,y) -> y < threshold) x...