
























































































Studia grazie alle numerose risorse presenti su Docsity
Guadagna punti aiutando altri studenti oppure acquistali con un piano Premium
Prepara i tuoi esami
Studia grazie alle numerose risorse presenti su Docsity
Prepara i tuoi esami con i documenti condivisi da studenti come te su Docsity
Trova i documenti specifici per gli esami della tua università
Preparati con lezioni e prove svolte basate sui programmi universitari!
Rispondi a reali domande d’esame e scopri la tua preparazione
Riassumi i tuoi documenti, fagli domande, convertili in quiz e mappe concettuali
Studia con prove svolte, tesine e consigli utili
Togliti ogni dubbio leggendo le risposte alle domande fatte da altri studenti come te
Esplora i documenti più scaricati per gli argomenti di studio più popolari
Ottieni i punti per scaricare
Guadagna punti aiutando altri studenti oppure acquistali con un piano Premium
Knowledge Discovery nella Diagnosi di Sistemi Attivi
Tipologia: Dispense
1 / 96
Questa pagina non è visibile nell’anteprima
Non perderti parti importanti!

























































































Scope statico Sintassi più simile a LP tradizionali che a Lisp Dichiarazione di tipi Inferenza di tipi Tipizzazione forte (a differenza di Scheme, sostanzialmente typeless) Costrutto di modularizzazione per ADT FPL puro variabili, assegnamento, effetti collaterali Lazy evaluation : valutazione della expr solo quando necessario Possibile definire liste infinite
2+3 invece che (+ 2 3) 5(4+6)–2 _invece che ( ( 5 (+ 4 6)) 2)_ cavallo = flipV cavallo flipH cavallo invertColour (flipV cavallo) invertColour cavallo
flipH. flipV square :: Int > Int square n = n*n id :: type 1 -> type 2 -> … -> typek -> type id p 1 p 2 … pk = expr tipi dei parametri formali (^) tipo del risultato rotate :: Picture > Picture rotate figura = flipH (flipV figura) cavalloRuotato :: Picture cavalloRuotato = rotate cavallo rotate :: Picture > Picture rotate = flipH. flipV flipV^ flipH parametri formali
exOr :: Bool > Bool > Bool exOr x y = (x || y) && not (x && y) artNot :: Bool > Bool artNot True = False artNot False = True exOr :: Bool > Bool > Bool exOr True x = not x exOr False x = x
x y exOr True True False True False True False True True False False False
max :: Int > Int > Int max x y | x >= y = x | otherwise = y max3 :: Int > Int > Int > Int max3 x y z | x >= y && x >= z = x | y >= z = y | otherwise = z id p 1 p 2 … pk | g 1 = expr 1 | g 2 = expr 2 … … | otherwise = expr parametri formali opzionale
if cond then expr 1 else expr 2 max :: Int > Int > Int max x y = if x >= y then x else y max3 :: Int > Int > Int > Int max3 x y z = if x >= y && x >= z then x else if y >= z then y else z obbligatorio
Funzione Protocollo Significato
2 max 3 max 2 3 (|||) :: Bool > Bool > Bool True ||| x = not x False ||| x = x exOr :: Bool > Bool > Bool exOr True x = not x exOr False x = x
divide :: Int > Int > Int divide n m | n < m = 0 | otherwise = 1 + divide (nm) m remainder :: Int > Int > Int remainder n m | n < m = n | otherwise = remainder (nm) m
sumFacs :: Int > Int sumFacs n | n == 0 = 1 | otherwise = sumFacs(n1) + fac n sumF :: ( Int > Int ) > Int > Int sumF f n | n == 0 = f(0) | otherwise = sumF f (n1) + f n sumFacs :: Int > Int sumFacs n = sumF fac n sumSquares :: Int > Int sumSquares n = sumF square n sumFibs :: Int > Int sumFibs n = sumF fib n
fib :: Int > Int fib n | n == 0 = 0 | n == 1 = 1 | n > 1 = fib(n2) + fib(n1)
fcont :: Integer > Integer fcont 0 = 1 fcont 1 = 1 fcont n = fcont(n2) + fcont(n1) + 1 n fcont n 0 1 1 1 2 3 3 5 4 9 5 15 6 25 7 41 8 67 9 109 10 177 ... ... 35 29860703
type Articolo = ( String , Int ) ("Sale:^ 1Kg",^ 50) ("Latte: 1Lt", 90) minAndMax :: Int > Int > ( Int , Int ) minAndMax x y | x <= y = (x, y) | otherwise = (y, x) nuovoArt :: String > Int > Articolo nuovoArt nome costo = (nome, costo) ordinati :: Int > Int > Int > ( Int , Int , Int ) ordinati x y z | x <= y && y <= z = (x, y, z) | x <= z && z <= y = (x, z, y) | y <= x && x <= z = (y, x, z) | y <= z && z <= x = (y, z, x) | z <= x && x <= y = (z, x, y) | otherwise = (z, y, x)
fib :: Int > Int fib n | n == 0 = 0 | n == 1 = 1 | n > 1 = fib(n2) + fib(n1) Inefficienza: ricomputazione di fib(n2) per computare fib(n1) soluzione efficiente mediante l’uso di tuple fibStep :: ( Int , Int ) > ( Int , Int ) fibStep (u, v) = (v, u+v) fibPair :: Int > ( Int , Int ) fibPair n | n == 0 = (0, 1) | otherwise = fibStep (fibPair (n1)) fastFib :: Int > Int fastFib = fst. fibPair n Coppia 0 (0,1) 1 (1,1) 2 (1,2) 3 (2,3) 4 (3,5) 5 (5,8) 6 (8,13) 7 (13,21) Idea: mantenere gli ultimi due numeri nella tupla (u,v)
[1,2,3,4,2,3,4,8] :: [ Int ] [True] :: [ Bool ] ['c','i','a','o'] :: String "ciao" :: String [[1,2,3],[45,53,12,68]] :: [[ Int ]] [fac, square, cube] :: [ Int > Int ]