Online Resource. Online introductory book on programming in SML (Standard ML): online.pdf

Similar documents
Functional Programming

SML A F unctional Functional Language Language Lecture 19

Chapter 15. Functional Programming Languages

CPS 506 Comparative Programming Languages. Programming Language Paradigm

CS109A ML Notes for the Week of 1/16/96. Using ML. ML can be used as an interactive language. We. shall use a version running under UNIX, called

Example Scheme Function: equal

A Second Look At ML. Chapter Seven Modern Programming Languages, 2nd ed. 1

A quick introduction to SML

Redefinition of an identifier is OK, but this is redefinition not assignment; Thus

A Brief Introduction to Standard ML

Note that pcall can be implemented using futures. That is, instead of. we can use

Processadors de Llenguatge II. Functional Paradigm. Pratt A.7 Robert Harper s SML tutorial (Sec II)

A First Look at ML. Chapter Five Modern Programming Languages, 2nd ed. 1

OCaml. ML Flow. Complex types: Lists. Complex types: Lists. The PL for the discerning hacker. All elements must have same type.

Scheme. Functional Programming. Lambda Calculus. CSC 4101: Programming Languages 1. Textbook, Sections , 13.7

If we have a call. Now consider fastmap, a version of map that uses futures: Now look at the call. That is, instead of

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

Variables and Bindings

Functional Programming. Pure Functional Programming

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

Tail Recursion: Factorial. Begin at the beginning. How does it execute? Tail recursion. Tail recursive factorial. Tail recursive factorial

Begin at the beginning

Mini-ML. CS 502 Lecture 2 8/28/08

Fall Lecture 3 September 4. Stephen Brookes

Standard ML. Curried Functions. ML Curried Functions.1

