Sml-Intro-Solns 4Up.Pdf

Sml-Intro-Solns 4Up.Pdf

The ML Programming Language ML (Meta Language) was developed By RoBin Milner in 1975 for specifying theorem provers. It since has evolved into a general purpose programming language. Introduc)on To Standard ML Important features of ML: • sta?c typing: catches type errors at compile-)me. • type reconstruc?on: infers types so programmers don’t have to SOLUTIONS write them explicitly • polymorphism: func)ons and values can Be parameterized over types (think Java generics, But much BeRer). CS251 Programming Languages • func?on-oriented (func?onal): encourages a composi)on-Based Spring 2019 style of programming and first-class func)ons Lyn Turbak • sum-of-products dataypes with paIern-matching: simplifies the manipula)on of tree-structured data Department of Computer Science These features make ML an excellent language for mathema)cal Wellesley College calcula)on, data structure implementa)on, and programming language implementa)on (= metaprogramming). Introduction to Standard ML 2 Two ways to run sml ML Dialects There are several different dialects of ML. The two we use at Wellesley are: Way #1: Run sml on the csenv or wx Virtual Box appliances from CS240 (see following slides). • Standard ML (SML): Version developed at AT&T Bell Labs. We’ll use this in CS251. The par)cular implementa)on we’ll use is Way #2: Run sml within a terminal window on the new CS server, cs.wellesley.edu. Standard ML of New Jersey (SMLNJ): (It is no longer necessary to used the old server, old-tempest.wellesley.edu.) • Begin By connec)ng to your CS server account via ssh. hRp://www.smlnj.org/ • On a Mac, you can do this in your terminal window. • On a Windows PC, you’ll need to use a terminal emulator like puRy • Objec?ve CAML: Version developed at INRIA (France). We have [fturbak@Franklyns-MBP ~]$ ssh [email protected] some)mes used this in other Wellesley courses. [email protected]'s password: Last login: Sun Mar 31 22:19:28 2019 from … These dialects differ in minor ways (e.g., syntac)c conven)ons, liBrary This is the new virtual server running CentOS 7 func)ons). See the following for a comparison: New CentOS 7 [gdome@tempest ~]$ which sml /usr/local/smlnj/bin/sml hRp://www.mpi-sws.mpg.de/~rossBerg/sml-vs-ocaml.html New CentOS 7 [gdome@tempest ~]$ sml Standard ML of New Jersey v110.85 [built: Tue Mar 26 16:24:43 2019] - 1 + 2; Introduction to Standard ML 3 val it = 3 : int Introduction to Standard ML 4 Two ways to run sml SML and csenv/wx Way #1: Run sml on the csenv or wx Virtual Box appliances from CS240 (see following slides). Way #2: Run sml within a terminal window the new CS server, cs.wellesley.edu. (It is no longer necessary to used the old server, old-tempest.wellesley.edu.) • Begin By connec)ng to your CS server account via ssh. • On a Mac, you can do this in your terminal window. • On a Windows PC, you’ll need to use a terminal emulator like puRy [fturbak@Franklyns-MBP ~]$ ssh [email protected] [email protected]'s password: Last login: Sun Mar 31 22:19:28 2019 from … This is the new virtual server running CentOS 7 New CentOS 7 [gdome@tempest ~]$ which sml We will use SML inside the csenv Virtual Machine appliance. Details on how to install /usr/local/smlnj/bin/sml csenv and install SML within csenv are available on the CS251 schedule page. New CentOS 7 [gdome@tempest ~]$ sml For ini)al examples, it’s easiest to run SML in a terminal window, as shown above. Standard ML of New Jersey v110.85 [built: Tue Mar 26 16:24:43 2019] But we’ll soon see (slides 19-21) running it in Emacs is much beRer! - 1 + 2; val it = 3 : int Introduction to Standard ML 5 Introduction to Standard ML 6 Learning SML By Interac)ve Examples Naming Values Solu)ons Try out these examples. (Note: many answers are missing in these slides so you can predict them. See the solu;on slides for answers.) - val a = 2 + 3; val a = 5 : int [wx@wx ~] sml Standard ML of New Jersey v110.78 [built: Wed Jan 14 12:52:09 2015] - 1 + 2; - a * a; val it = 3 : int val it = 25 : int - 3+4; val it = 7 : int - it + a; - 5+6 val it = 30 : int = ; val it = 11 : int - 7 = + = 8; val it = 15 : int Introduction to Standard ML 7 Introduction to Standard ML 8 Nega)ve Quirks Division Quirks - 2 - 5; - 7 / 2; val it = ~3 : int stdIn:1.1-1.6 Error: operator and operand don't agree [literal] - -17; operator domain: real * real stdIn:60.1 Error: expression or pattern begins with infix identifier "-" operand: int * int stdIn:60.1-60.4 Error: operator and operand don't agree in expression: [literal] 7 / 2 operator domain: 'Z * 'Z operand: int - 7.0 / 2.0; in expression: val it = 3.5 : real - 17 - 7 div 2; (* integer division *) - ~17; val it = 3 : int val it = ~17 : int (* For a description of all top-level operators, see: - 3 * ~1; http://www.standardml.org/Basis/top-level-chapter.html *) val it = ~3 : int Introduction to Standard ML 9 Introduction to Standard ML 10 When Parentheses MaRer Simple Func)ons Solu)ons - dbl(5); (* parens are optional here *) val it = 10 : int - val inc = fn x => x + 1; val inc = fn : int -> int (* SML figures out type! *) - (dbl 5); (* parens are optional here *) val it = 10 : int - inc a; - inc (dbl 5); (* parens for argument subexpressions are required! *) val it = 6 : int val it = 11 : int - fun dbl y = y * 2; - (inc dbl) 5; stdIn:1.2-2.2 Error: operator and operand don't agree [tycon mismatch] (* Syntactic sugar for val dbl = fn y => y * 2 *) operator domain: int val dbl = fn : int -> int operand: int -> int in expression: - dbl 5; inc dbl val it = 10 : int - inc dbl 5; (* default left associativity for application *) stdIn:22.1-22.10 Error: operator and operand don't agree [tycon - (fn x => x * 3) 10; (* Dont need to name function mismatch] to use it *) operator domain: int operand: int -> int val it = 30 : int in expression: inc dbl Introduction to Standard ML 11 Introduction to Standard ML 12 Booleans Solu)ons Condi)onals Solu)ons - 1 = 1; - fun f n = if n > 10 then 2 * n else n * n; val it = true : bool val f = fn : int -> int - 1 > 2; val it = false : bool - f 20; - (1 = 1) andalso (1 > 2); val it = 40 : int val it = false : bool - (1 = 1) orelse (1 = 2); - f 5; val it = true : bool val it = 25 : int - (3 = 4) andalso (5 = (6 div 0)); (* short-circuit evaluation *) val it = false : bool - fun isEven n = (n mod 2) = 0; val isEven = fn : int -> bool (* SML figures out type! *) - isEven 17; val it = false : bool - isEven 6; val it = true : bool Introduction to Standard ML 13 Introduction to Standard ML 14 Recursion Solu)ons Easier to Put Your Code in a File (* This is the contents of the file - fun fact n = ~cs251/download/sml/mydefns.sml ‘ = if n = 0 then (* By the way, comments nest properly in SML! *) = 1 It defines integers a and b and functions named sq, hyp, and fact *) = else val a = 2 + 3 = n * (fact (n - 1)); (* fun names have recursive scope *) val fact = fn : int -> int val b = 2 * a (* simpler than Java definition b/c no explicit types! *) fun sq n = n * n (* squaring function *) - fact 5; (* calculate hypotenuse of right triangle with sides a and b *) val it = 120 : int fun hyp a b = Math.sqrt(Real.fromInt(sq a + sq b)) fun fact n = (* a recursive factorial function *) - fact 12; if n = 0 then val it = 479001600 : int 1 else n * (fact (n - 1)) - fact 13; uncaught exception Overflow [overflow] • File is a sequence of value/func)on defini)ons. raised at: <file stdIn> (* SML ints have limited size L *) • Defini)ons are not followed by semi-colons in files! • There are no con?nua?on characters (equal signs) for mul)ple-line defini)ons. Introduction to Standard ML 15 - Introduction to Standard ML 16 Using Code From a File Another File Example - Posix.FileSys.getcwd(); (* current working directory *) val it = "/students/gdome" : string (* This is the contents of the file test-fact.sml *) - Posix.FileSys.chdir("/students/gdome/cs251/sml"); (* change working directory *) val fact_3 = fact 3 val it = () : unit val fact_a = fact a - Posix.FileSys.getcwd(); val it = "/students/gdome/cs251/sml" : string - use "test-fact.sml"; - use "mydefns.sml"; (* load defns from file as if *) [opening test-fact.sml] [opening mydefns.sml] (* they were typed manually *) val fact_3 = 6 : int val a = 5 : int val b = 10 : int val fact_a = 120 : int val sq = fn : int -> int val it = () : unit val hyp = fn : int -> int -> real val fact = fn : int -> intval it = () : unit - fact a val it = 120 : int Introduction to Standard ML 17 Introduction to Standard ML 18 Use Emacs within csenv/wx for all your SML edi)ng/tes)ng Nested File Uses Launch Emacs by clicking on the icon, or executing emacs & ! (to create a new Emacs window) or emacs –nw (to run Emacs directly in the shell..) (* The contents of the file load-fact.sml *) use "mydefns.sml"; (* semi-colons are required here *) use “test-fact.sml"; Emacs editor buffer in - use "load-fact.sml"; SML mode. [opening load-fact.sml] Edit your [opening mydefns.sml] SML code here.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    11 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us