Download præsentationen
Præsentation er lastning. Vent venligst
Offentliggjort afLine Dideriksen Redigeret for ca. et år siden
1
Paradigmer i Programmering 4
2
IO + oversigt over udtryk
3
Udtryk konstanter parametre operationer og funktionskald (,, ) [,, ] raise handle if then else ( ; ; ) let ; ;.. in end case of fn
4
Operationer andalso, orelse (sprogkonstruktioner) + - * div mod = <> >= > <= < :: @ ^ o before f.eks (3 before print ”hej”) udskriv ”hej” og returner 3
5
, match ::= mrule { ”|” mrule } mrule ::= pat ”=>” exp dec ::= ”val” pat ”=” exp { ”and” pat ”=” exp} | ”fun” nm pat-list = exp |... ”and”... | ”datatype”.. | ”exception”.. | ”local” dec ”in” dec ”end” | ”infix”...
6
case udtryk - fun myapp [] ys = ys | myapp (x::xs) ys = x::(myapp xs ys); > val 'a myapp = fn : 'a list -> 'a list -> 'a list - fun myapp xl yl = case xl of [] => yl | (x::xs) => x::(myapp xs yl); > val 'a myapp = fn : 'a list -> 'a list -> 'a list
7
Læse en fil fun readfile1 is = if TextIO.endOfStream is then (TextIO.closeIn is; []) else (TextIO.inputLine is)::(readfile1 is); fun readfile nm = readfile1 (TextIO.openIn nm); val lst = readfile "tst.sml"; quit();
8
Resultat Moscow ML version 2.00 (June 2000) Enter `quit();' to quit. [opening file "C:\mads\undvs\pip-f02\tst.sml"] > val readfile1 = fn : instream -> string list > val readfile = fn : string -> string list > val lst = ["fun readfile1 is = \n", " if TextIO.endOfStream is \n", " then (TextIO.closeIn is; [])\n", " else (TextIO.inputLine is)::(readfile1 is);\n", "fun readfile nm = \n", " readfile1 (TextIO.openIn nm);\n", "\n", "val fil=readfile \"tst.sml\";\n", "quit();\n", "\n", "\n"] : string list
9
Skrive til en fil fun writefile1 os [] = TextIO.closeOut os | writefile1 os (x::xs) = (TextIO.output(os,x) ; writefile1 os xs); fun writefile nm lst = writefile1 (TextIO.openOut nm) lst; writefile "tst1.sml" lst;
10
printVal i fortolkeren - val x = ([1,2,3],"hej",true); > val x = ([1, 2, 3], "hej", true) : int list * string * bool - printVal x; ([1, 2, 3], "hej", true) > val it = ([1, 2, 3], "hej", true) : int list * string * bool printVal kan bruges for alle typer, men kan kun bruges i fortolkeren - ikke i et oversat program
11
Dialog fun rl() = TextIO.inputLine(TextIO.stdIn); fun wl x = (TextIO.output(TextIO.stdOut,x); TextIO.flushOut(TextIO.stdOut)); fun readtoblank () = let val ln = rl() in if ln="\n" then [ln] else ( wl ("læste : "^ln); ln::(readtoblank()) ) end; val x = readtoblank(); val y = quit();
12
Lidt mere generelt fun processtoexit f state = let val ln = (wl ”- ”;rl()) in if ln="exit\n" then state else processtoexit f (f ln state) end; fun ff ln state = state@[ln]; val x = processtoexit ff []; ff : tekst -> tilstand -> tilstand hver linie inddata giver programmet en ny tilstand
13
Typer > val rl = fn : unit -> string > val wl = fn : string -> unit > val readtoblank = fn : unit -> string list > val 'a processtoexit = fn : (string -> 'a -> 'a) -> 'a -> 'a > val 'a ff = fn : 'a -> 'a list -> 'a list
14
mosmlc tst.sml C:\mads\undvs\pip-f02>mosmlc tst.sml C:\mads\undvs\pip-f02>mosmlout - 1 - 2 - 3 - ghs - exit C:\mads\undvs\pip-f02>
15
Referencer val = ref lav reference til værdi ! hent værdi fra reference := ændr værdi af reference
16
Referencer - val x = ref [1]; > val x = ref [1] : int list ref - x := 2::(!x); > val it = () : unit - x; > val it = ref [2, 1] : int list ref - x := 3::(!x); > val it = () : unit - x; > val it = ref [3, 2, 1] : int list ref
Lignende præsentationer
© 2024 SlidePlayer.dk Inc.
All rights reserved.