Edit this page Backlinks This page is read only. You can view the source, but not change it. Ask your administrator if you think this is wrong. ====== Aplicatii (higher-order functions) ====== Separarea unui sir dupa un caracter, folosind ''foldr'': <code haskell> 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 ' ' </code> Parsarea unei matrici folosind compunere si functii de ordin superior: <code haskell> parseMat :: String -> [[Integer]] parseMat = (map (map read)) . (map (charSep ' ')) . (charSep '\n') </code> Inmultirea unei matrici: <code haskell> 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 </code>