190 likes | 303 Vues
Paradigmer i Programmering 3. PIP. Højere ordens funktioner. Idag: Højere ordens funktioner Algebraiske datatyper Næste gang: I/O, Filer, interaktive programmer. Flere parametre. - fun add(x,y) = x+y; > val add = fn : int * int -> int - add(2,3); > val it = 5 : int
E N D
Højere ordens funktioner Idag: Højere ordens funktioner Algebraiske datatyper Næste gang: I/O, Filer, interaktive programmer
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
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
Eksempel - fun upString x = implode (map Char.toUpper (explode x)); > val upString = fn : string -> string - upString "hejsahj+986543"; > val it = "HEJSAHJ+986543" : string
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
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
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
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
Algebraiske datatyper I mængdelæren: • N M i SML: tupler • Nm i SML: lister • N + M i SML: datatyper N + M er stort set N M, men man kan se om en værdi kommer fra den ene eller anden.
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 -
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
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
Funktion på træer - fun flad (blad(b)) = [b] | flad (gren(t1,t2)) = (flad t1) @ (flad t2); > val 'a flad = fn : 'a trae -> 'a list - flad t; > val it = [1, 2, 3, 4] : int list
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
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
Fange undtagelser - fun trylast ls = last ls handle TomListe => 0; > val trylast = fn : int list -> int - trylast []; > val it = 0 : int
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
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