{- 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 2 3 5 7 9 11 13 15 17 2 3 5 7 11 13 17 2 3 5 ... -} primes = sieve [2..] where sieve (p:numbers) = p : sieve [x | x <- numbers, x `mod` p /= 0] -- p știm că este număr prim -- în numbers au rămas doar numere care nu au divizori mai mici decât p {- înainte să evaluez primes = sieve [2..] dacă evaluez head primes primes = 2 : sieve [x | x <- [2..], x `mod` 2 /= 0] dacă evaluez primes !! 1 primes = 2 : 3 : sieve [x | x <- [x | x <- [2..], x `mod` 2 /= 0], x `mod` 3 /= 0] primes = 2 : 3 : 5 : sieve [x | x <- [x | x <- [x | x <- [2..], x `mod` 2 /= 0], x `mod` 3 /= 0], x `mod` 5 /= 0] ... -} f1 s1 s2 = length (s1 ++ s2) f g = (g 3) + 1 fix f = f ( fix f ) --f3 x = (x x) {- let m f l | l==[] = [] | True = f (head l) : m f (tail l) in m (+1) [1..] -} m f l | l==[] = [] | True = f (head l) : m f (tail l) a = m (+1) [1..] type ListInt = [Integer] -- alias al lui [Integer] myList :: ListInt myList = [1,2,3,4] mySecondList :: ListInt mySecondList = [1..10] fMyList :: Int -> ListInt fMyList n = myList ++ mySecondList -- construim un tip de date nou, ca enumerare de valori data FuzzyBoolean = CertainTrue | CertainFalse | Fuzzy -- tip de date nou -- FuzzyBoolean - constructor de tip -- 3 constructori de date -- pot apărea în expresii fromFuzzy CertainTrue = True fromFuzzy CertainFalse = False fromFuzzy Fuzzy = undefined -- construim un nou tip de date nou folosind constructori de bază. -- aici nu este nimic predefinit, aș putea da orice alt nume data Natural = Zero | Succ Natural deriving (Show, Eq) -- putem afișa la consolă și putem face == -- Zero -- constructor de date nular ~ valoare -- Succ -- constructor de date unar ~ funcție unu = Succ Zero doi = Succ unu addNat Zero n = n --addNat (Succ m) n = Succ (addNat m n) addNat (Succ m) n = Succ $ addNat m n {- rulați addNat (Succ (Succ doi)) (Succ (Succ (Succ Zero))) addNat unu doi addNat doi Zero == doi :t addNat -} lenNat [] = Zero --lenNat (_:t) = addNat unu (lenNat t) lenNat (_:t) = addNat unu $ lenNat t -- tip de date pentru arbori; arbore binar infinit de 1-uri -- Racket: (valoare . lista_copii) --treeOnes = (1, [treeOnes, treeOnes]) -- nu merge, tip infinit {- treeOnes :: x x = (Integer, [x]) -- fails occurs check -} {- data TreeInt = NilInt -- arborele vid | LeafInt Int -- frunză (conține o valoare) | NodeInt Int [TreeInt] -- nod interior (valoare + listă copii) -} -- mai corect, fără suprapunere semantică: data TreeInt = NilInt -- arborele vid | NodeInt Int [TreeInt] -- nod interior (valoare + listă copii) -- atenție! Definiție ineficientă (semnatic) în care se suprapun semantic constructorii de date data Tree a -- construiesc tipul Tree peste un alt tip, a -- o valoare de tip Tree a va fi un arbore care conține valori de tipul a = Nil | Leaf a | Node a [Tree a] -- copiii au tot tipul Tree a treeOnes = Node 1 [treeOnes, treeOnes] -- e.g. -- Node "radacina" [Node "copil1" [Leaf "frunza1", Leaf "frunza2"], Leaf "frunza3"] takeMy 0 _ = [] takeMy n (h:t) = h : takeMy (n-1) t -- limit primește un nivel și un arbore și taie arborele la nivelul respectiv limit 0 _ = Nil limit level t = case t of Node val children -> Node val $ map (limit (level-1)) children _ -> t instance Show a => Show (Tree a) where show tree = pt 0 tree where pt level tree = case tree of Nil -> space level ++ "- \n" Leaf val -> space level ++ show val ++ "\n" Node val children -> space level ++ show val ++ "\n" ++ if all isNil children then "" else concatMap (pt $ level + 1) children space sp = [' ' | _ <- [1..sp * 2]] isNil Nil = True isNil _ = False treeB = build 1 -- where build n = Node n [build $ n * 2, build $ n * 2 + 1] -- where build n = Node n $ map (\x -> build $ n * 2 + x) [0, 1] where build n = Node n $ map build $ map (+(2*n)) [0, 1] preord Nil = [] preord (Leaf x) = [x] --preord (Node x children) = x : concat (map preord children) preord (Node x children) = x : concatMap preord children {- încercați la consolă limit 5 treeOnes limit 5 treeB take 10 $ preord treeB -- doar calea de pe partea stângă a arborelui, pentru că nu ne întoarcem niciodată preord $ limit 5 treeB -} -- point free --incList list = map (+1) list --incList = \list -> (map (+1)) list incList = map (+1) -- map (+1) mai așteaptă încă un argument