SCHEME 10 COMPUTER SCIENCE 61A. July 26, Warm Up: Conditional Expressions. 1. What does Scheme print? scm> (if (or #t (/ 1 0)) 1 (/ 1 0))

A list is a finite sequence of elements. Elements may appear more than once

Functional Programming. Pure Functional Programming

Handout 2 August 25, 2008

Chapter 3 Linear Structures: Lists

CIS24 Project #3. Student Name: Chun Chung Cheung Course Section: SA Date: 4/28/2003 Professor: Kopec. Subject: Functional Programming Language (ML)

All the operations used in the expression are over integers. a takes a pair as argument. (a pair is also a tuple, or more specifically, a 2-tuple)

Side note: Tail Recursion. Begin at the beginning. Side note: Tail Recursion. Base Types. Base Type: int. Base Type: int

Data Types The ML Type System

Chapter 3 Linear Structures: Lists

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

LECTURE 16. Functional Programming

Functional Programming Languages (FPL)

Exercises on ML. Programming Languages. Chanseok Oh

Chapter 15. Functional Programming Languages ISBN

Dialects of ML. CMSC 330: Organization of Programming Languages. Dialects of ML (cont.) Features of ML. Functional Languages. Features of ML (cont.

Homework 3 COSE212, Fall 2018

PROGRAMMING IN HASKELL. CS Chapter 6 - Recursive Functions

Background. CMSC 330: Organization of Programming Languages. Useful Information on OCaml language. Dialects of ML. ML (Meta Language) Standard ML

Recap from last time. Programming Languages. CSE 130 : Fall Lecture 3: Data Types. Put it together: a filter function

Specification and Verification in Higher Order Logic

News. Programming Languages. Recap. Recap: Environments. Functions. of functions: Closures. CSE 130 : Fall Lecture 5: Functions and Datatypes

Programming Languages

Plan (next 4 weeks) 1. Fast forward. 2. Rewind. 3. Slow motion. Rapid introduction to what s in OCaml. Go over the pieces individually

An introduction to Scheme

CSE 341, Autumn 2005, Assignment 3 ML - MiniML Interpreter

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

Programming Languages

Programming Languages

Fall 2017 Discussion 7: October 25, 2017 Solutions. 1 Introduction. 2 Primitives

Spring 2018 Discussion 7: March 21, Introduction. 2 Primitives

MIDTERM EXAMINATION - CS130 - Spring 2005

Recursion defining an object (or function, algorithm, etc.) in terms of itself. Recursion can be used to define sequences

Patterns The Essence of Functional Programming

CSc 372, Fall 1996 Mid-Term Examination Monday, October 21, 1996 READ THIS FIRST

Functional programming with Common Lisp

Fall 2018 Discussion 8: October 24, 2018 Solutions. 1 Introduction. 2 Primitives

News. Programming Languages. Complex types: Lists. Recap: ML s Holy Trinity. CSE 130: Spring 2012

(Func&onal (Programming (in (Scheme)))) Jianguo Lu

So what does studying PL buy me?

Computer Science CSC324 Wednesday February 13, Homework Assignment #3 Due: Thursday February 28, 2013, by 10 p.m.

Sample Final Exam Questions

CSCI-GA Scripting Languages

Common LISP Tutorial 1 (Basic)

CSCI-GA Final Exam

Tuples. CMSC 330: Organization of Programming Languages. Examples With Tuples. Another Example

Principles of Programming Languages 2017W, Functional Programming

Chapter 15. Functional Programming Languages

Higher-Order Functions

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

CS 11 Ocaml track: lecture 2

F28PL1 Programming Languages. Lecture 14: Standard ML 4

An anecdote about ML type inference

An Explicit Continuation Evaluator for Scheme

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

Shell CSCE 314 TAMU. Functions continued

OCaml. History, Variants. ML s holy trinity. Interacting with ML. Base type: Integers. Base type: Strings. *Notes from Sorin Lerner at UCSD*

CSE 341 Sample Midterm #2

Recursion defining an object (or function, algorithm, etc.) in terms of itself. Recursion can be used to define sequences

CSE341, Fall 2011, Midterm Examination October 31, 2011

Lists. Michael P. Fourman. February 2, 2010

Programming Languages

Datatype declarations

CS Lectures 2-3. Introduction to OCaml. Polyvios Pratikakis

# true;; - : bool = true. # false;; - : bool = false 9/10/ // = {s (5, "hi", 3.2), c 4, a 1, b 5} 9/10/2017 4

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

SML. CSE 307 Principles of Programming Languages Stony Brook University

Currying fun f x y = expression;

15. Functional Programming

Lecture #13: Type Inference and Unification. Typing In the Language ML. Type Inference. Doing Type Inference

Combining Static and Dynamic Contract Checking for Curry

CMSC 330: Organization of Programming Languages. Functional Programming with Lists

Chapter 3 Programming with Recursion

Recap: Functions as first-class values

Lab 10: OCaml sequences, comparators, stable sorting 12:00 PM, Nov 12, 2017

Transcription:

ML

Online Resource Online introductory book on programming in SML (Standard ML): http://www.cs.cmu.edu/~rwh/smlbook/ online.pdf

Running SML SML is a strongly typed language - all (well-formed) expressions have a type that can be determined by examing the expression. Connect to access.cims.nyu.edu using putty At the prompt, type sml (all lower case) to start the SML program. SML will type "-" as its prompt. To exit sml, type ctrl-d Type an expression, ending it with a semicolon and <enter>. SML will interpret it, name the result "it", and print out its value and type as: val it = <value> : <type> For example, type: -2+2; val it = 4 : int * The expression evaluated to 4, and this value is bound to the name it, and is of type int

Compiling and Running SML Create an SML program file: hello.sml At the prompt, compile the program using mlton: mlton hello.sml An executable file will be generated. Run the executable similar to a C program:./hello

Basic Expressions Mathematical expressions are infix compared to Scheme, which are prefix. Basic Arithmetic operators: +, -, *, /. NOTE: the operator, / is applied to reals (an error will occur if you don t use reals). For integers, for example, you should use div : - 2 div 3; val it = 0 : int Negative numbers are represented using ~ : - 2 + (~4); val it = ~2 : int

Define & Call Functions To define a function: - fun add1 x = x + 1; val add1 = fn : int -> int To call a function: - add1 4; val it = 5 : int

Hello World Function: print(string): - print("hello World from ML!\n"); Hello World from ML! val it = () : unit

Types Basic Types: Integers, reals: 1, 0.1 Strings: Hello Lists: Must be homogenous (i.e., all of one type). Length is not limited. val a = [1,2,3]; or val b = [ a, b, c ]; Cannot have: [1, a,3]; Empty lists are denoted by: [] or nil Tuples Can combine types. Length is limited. val x = (1,2,3); // cannot add to the end of x val y = (1,0.1, a ); // int*real*string

Operations on Lists hd (similar to car in Scheme): - val x = [1,2,3,4,5,6]; val x = [1,2,3,4,5,6] : int list - hd x; val it = 1 : int tl (similar to cdr in Scheme): - tl x; val it = [2,3,4,5,6] : int list @ adds elements to the front of the list: - [7,8]@x; val it = [7,8,1,2,3,4,5,6] : int list

Operations on Lists The "cons" operator (for "construct"): combines an element and a list to make a larger list. The element becomes the head of the resulting list. The list which is given as the second argument to cons becomes the tail of the new list. The cons operator is written with two colons, as "::". - 4::[3,2,6,7]; val it = [4,3,2,6,7] : int list

More Operations on Lists Explode: Explodes a string into a list of characters - explode("hello"); val it = [#"h",#"e",#"l",#"l",#"o"] : char list Implode: Form a string from a list of characters - implode [#"h",#"e",#"l",#"l",#"o"]; val it = "hello" : string

Functions with List Computes the length of a list - fun length L = if null(l) then 0 else length (tl(l))+1; val length = fn : 'a list -> int - val z = [1,2,3,4,5,6,7,8,9,0]; val z = [1,2,3,4,5,6,7,8,9,0] : int list - length z; val it = 10 : int

Recursion Fibonacci numbers Definition: F n = F n-1 + F n-2 Seed Values: F 0 = 0, F 1 = 1 Sequence: 0,1,1,2,3,5,8,13,21,34,55,89,144, Program: - fun fib x = if (x=1) then x else if (x=0) then x else fib(x-1)+fib(x-2); - fib 0; val it = 0 : int - fib 1; val it = 1 : int - fib 2; val it = 1 : int - fib 3; val it = 2 : int - fib 4; val it = 3 : int - fib 5; val it = 5 : int - fib 6; val it = 8 : int

Recursion (cont d) Factorial Example: 5! = 5*4*3*2*1 = 120 Program: - fun fac n = if (n=1) then 1 else n*fac(n-1); val fac = fn : int -> int -fac 5; val it = 120 : int

Merge Sort To sort the list: First split the list, L, into two disjoint sublists. Then, recursively sort the sublists. Finally, merge the sorted sublists. Needed functions: Split a list into two sublists. Merge the two sublists into one sorted list.

Merge Sort (cont d) Implementing firstk : returns the list of the first k elements of L by recursively calling firstk and using the con function to construct the first element with up to k elements: - fun firstk(l, k) = if k = 0 then [] else if L = nil then nil else hd(l)::firstk(tl(l),k-1); Testing firstk - val L = [4,2,6,4,7,1,8]; val Left = [4,2] : int list

Merge Sort (cont d) Implementing dropk : returns the list of all but the first k elements of L by recursively calling dropk and using the tl function to return all but the first element up to k element: - fun dropk(l,k) = if k = 0 then L else if L = nil then nil else dropk(tl(l),k-1); Testing dropk - val Right = dropk(l,3); val Right = [4,7,1,8] : int list

Merge Sort (cont d) Implementing merge : - fun merge ([], M) = M merge (L, []) = L merge (x::x1, y::y1) = if (x:int) < y then x::merge(x1, y::y1) else y::merge(x::x1, y1); Testing merge: - merge([1,5,7,9],[2,3,5,5,10]); val it = [1,2,3,5,5,5,7,9,10] : int list Notes: is used for case statements The merge function is doing the actual sorting.

Merge Sort (cont d) Implementing newsort : - fun newsort(l) = if L = nil then nil else if tl(l) = nil then L else merge(newsort(firstk(l,length(l) div 2)), newsort(dropk(l,length(l) div 2))); Let k = length(l) div 2, then we obtain the left and right halves of L. Calling merge passing a sorted sublist. The sublist is obtained by calling firstk and dropk passing the list and the length of the list divided by 2 as k.

Merge Sort (cont d) Running the Merge Sort code: - val L1 = [5,2,6,8,2,9,8,4,0]; val L1 = [5,2,6,8,2,9,8,4,0] : int list - val Left = firstk(l1,2); val Left = [5,2] : int list - val Right = dropk(l1,2); val Right = [6,8,2,9,8,4,0] : int list - val Left_s = newsort(left); val Left_s = [2,5] : int list - val Right_s = newsort(right); val Right_s = [0,2,4,6,8,8,9] : int list - merge(left_s, Right_s); val it = [0,2,2,4,5,6,8,8,9] : int list - newsort(l1); val it = [0,2,2,4,5,6,8,8,9] : int list