{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Lecture4.6 - DPL-4 Introduction to ML Material in these...

Info icon This preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
1 10/2/06 From slides by A. Dimock (Copyright) 1 DPL-4: Introduction to ML Material in these notes is adapted from materials by N. Ramsey, J. Ullman and A. Dimock. It should be considered as copyright by all of them. 10/2/06 From slides by A. Dimock (Copyright) 2 DPL-4: ML ML: designed for symbolic computation (including supporting the design of other languages: ML = Meta Language - initially, for a verification system). Observation: μ Scheme interpreter < 20% the size of C never an unexplained core dump and the interpreter rarely halts unexpectedly it thus provides lots of savings in error checking We want to extend this to functional programming, but detect errors as soon as possible (preferably at compile time!) We aim for: Scheme + pattern matching + static typing + exceptions. 10/2/06 From slides by A. Dimock (Copyright) 3 DPL-4: What's new with ML Real language means complexity: Core language: Pucella in 25 pages; Harper in 130 pages; Ullman in 250 pages. Language definition: Milne, Tofte, Harper in 101 pages. Main areas of novelty: new syntax programming by pattern matching exceptions static polymorphic type inference Plus : very strong support for Abstract Data Types For now, we ignore "programming in the large". We'll be consumers of library routines, but not (yet) producers of modules. 10/2/06 From slides by A. Dimock (Copyright) 4 DPL-4: Starting ML One option: (on mercury.cs.uml.edu) $ mosml -P full - load " IO " ; > val it = () : unit - ~3 + 5; > val it = 2 : int - quit(); (or ^D) Another option: $ sml /* download from www.smlnj.org */ - ^D 10/2/06 From slides by A. Dimock (Copyright) 5 DPL-4: Basic values and expressions The arithmetic is "as usual" (infix), except that negative numbers are represented at input with ~ rather than - (~ is a unary operator, - is a binary one, and ML is a bit fussy, all in a good cause). - ~3 + 3; > val it = 0 : int ground types: int, real, string, char, unit, bool string concatenation with infix "hat": - "this"^" and that"; > val it = "this and that" : string true boolean types bool(true, false) - 3 < 4; > val it = true : bool 10/2/06 From slides by A. Dimock (Copyright) 6 DPL-4: Basic values and expressions, II Short circuit (= stop as soon as satisfied) andalso , orelse - (3 < 4) andalso (4 < 5); > val it = true : bool strings "as in C"; characters #"a", #"b", #"c", etc.; ' is "prime" or "tick mark" and is used in type variables (polymorphism??…) the language itself is completely expression based, like Scheme: if a then b else c is the same as C's a?b:c . Ex. of string and print formatting: - "A\tB\tC\n1\t2\t3\n"; > val it = "A\tB\tC\n1\t2\t3\n" : string - print("A\tB\tC\n1\t2\t3\n"); A B C 1 2 3 > val it = () : unit
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
2 10/2/06 From slides by A. Dimock (Copyright) 7 DPL-4: some typing observations ML is strongly typed - it does type inference for you, and provides error messages for mistyping…. - if 1 < 2 then #"a" else #"b"; > val it = #"a" : char - if 1 < 2 then #"a" else "bc"; ! Toplevel input: ! if 1 < 2 then #"a" else "bc"; ! ^^^^ ! Type clash: expression of type ! string ! cannot have type ! char 10/2/06 From slides by A. Dimock (Copyright) 8 DPL-4: Typing observations, II Coercions: - 4 div 5; > val it = 0 : int - 4/5; ! Toplevel input: ! 4/5; (* 4.0/5 gives similar error *) ! ^ ! Type clash: expression of type
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern