["Hej", "bees", "Bye bye"] : string list"> ["Hej", "bees", "Bye bye"] : string list">
Ladda ner presentationen
Presentation laddar. Vänta.
Publicerades avKerstin Sandström
1
1 Listor En sekvens värden av samma typ Nr 5
2
2 Listor Sekvens av värden, element Variabelt antal Alla med samma typ Bara första elementet direkt åtkomligt
3
3 Exempel Heltalslista [1,2,3,4,78,3] > [1,2,3,4,78,3] : int list Sträng lista ["Hej", plural "bee", "Bye "^"bye"] > ["Hej", "bees", "Bye bye"] : string list
4
4 Grundvärden En lista byggs upp genom att lägga ett element till framför en list Listan består av ett par (head och tail) head:det första elementet i listan tail: resten av lista Alla listor slutar med tomma listan, nil []
5
5 Cons (::) Listan konstrueras med den infixa list konstrueraren :: uttalas som cons expression 1 :: expression 2 –expression 2 måste vara en lista med element som har samma typ som expression 1 - op :: ; > :: = fn : 'a * 'a list -> 'a list (E 1 :: E 2 ) (A, A list) : A list - 1 :: (2 :: []) > [1,2] : int list
6
6 Cons höger associativt Cons är höger associativ 1 :: (2 :: (3 :: nil)) == 1 :: 2 :: 3 :: nil == [1,2,3] Förenklad form expression 1 :: epxression 2 :: … :: expression n :: [] [expression 1, epxression 2, …, expression n ] 1+2 :: 3+4 :: 3+4*2 :: 0 :: nil > [3, 7, 11, 0] : int list
7
7 Ortogonalitet Inga operationer godtyckligt begränsade på grund av ingående typer Vad vi ska kunna göra med värden av alla typer: –Beräkna –Vara del i sammansatt värde (tuppler, listor,…) –Vara argument till funktioner –Vara resultat från funktioner
8
8 Exempel Högre ordningens funktioner twice : ('a -> 'a) -> 'a -> 'a Listor som argument och resultat av funktioner length : 'a list -> int fromto : int -> int -> int list Listor av tuppler, tuppler av listor [(1,"Hej"),(98,"Sen")] : (int * string) list ([1,98],["Hej","Sen")] : (int list) * (string list) Listor av listor [[2,3,4],[34,55,66],[99],[]] : int list list Listor av tuppler av funktioner [(sin,plural),(cos,ended)] : ((real -> real) * (string -> string)) list
9
9 Formell definition av en lista [] (nil) är en lista om expression 1 har typen 'a och expression 2 har typen 'a list så är expression 1 :: expression 2 en 'a list [] för basfallet vid rekursion (head :: tail) för det rekursiva fallet fun name [] = base case | name (x :: xs) = recursion case using x and xs
10
10 Exempel fun length [] = 0 | length (x :: xs) = 1 + length xs > val length = fn : 'a list -> int length [1,2,3] ==> 1 + length [2,3] => 1 + (1 + length [3]) ==> 1 + (1 + (1 + length [])) ==> 1 + (1 + (1 + 0)) ==> 3 fun sum nil = 0 | sum (x :: xs) = x + sum xs > val sum = fn : int list -> int
11
11 Fler exempel fun count0 nil = 0 | count0 (0 :: xs) = 1 + count0 xs | count0 (_ :: xs) = count0 xs > val count0 = fn : int list -> int count0 [0,1,2,0] ==> 1 + count0 [1,2,0] ==> 1 + count0 [2,0] ==> 1 + count0 [0] ==> 1 + (1 + count0 []) ==> 1 + (1 + 0) ==> 2
12
12 Fler exempel fun count _ nil = 0 | count v (x :: xs) = if v = x then 1 + count v xs else count v xs > val count = fn : ''a -> ''a list -> int count "Hej" ["Bay","Hejdå","Hej","Vids","Hej"]; > val it = 2 : int - count sin [sin,cos,arctan]; ! Toplevel input: ! count sin [sin,cos,arctan]; ! ^^^ ! Type clash: expression of type ! real -> real ! cannot have equality type ''a
13
13 Generella list operationer Lägga till på slutet Lägga ihop två listor Sätta in före ett värde Ta bort ett värde Generell form fun name [] = ? | name (x :.xs) = ??
14
14 Lägga till på slutet fun add v [] = [v] | add v (x :: xs) = x :: add v xs > val add = fn : 'a -> 'a list -> 'a list add 5 [1,2,3,4] > [1,2,3,4,5] : int list add 5 [1,2,3] ==> 1 :: add 5 [2,3] ==> 1 :: 2 :: add 5 [3] ==> 1 :: 2 :: 3 :: add 5 [] ==> 1 :: 2 :: 3 :: [5] ==> 1 :: 2 :: [3,5] ==> 1 :: [2,3,5] ==> [1,2,3,5]
15
15 Lägga ihop två listor fun append [] ys = ys | append (x::xs) ys = x :: append xs ys > val append = fn : 'a list -> 'a list -> 'a list append ["a", "c", "d"] ["e", "b", "h"] ==> "a" :: append ["c", "d"] ["e", "b", "h"] ==> "a" :: "c" :: append ["d"] ["e", "b", "h"] ==> "a" :: "c" :: "d" :: append [] ["e", "b", "h"] ==> "a" :: "c" :: "d" :: ["e", "b", "h"] ==> "a" :: "c" :: ["d", "e", "b", "h"] ==> "a" :: ["c", "d", "e", "b", "h"] ==> ["a", "c", "d", "e", "b", "h"]
16
16 Sätta in före ett värde insertb "c" "d" ["a", "b","d","e"] ["a", "b","c","d","e"] : string list fun insertb _ _ [] = [] | insertb v1 v2 (x::xs) = if v2 = x then v1 :: x :: xs else x :: insertb v1 v2 xs val insertb = fn : ''a -> ''a -> ''a list -> ''a list insertb 1 3 [1,2,3,4] ==> 1 :: insertb 1 3 [2,3,4] ==> 1 :: 2 :: insertb 1 3 [3,4] ==> 1 :: 2 :: 1 :: 3 :: [4] ==> … [1,2,1,3,4]
17
17 Sätt in före alla av ett värde insertball "c" "d" ["d", "b","d","e"] ["c", "d", "b","c","d","e"] : string list fun insertball _ _ [] = [] | insertball v1 v2 (x::xs) = if v2 = x then v1 :: x :: insertball xs else x :: insertball v1 v2 xs val insertball = fn : ''a -> ''a -> ''a list -> ''a list insertball 1 3 [1,3,4,3] ==> 1 :: insertball 1 3 [2,3,4,3] ==> 1 :: 1 :: 3 :: insertball 1 3 [4,3] ==> 1 :: 1 :: 3 :: 4 :: insertball 1 3 [3] ==> 1 :: 1 :: 3 :: 4 :: 1 :: 3 :: insertball 1 3 [] ==> 1 :: 1 :: 3 :: 4 :: 1 :: 3 :: [] ==> … [1,1,3,4,1,3]
18
18 Ta bort ett värde delete 2 [1,2,3,2] > [1,3,2] : int list fun delete _ [] = [] | delete v (x :: xs) = if x = v then xs else x :: delete v xs > val delete = fn : ''a -> ''a list -> ''a list delete 2 [1,2,3,2] ==> 1 :: delete 2 [2,3,2] ==> 1 :: [3,2] ==> [1,3,2]
19
19 Ta bort alla förekomster deleteall 2 [1,2,3,2] > [1,3] : int list fun deleteall _ [] = [] | deleteall v (x :: xs) = if x = v then deleteall v xs else x :: deleteall v xs > val deleteall = fn : ''a -> ''a list -> ''a list deleteall 2 [1,2,3,2] ==> 1 :: deleteall 2 [2,3,2] ==> 1 :: deleteall 2 [3,2] ==> 1 :: 3 :: deleteall 2 [2] ==> 1 :: 3 :: deleteall 2 [] ==> 1 :: 3 :: [] ==> [1,3]
20
20 hd och tl Explicit urval ur listor Om vi vill ta fram head eller tail utan mönstermatchning exception Hd and Tl > exception Hd excpetion Tl fun hd (x::xs) = x | hd [] = raise Hd > val hd = fn 'a list -> 'a fun tl (x::xs) = xs | tl [] = raise Tl > val tl = fn : 'a list -> 'a list hd (tl (tl (tl [1,2,3,4,5]))) > 4 : int
21
21 Indexera listor Ta fram värde i en viss position find 4 ["Hej","So","bara","Most", "Lava"] > "Most" : string exception Find > exception Find fun find 1 (x::xs) = x | find i (x::xs) = find (i-1) xs | find _ [] = raise Find > val find = fn : int -> 'a list -> 'a
Liknande presentationer
© 2024 SlidePlayer.se Inc.
All rights reserved.