Aplicatii (higher-order functions)

Separarea unui sir dupa un caracter, folosind foldr:

spaceSep :: String -> [String]
spaceSep = foldr op []
               where op :: Char -> [String] -> [String]
                     op ' ' acc = []:acc
                     op x [] = [[x]]  -- atentie!
                     op x (y:ys) = (x:y):ys 
 
charSep :: Char -> String -> [String]
charSep c = foldr op []
               where op x []
                       | x == c = []
                       | otherwise = [[x]]
                     op x (y:ys)
                       | x == c = []:y:ys
                       | otherwise = (x:y):ys
 
spaceSep1 = charSep ' '                     

Parsarea unei matrici folosind compunere si functii de ordin superior:

parseMat :: String -> [[Integer]]
parseMat = (map (map read)) . 
           (map (charSep ' ')) .
           (charSep '\n')

Inmultirea unei matrici:

transpose ([]:_) = []
-- ([]:_) -> o lista NEVIDA care contine ca prim element, []
transpose m = (map head m):(transpose (map tail m))
multiply m1 m2 = map (\lx -> map (\cy -> foldr (+) 0 (zipWith (*) lx cy)) (transpose m2)) m1