import Data.List import Debug.Trace --------------------Programare point-free {- odds lst = filter odd lst odds lst = (filter odd) lst --asociativitate la stânga -} odds lst = filter odd lst --------------------Definiții point-free myLast :: [a] -> a myLast = head . reverse myMin :: Ord a => [a] -> a myMin = head . sort myMax :: Ord a => [a] -> a myMax = myLast . sort mySum :: Num a => [a] -> a mySum = foldl (+) 0 myAnd :: [Bool] -> Bool myAnd = foldl (&&) True myAny :: (a -> Bool) -> [a] -> Bool myAny = ((not . null) . ) . filter --ex --selectarea listelor nevide dintr-o listă de liste --[[],[1],[],[2,3]] ===> [[1],[2,3]] --evoluăm treptat de la varianta fără compunere la cea cu compunere --notNull :: [[a]] -> [[a]] --notNull lst --înlocuirea cu 1 a head-ului fiecărei liste dintr-o listă de liste nevide --[[1],[2,3],[4,5,6]] ===> [[1],[1,3],[1,5,6]] --replaceH :: Num a => [[a]] -> [[a]] --replaceH lst --------------------Operatorul $ --de încercat în terminal --take 4 $ filter (odd . fst) $ zip [1..] [2..] --------------------Evaluare leneșă xs :: [Int] xs = [id 2, trace "8" 2*4] f x = trace "x" 2*x g x = f 2 + f 2 -- se evaluează de ? ori la apelul g 5 h x = x * x * x -- ? la apelul h (f 2) {- g 5 = h (f 2) = -} i x y = if y > 5 then y * y else x * x -- la apelul i (f 2) (f 3) ? --------------------Fluxuri {- (define naturals (let loop ((n 0)) (stream-cons n (loop (add1 n))))) -} --naturals {- (define ones (stream-cons 1 ones)) -} --ones {- (define fibonacci (stream-cons 0 (stream-cons 1 (stream-zip-with + fibonacci (stream-rest fibonacci))))) -} --fibonacci --evens --ex: 1/1, 1/2, 1/3... --invNats --ex: 1, 1/(1*2), 1/(2*3), 1/(3*4)... --invProds --------------------List comprehensions lc1 = [ (x,y,z) | x<-[1..3], y<-[1..4], x