1 / 30

Paradigmer i Programmering 2

Paradigmer i Programmering 2. PIP. Sammensatte typer. Tupler og lister Programmering med rekursion Programmering med pipe-lines Programmering med polymorfe typer I eftermiddag: + flere opgaver + spørgsmål/svar. Tupler og lister. Moscow ML version 2.00 (June 2000) Enter `quit();' to quit.

alka
Download Presentation

Paradigmer i Programmering 2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Paradigmer i Programmering 2 PIP

  2. Sammensatte typer Tupler og lister Programmering med rekursion Programmering med pipe-lines Programmering med polymorfe typer I eftermiddag: + flere opgaver + spørgsmål/svar

  3. Tupler og lister Moscow ML version 2.00 (June 2000) Enter `quit();' to quit. - (1,"strng",true); > val it = (1, "strng", true) : int * string * bool - [1,2,3]; > val it = [1, 2, 3] : int list -

  4. Tupler af lister/lister af tupler - [(1,2),(3,4),(5,6)]; > val it = [(1, 2), (3, 4), (5, 6)] : (int * int) list - ([1,2,3],[4,5,6]); > val it = ([1, 2, 3], [4, 5, 6]) : int list * int list -

  5. Tupler - val x = (1,"1"); tupler er værdier > val x = (1, "1") : int * string - val (x1,x2) = x; tupler som mønstre > val x1 = 1 : int val x2 = "1" : string - fun f1 (x1,x2) = x1+x2;i funktioner > val f1 = fn : int * int -> int - fun f2 x = (x-1,x+1); > val f2 = fn : int -> int * int

  6. Polymorfe typer - fun swop (x,y) = (y,x); i funktioner > val ('a, 'b) swop = fn : 'a * 'b -> 'b * 'a - swop x; > val it = ("1", 1) : string * int 'a og 'b er typevariable: funktionen er defineret hvor 'a og 'b er vilkårlige typer. f.eks: swop : int * string -> string * int

  7. Pipe-lines - f1 (f2 1); > val it = 2 : int - val f12 = f1 o f2; > val f12 = fn : int -> int - f12 1; > val it = 2 : int Funktionssammensætning med o eller således - fun f12 x = f1 (f2 x); > val f12 = fn : int -> int

  8. Polymorfi Moscow ML version 2.00 (June 2000) Enter `quit();' to quit. - fun f x = x; > val 'a f = fn : 'a -> 'a - fun g x = x+1; > val g = fn : int -> int - fun h x = f ( g (x)); > val h = fn : int -> int

  9. I java public class tst2{ Object f (Object x){return x;} String g (String x){return x+" ";} String h (String x){return f(g(x));} public static void main(String[] args){} } tst2.java:4: incompatible types found : java.lang.Object required: java.lang.String String h (String x){return f(g(x));} ^ Men ok hvis vi tilføjer String f (String x){return x;}

  10. Begreber I ML: val 'a f = fn : 'a -> 'a f er polymorf – defineret for alle mulige typer ’a og vil returnere noget af samme type. I Java Object f (Object x){return x;} f er defineret for en super type og kan bruges for alle subtyper String f (String x){return x;} f er generisk – forskellige definitioner for forskellige argumenttyper

  11. Syntaks for del af ML exp : exp1 { exp1 } | exp : type | exp infixop exp | raise exp | exp handle match | case exp of match | if exp then exp else exp | fn match | let dec in exp { ; exp } end exp1 : name | number | string | char | ( exp {, exp } ) | ( exp { ; exp } ) | [ exp {, exp} ] | op infixop

  12. Syntaks for del af ML match : pattern => exp { | pattern => exp } pattern : name | _ | number | string | char | ( pattern { , pattern } ) | [ pattern { , pattern } ] | () | [] | name as pattern | pattern : type | pattern infixcon pattern

  13. Syntaks for del af ML dec : val pattern = exp {and pattern = exp } | pattern ; pattern | type name = type | fun fdef {and fdef } | datatype name = cdef { | cdef } fdef : name pattern {pattern} = exp | op infixop pattern {pattern} = exp cdef : name of type type: name | type { * type } | type -> type | ( type ) | type name

  14. Lister - val x = [1,2,3]; lister som værdier > val x = [1, 2, 3] : int list - val [x1,x2,x3]= x; lister i mønstre > val x1 = 1 : int val x2 = 2 : int val x3 = 3 : int - val [x1] = x; bemærk fejl ! Uncaught exception: ! Bind mønster og værdi passer ikke sammen

  15. lister - fun f(x1,x2) = 1;bemærk ok > val ('a, 'b) f = fn : 'a * 'b -> int - fun f [x1,x2] = 1; bemærk fejl ! Toplevel input: ! fun f [x1,x2] = 1; ! ^^^^^^^^^^^^^ ! Warning: pattern matching is not exhaustive > val 'a f = fn : 'a list -> int - fun tom [] = true | tom x = false; > val 'a tom = fn : 'a list -> bool

  16. Kolon kolon, nil, [ ] - 1 :: []; > val it = [1] : int list - 1 :: nil ; > val it = [1] : int list - 1::2::3::[]; > val it = [1, 2, 3] : int list - []; > val 'a it = [] : 'a list - nil; > val 'a it = [] : 'a list

  17. Kolon kolon - 1 :: []; > val it = [1] : int list - [] :: 1; ! Toplevel input: ! []::1; ! ^ ! Type clash: expression of type ! int ! cannot have type ! 'a list list - []::[]; > val 'a it = [[]] : 'a list list

  18. Infix operationer - op +; > val it = fn : int * int -> int - op ::; > val 'a it = fn : 'a * 'a list -> 'a list - op ^; > val it = fn : string * string -> string - op @; > val 'a it = fn : 'a list * 'a list -> 'a list - op o; > val ('a, 'b, 'c) it = fn : ('a -> 'b) * ('c -> 'a) -> 'c -> 'b

  19. Funktioner på lister op @ : 'a list * 'a list -> 'a list rev : 'a list -> 'a list length : 'a list -> int null : 'a list -> bool hd : 'a list -> 'a tl : 'a list -> 'a list List.nth : 'a list * int -> 'a List.last : 'a list -> 'a List.take : 'a list * int -> 'a list List.drop : 'a list * int -> 'a list List.concat : 'a list list -> 'a list List.revAppend : 'a list * 'a list -> 'a list

  20. Lister Enten er lister tomme eller også har de et hoved og en hale [ ] [1,2] 1 2 nil

  21. Funktioner på lister Man deler efter hvordan lister kan se ud - fun erTom [] = true | erTom xs = false; > val 'a erTom = fn : 'a list -> bool - erTom nil; > val it = true : bool - erTom [1]; > val it = false : bool -

  22. Sammenhæftning af lister Sammenhæftning - [1,2]@[3,4]; > val it = [1, 2, 3, 4] : int list Nedbryd til simplere problem [1,2] @ [3,4] = (1::[2]) @ [3,4] (1::[2]) @ [3,4] = 1 :: ([2] @ [3,4]) [2] @ [3,4] = 2 :: ([ ] @ [3,4]) [ ] @ [3,4] = [3,4]

  23. Append -fun append ([],ys)=ys | append (x::xs,ys) = x::append(xs,ys); > val 'a append = fn : 'a list * 'a list -> 'a list - Problemet er løst rekursivt

  24. Findes noget i en liste Definer funktion member så - member(1,[2,4,7,1,4]); > val it = true : bool - member(1,[2,4,7,4]); > val it = false : bool Tre situationer: • listen er tom • element findes som hoved • element søges i halen

  25. Member - fun member (x,[ ]) = false | member (x,(x1::xs)) = if x = x1 then true else member(x,xs); > val ''a member = fn : ''a * ''a list -> bool ''a er en typevariabel for typer hvor værdier kan sammenlignes med ”=” Det kaldes ”equality type”.

  26. Eksempler Foreningsmængde (union) [1,2,5,7]  [3,5,6] = [1,2,3,5,6,7] Fællesmængde (intersect) [1,2,5,7]  [3,5,6] = [5] Mængdedifferens (setdiff) [1,2,5,7] ∖ [3,5,6] = [1,2,7]

  27. remove Fjern værdier fra liste - fun remove(x,[]) = [] | remove(x,y::ys) = if x=y then remove(x,ys) else y::remove(x,ys); > val ''a remove = fn : ''a * ''a list -> ''a list - remove(1,[2,5,1,7,4,3,1,2]); > val it = [2, 5, 7, 4, 3, 2] : int list

  28. removeDub Fjern dupletter i en liste - fun removeDub [] = [] | removeDub(x::xs) = x::removeDub(remove(x,xs)); > val ''a removeDub = fn : ''a list -> ''a list - removeDub [1,5,2,3,1,2,6,4,6]; > val it = [1, 5, 2, 3, 6, 4] : int list

  29. Opgaver definer split så split [x1,x2,x3,x4,..] = ([x1,x3,..],[x2,x4,..]) definer length(lst) - længde af liste nth(i,lst) - returner i’de element i listen drop(i,lst) - drop første i elementer take(i,lst) - tage første i elementer

  30. Lav streng til store bogstaver Definer upString så: upString ”tekst” = ”TEKST” brug - explode: string -> char list laver streng om til liste af tegn - implode : char list -> string laver liste af tegn om til streng - Char.toUpper : char -> char laver bogstaver om til store (og andet uændret)

More Related