1 / 29

Programming Language Concepts (CIS 635)

Programming Language Concepts (CIS 635). Elsa L Gunter 4303 GITC NJIT, www.cs.njit.edu/~elsa/635. SML. Compiler is on the AFS system at /usr/local/sml/bin/sml A (possibly better, non-PowerPoint) text version of this lecture can be found at www.cs.njit.edu/~elsa/635/SML_Lect/sml-shell

rossa
Télécharger la présentation

Programming Language Concepts (CIS 635)

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. Programming Language Concepts (CIS 635) Elsa L Gunter 4303 GITC NJIT, www.cs.njit.edu/~elsa/635

  2. SML • Compiler is on the AFS system at /usr/local/sml/bin/sml • A (possibly better, non-PowerPoint) text version of this lecture can be found at www.cs.njit.edu/~elsa/635/SML_Lect/sml-shell • For the SML code for today’s lecture see www.cs.njit.edu/~elsa/635/SML_Lect/intro.sml

  3. WWW Addresses for SML • http://www.cs.njit.edu/~elsa/635/110-smlnj.exe • ftp://ftp.research.bell-labs.com/dist/smlnj/release/110/110-smlnj.exe • http://cm.bell-labs.com/cm/cs/what/smlnj/index.html • http://cm.bell-labs.com/cm/cs/what/smlnj/doc/basis/pages/sml-std-basis.html

  4. Books on SML • Supplemental texts (not required) • Elements of ML Programming, by Jeffrey D. Ullman, on Prentice Hall • ML for the Working Programmer, by Lawrence C. Paulson, on Cambridge University Press

  5. Session in SML Standard ML of New Jersey, Version 110.0.7, September 28, 2000 [CM&CMB] - (* Read-eval-print loop; expressions and declarations *) - 2 + 3; val it = 5 : int - val test = 3 < 2; val test = false : bool

  6. SML Expressions and Declarations - (* = At top-level, an expression = <exp>; = is treated as an abbreviation for the declaration = val it = <exp>; =*) - "Hi there"; val it = "Hi there" : string

  7. SML Top-level Expressions and Declarations - (* ^ is string concatenation *) - it ^ " my good friend"; val it = "Hi there my good friend" : string - (* () pronounced "unit" is the result for functions that don’t have a result *) - print "Hello world\n"; Hello world val it = () : unit

  8. Overloading for Basic Arithmetic - val x = 5 + 7; val x = 12 : int - val y = x * 2; val y = 24 : int - val z = 1.35 + 0.23; val z = 1.58 : real

  9. Overloading but No Coercion - val w = y + z; stdIn:51.1-51.14 Error: operator and operand don't agree [tycon mismatch] operator domain: int * int operand: int * real in expression: y + z

  10. Using SML Code From a File • File named test.sml contains 3 + 2; val x = 5 + 7; val y = x * 2; val z = 1.35 + 0.23; val w = y + z;

  11. Compiler Output - use "A:\\SML_Lect\\test.sml"; [opening A:\SML_Lect\test.sml] val it = 5 : int val x = 12 : int val y = 24 : int val z = 1.58 : real

  12. Compiler Output A:\SML_Lect\test.sml:5.1-5.14 Error: operator and operand don't agree [tycon mismatch] operator domain: int * int operand: int * real in expression: y + z

  13. Booleans (aka Truth Values) - true; val it = true : bool - false; val it = false : bool - if y > x then 25 else 0; val it = 25 : int

  14. Booleans - 3 > 1 andalso 4 > 6; val it = false : bool - 3 > 1 orelse 4 > 6; val it = true : bool - not (4 > 6); val it = true : bool

  15. Functions - fun plus_two n = n + 2; val plus_two = fn : int -> int - plus_two 17; val it = 19 : int - val plus_two = fn n => n + 2; val plus_two = fn : int -> int - plus_two 14; val it = 16 : int

  16. Values fixed at declaration time - val x = 12; val x = 12 : int - fun plus_x y = y + x; val plus_x = fn : int -> int - plus_x 3; val it = 15 : int

  17. Values fixed at declaration time - val x = 7; val x = 7 : int - plus_x 3; val it = 15 : int

  18. Functions with more than one argument - fun add_three (x:int) y z = x + y + z; val add_three = fn : int -> int -> int -> int - val t = add_three 6 3 2; val t = 11 : int

  19. Partial application of functions - val h = add_three 5 4; val h = fn : int -> int - h 3; val it = 12 : int - h 7; val it = 16 : int

  20. Functions as arguments - fun thrice f x = f (f (f x)); val thrice = fn : ('a -> 'a) -> 'a -> 'a - thrice plus_two; val it = fn : int -> int - it 4; val it = 10 : int - thrice (fn s => "Hi! " ^ s) "Good-bye!"; val it = "Hi! Hi! Hi! Good-bye!" : string

  21. Recursive Functions - fun factorial 0 = 1 = | factorial n = n * factorial (n - 1); val factorial = fn : int -> int - factorial 5; val it = 120 : int - - (* fun is needed for recursion function declarations *)

  22. Tuples - val s = (5,"hi",3.2); val s = (5,"hi",3.2) : int * string * real - val (a,b,c) = s; val a = 5 : int val b = "hi" : string val c = 3.2 : real

  23. Tuples - val d = ((1,4,62),("bye",15),73.95); val d = ((1,4,62),("bye",15),73.95) : (int * int * int) * (string * int) * real - val (p,(st,_),_) = d; val p = (1,4,6) : int * int * int val st = "bye" : string

  24. Tuples - fun fst_of_3 (x,_,_) = x; val fst_of_3 = fn : 'a * 'b * 'c -> 'a - s; val it = (5,"hi",3.2) : int * string * real - fst_of_3 s; val it = 5 : int - fst_of_3 d; val it = (1,4,62) : int * int * int

  25. Records - val teacher = {Name = "Elsa L. Gunter", ss = (119,73,6244), age = 102}; val teacher = {Name="Elsa L. Gunter",age=102,ss=(119,73,6244)} : {Name:string, age:int, ss:int * int * int}

  26. Records - val {ss = (s1,s2,s3), Name = elsa, age = years} = teacher; val elsa = "Elsa L. Gunter" : string val years = 102 : int val s1 = 119 : int val s2 = 73 : int val s3 = 6244 : int

  27. Records and Tuples - val q = (280,{student = {Name = "Joseph Martins", = ss = (325,40,1276), = age = 19}, = instructor = teacher});

  28. Records and Tuples val q = (280, {instructor={Name="Elsa L. Gunter",age=102,ss=(119,73,6244)}, student={Name="Joseph Martins",age=19,ss=(325,40,1276)}}) : int * {instructor:{Name:string, age:int, ss:int * int * int}, student:{Name:string, age:int, ss:int * int * int}}

  29. Tuples are Records - val strange = (1,"f",2) = {3 = 2, 2 = "f", 1 = 1};; val strange = true : bool

More Related