Presentasjon lastes. Vennligst vent

Presentasjon lastes. Vennligst vent

Paradigmer i Programmering 3. Højere ordens funktioner Idag: Højere ordens funktioner Algebraiske datatyper Næste gang: I/O, Filer, interaktive programmer.

Liknende presentasjoner


Presentasjon om: "Paradigmer i Programmering 3. Højere ordens funktioner Idag: Højere ordens funktioner Algebraiske datatyper Næste gang: I/O, Filer, interaktive programmer."— Utskrift av presentasjonen:

1 Paradigmer i Programmering 3

2 Højere ordens funktioner Idag: Højere ordens funktioner Algebraiske datatyper Næste gang: I/O, Filer, interaktive programmer

3 Flere parametre - fun add(x,y) = x+y; > val add = fn : int * int -> int - add(2,3); > val it = 5 : int - fun add x y = x+y; > val add = fn : int -> int -> int - add 1; > val it = fn : int -> int - val inc = add 1; > val inc = fn : int -> int - inc 4; > val it = 5 : int

4 map Anvend funktion på hvert element i en liste: - fun map f [] = [] | map f (x::xs) = (f x)::(map f xs); > val ('a, 'b) map = fn : ('a -> 'b) -> 'a list -> 'b list - map inc [1,2,3]; > val it = [2, 3, 4] : int list

5 Eksempel - fun upString x = implode (map Char.toUpper (explode x)); > val upString = fn : string -> string - upString "hejsahj "; > val it = "HEJSAHJ " : string

6 all, exists - fun all p [] = true | all p (x::xs) = (p x) andalso (all p xs); > val 'a all = fn : ('a -> bool) -> 'a list -> bool - fun exists p [] = false | exists p (x::xs) = (p x) orelse (exists p xs); > val 'a exists = fn : ('a -> bool) -> 'a list -> bool

7 Programmering med exists - fun eq x y = x=y; > val ''a eq = fn : ''a -> ''a -> bool - fun member elm lst = exists (eq elm) lst; > val ''a member = fn : ''a -> ''a list -> bool - member 3 [1,2,3,4]; > val it = true : bool - member 3 [1,2,4,5]; > val it = false : bool

8 Foldning af liste - fun foldr f b [] = b | foldr f b (x::xs) = f(x,foldr f b xs); > val ('a, 'b) foldr = fn : ('a * 'b -> 'b) -> 'b -> 'a list -> 'b - fun app xs ys = foldr op:: ys xs; > val 'a app = fn : 'a list -> 'a list -> 'a list - app [1,2,3] [4,5,6]; > val it = [1, 2, 3, 4, 5, 6] : int list - fun sum xs = foldr op+ 0 xs; > val sum = fn : int list -> int - sum [1,2,3,4]; > val it = 10 : int

9 Anonyme funktioner: fn - fun len xs = foldr (fn (_,x) => x+1) 0 xs; > val 'a len = fn : 'a list -> int - len [1,2,3]; > val it = 3 : int - len []; > val it = 0 : int eller.. - fun snd (x,y) = y; > val ('a, 'b) snd = fn : 'a * 'b -> 'b - fun len xs = foldr (inc o snd) 0 xs; > val 'b len = fn : 'b list -> int - len [1,2]; > val it = 2 : int

10 Algebraiske datatyper I mængdelæren: N  Mi SML: tupler N m i SML: lister N + Mi SML: datatyper N + M er stort set N  M, men man kan se om en værdi kommer fra den ene eller anden.

11 Eksempel - datatype Val = Chr of char | Str of string | Int of int; > New type names: =Val datatype Val = (Val,{con Chr: char -> Val, con Int: int -> Val, con Str: string -> Val}) con Chr = fn : char -> Val con Int = fn : int -> Val con Str = fn : string -> Val - Chr #"2"; > val it = Chr #"2" : Val - Int 3; > val it = Int 3 : Val -

12 Min liste - datatype Liste = Tom | Hoved of (int * Liste); > New type names: =Liste datatype Liste =(Liste,{con Hoved : (int * Liste) -> Liste, con Tom : Liste}) con Hoved = fn : (int * Liste) -> Liste con Tom = Tom : Liste - val x = Hoved(1,Hoved(2,Hoved(3,Tom))); > val x = Hoved(1,Hoved(2,Hoved(3,Tom))): Liste - fun len Tom = 0 | len (Hoved(x,xs)) = 1+ len xs; > val len = fn : Liste -> int - len x; > val it = 3 : int

13 Træer - datatype 'a trae = blad of 'a | gren of ('a trae * 'a trae ); > New type names: =trae datatype 'a trae = ('a trae, {con 'a blad : 'a -> 'a trae, con 'a gren : ('a trae * 'a trae) -> 'a trae}) con 'a blad = fn : 'a -> 'a trae con 'a gren = fn : ('a trae * 'a trae) -> 'a trae - val t = gren(gren(blad 1, blad 2), gren(blad 3, blad 4)); > val t = gren(gren(blad 1, blad 2), gren(blad 3, blad 4)) : int trae

14 Funktion på træer - fun flad (blad(b)) = [b] | flad (gren(t1,t2)) = (flad (flad t2); > val 'a flad = fn : 'a trae -> 'a list - flad t; > val it = [1, 2, 3, 4] : int list

15 Options datatype 'a option = NONE | SOME of 'a; Bruges man måske ikke får en værdi - fun last [] = NONE | last [x] = SOME x | last (x::xs) = last xs; > val 'a last = fn : 'a list -> 'a option - last [1,2,3]; > val it = SOME 3 : int option

16 Undtagelser - exception TomListe; > exn TomListe = TomListe : exn - fun last [] = raise TomListe | last [x] = x | last (x::xs) = last xs; > val 'a last = fn : 'a list -> 'a - last []; ! Uncaught exception: ! TomListe

17 Fange undtagelser - fun trylast ls = last ls handle TomListe => 0; > val trylast = fn : int list -> int - trylast []; > val it = 0 : int

18 Fange undtagelser - fun f x = if x = 0 then x div 0 else if x = 1 then raise TomListe else x; > val f = fn : int -> int - fun g x = f x handle TomListe => 17 | x => 18; > val g = fn : int -> int - g 0; > val it = 18 : int - g 1; > val it = 17 : int - g 2; > val it = 2 : int

19 Opgaver 7.2: lav funktion min: int list -> int option der finder mindste tal i en liste 9.3 lav funktion rod: (int -> int) -> int så rod f finder mindste tal n (n>0) så f(n) = 0; 9.4, 9.5


Laste ned ppt "Paradigmer i Programmering 3. Højere ordens funktioner Idag: Højere ordens funktioner Algebraiske datatyper Næste gang: I/O, Filer, interaktive programmer."

Liknende presentasjoner


Annonser fra Google