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

Size: px
Start display at page:

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

## Transcription

1 ML

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

3 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

4 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

5 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 ~ : (~4); val it = ~2 : int

6 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

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

8 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

9 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 adds elements to the front of the list: - val it = [7,8,1,2,3,4,5,6] : int list

10 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

11 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

12 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

13 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

14 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

15 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.

16 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

17 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

18 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.

19 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.

20 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

### Functional Programming

Functional Programming Function evaluation is the basic concept for a programming paradigm that has been implemented in functional programming languages. The language ML ( Meta Language ) was originally

### SML A F unctional Functional Language Language Lecture 19

SML A Functional Language Lecture 19 Introduction to SML SML is a functional programming language and acronym for Standard d Meta Language. SML has basic data objects as expressions, functions and list

### CPS 506 Comparative Programming Languages. Programming Language Paradigm

CPS 506 Comparative Programming Languages Functional Programming Language Paradigm Topics Introduction Mathematical Functions Fundamentals of Functional Programming Languages The First Functional Programming

### 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

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 SML/NJ or \Standard ML of New Jersey." You can get SML/NJ by

### Example Scheme Function: equal

ICOM 4036 Programming Languages Functional Programming Languages Mathematical Functions Fundamentals of Functional Programming Languages The First Functional Programming Language: LISP Introduction to

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

A Second Look At ML Chapter Seven Modern Programming Languages, 2nd ed. 1 Outline Patterns Local variable definitions A sorting example Chapter Seven Modern Programming Languages, 2nd ed. 2 Two Patterns

### A quick introduction to SML

A quick introduction to SML CMSC 15300 April 9, 2004 1 Introduction Standard ML (SML) is a functional language (or higherorder language) and we will use it in this course to illustrate some of the important

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

Redefinition of an identifier is OK, but this is redefinition not assignment; Thus val x = 100; val x = (x=100); is fine; there is no type error even though the first x is an integer and then it is a boolean.

### A Brief Introduction to Standard ML

A Brief Introduction to Standard ML Specification and Verification with Higher-Order Logic Arnd Poetzsch-Heffter (Slides by Jens Brandt) Software Technology Group Fachbereich Informatik Technische Universität

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

Note that pcall can be implemented using futures. That is, instead of (pcall F X Y Z) we can use ((future F) (future X) (future Y) (future Z)) In fact the latter version is actually more parallel execution

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

Processadors de Llenguatge II Functional Paradigm Pratt A.7 Robert Harper s SML tutorial (Sec II) Rafael Ramirez Dep Tecnologia Universitat Pompeu Fabra Paradigm Shift Imperative Paradigm State Machine

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

A First Look at ML Chapter Five Modern Programming Languages, 2nd ed. 1 ML Meta Language One of the more popular functional languages (which, admittedly, isn t saying much) Edinburgh, 1974, Robin Milner

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

OCaml The PL for the discerning hacker. ML Flow Expressions (Syntax) Compile-time Static 1. Enter expression 2. ML infers a type Exec-time Dynamic Types 3. ML crunches expression down to a value 4. Value

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

Scheme Textbook, Sections 13.1 13.3, 13.7 1 Functional Programming Based on mathematical functions Take argument, return value Only function call, no assignment Functions are first-class values E.g., functions

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

If we have a call (map slow-function long-list where slow-function executes slowly and long-list is a large data structure, we can expect to wait quite a while for computation of the result list to complete.

### COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

COP4020 Programming Languages Functional Programming Prof. Robert van Engelen Overview What is functional programming? Historical origins of functional programming Functional programming today Concepts

### Variables and Bindings

Net: Variables Variables and Bindings Q: How to use variables in ML? Q: How to assign to a variable? # let = 2+2;; val : int = 4 let = e;; Bind the value of epression e to the variable Variables and Bindings

### Functional Programming. Pure Functional Programming

Functional Programming With examples in F# Pure Functional Programming Functional programming involves evaluating expressions rather than executing commands. Computation is largely performed by applying

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

CS 6A Scheme Summer 207 Discussion 0: July 25, 207 Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs,

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

Begin at the beginning Epressions (Synta) Compile-time Static Eec-time Dynamic Types Values (Semantics) 1. Programmer enters epression 2. ML checks if epression is well-typed Using a precise set of rules,

### Begin at the beginning

Begin at the beginning Expressions (Syntax) Exec-time Dynamic Values (Semantics) Compile-time Static Types 1. Programmer enters expression 2. ML checks if expression is well-typed Using a precise set of

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

Mini-ML CS 502 Lecture 2 8/28/08 ML This course focuses on compilation techniques for functional languages Programs expressed in Standard ML Mini-ML (the source language) is an expressive core subset of

### Fall Lecture 3 September 4. Stephen Brookes

15-150 Fall 2018 Lecture 3 September 4 Stephen Brookes Today A brief remark about equality types Using patterns Specifying what a function does equality in ML e1 = e2 Only for expressions whose type is

### Standard ML. Curried Functions. ML Curried Functions.1

Standard ML Curried Functions ML Curried Functions.1 Curried Functions Side Note: Infix function declaration o Curried Function declaration o Declaring via "fun" o Function Calling Order o Examples o Mixing

### 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))

SCHEME 0 COMPUTER SCIENCE 6A July 26, 206 0. Warm Up: Conditional Expressions. What does Scheme print? scm> (if (or #t (/ 0 (/ 0 scm> (if (> 4 3 (+ 2 3 4 (+ 3 4 (* 3 2 scm> ((if (< 4 3 + - 4 00 scm> (if

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

Standard ML Lists ML Lists.1 Lists A list is a finite sequence of elements. [3,5,9] ["a", "list" ] [] Elements may appear more than once [3,4] [4,3] [3,4,3] [3,3,4] Elements may have any type. But all

### Functional Programming. Pure Functional Programming

Functional Programming Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends only on the values of its sub-expressions (if any).

### Handout 2 August 25, 2008

CS 502: Compiling and Programming Systems Handout 2 August 25, 2008 Project The project you will implement will be a subset of Standard ML called Mini-ML. While Mini- ML shares strong syntactic and semantic

### Chapter 3 Linear Structures: Lists

Plan Chapter 3 Linear Structures: Lists 1. Lists... 3.2 2. Basic operations... 3.4 3. Constructors and pattern matching... 3.5 4. Polymorphism... 3.8 5. Simple operations on lists... 3.11 6. Application:

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

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

### 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)

Weekly exercises in INF3110 week 41 6-10.10.2008 Exercise 1 Exercise 6.1 in Mitchell's book a) fun a(x,y) = x+2*y; val a = fn : int * int -> int All the operations used in the expression are over integers.

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

Begin at the beginning Epressions (Synta) Compile-time Static Eec-time Dynamic Types Values (Semantics) 1. Programmer enters epression 2. ML checks if epression is well-typed Using a precise set of rules,

### Data Types The ML Type System

7 Data Types 7.2.4 The ML Type System The following is an ML version of the tail-recursive Fibonacci function introduced Fibonacci function in ML in Section 6.6.1: EXAMPLE 7.96 1. fun fib (n) = 2. let

### Chapter 3 Linear Structures: Lists

Plan Chapter 3 Linear Structures: Lists 1. Two constructors for lists... 3.2 2. Lists... 3.3 3. Basic operations... 3.5 4. Constructors and pattern matching... 3.6 5. Simple operations on lists... 3.9

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

SCHEME 8 COMPUTER SCIENCE 61A March 2, 2017 1 Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs,

### LECTURE 16. Functional Programming

LECTURE 16 Functional Programming WHAT IS FUNCTIONAL PROGRAMMING? Functional programming defines the outputs of a program as a mathematical function of the inputs. Functional programming is a declarative

### Functional Programming Languages (FPL)

Functional Programming Languages (FPL) 1. Definitions... 2 2. Applications... 2 3. Examples... 3 4. FPL Characteristics:... 3 5. Lambda calculus (LC)... 4 6. Functions in FPLs... 7 7. Modern functional

### Exercises on ML. Programming Languages. Chanseok Oh

Exercises on ML Programming Languages Chanseok Oh chanseok@cs.nyu.edu Dejected by an arcane type error? - foldr; val it = fn : ('a * 'b -> 'b) -> 'b -> 'a list -> 'b - foldr (fn x=> fn y => fn z => (max

### Chapter 15. Functional Programming Languages ISBN

Chapter 15 Functional Programming Languages ISBN 0-321-49362-1 Chapter 15 Topics Introduction Mathematical Functions Fundamentals of Functional Programming Languages The First Functional Programming Language:

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

CMSC 330: Organization of Programming Languages OCaml 1 Functional Programming Dialects of ML ML (Meta Language) Univ. of Edinburgh,1973 Part of a theorem proving system LCF The Logic of Computable Functions

### Homework 3 COSE212, Fall 2018

Homework 3 COSE212, Fall 2018 Hakjoo Oh Due: 10/28, 24:00 Problem 1 (100pts) Let us design and implement a programming language called ML. ML is a small yet Turing-complete functional language that supports

### PROGRAMMING IN HASKELL. CS Chapter 6 - Recursive Functions

PROGRAMMING IN HASKELL CS-205 - Chapter 6 - Recursive Functions 0 Introduction As we have seen, many functions can naturally be defined in terms of other functions. factorial :: Int Int factorial n product

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

CMSC 330: Organization of Programming Languages Functional Programming with OCaml 1 Background ML (Meta Language) Univ. of Edinburgh, 1973 Part of a theorem proving system LCF The Logic of Computable Functions

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

CSE 130 : Fall 2011 Recap from last time Programming Languages Lecture 3: Data Types Ranjit Jhala UC San Diego 1 2 A shorthand for function binding Put it together: a filter function # let neg = fun f

### Specification and Verification in Higher Order Logic

Specification and Verification in Higher Order Logic Prof. Dr. K. Madlener 13. April 2011 Prof. Dr. K. Madlener: Specification and Verification in Higher Order Logic 1 Chapter 1 Functional Programming:

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

CSE 130 : Fall 2007 Programming Languages News PA deadlines shifted PA #2 now due 10/24 (next Wed) Lecture 5: Functions and Datatypes Ranjit Jhala UC San Diego Recap: Environments Phone book Variables

### Programming Languages

CSE 130 : Spring 2011 Programming Languages Lecture 3: Crash Course Ctd, Expressions and Types Ranjit Jhala UC San Diego A shorthand for function binding # let neg = fun f -> fun x -> not (f x); # let

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

Plan (next 4 weeks) 1. Fast forward Rapid introduction to what s in OCaml 2. Rewind 3. Slow motion Go over the pieces individually History, Variants Meta Language Designed by Robin Milner @ Edinburgh Language

### An introduction to Scheme

An introduction to Scheme Introduction A powerful programming language is more than just a means for instructing a computer to perform tasks. The language also serves as a framework within which we organize

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

Due: Thurs October 27, 10:00pm CSE 341, Autumn 2005, Assignment 3 ML - MiniML Interpreter Note: This is a much longer assignment than anything we ve seen up until now. You are given two weeks to complete

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

SCHEME 7 COMPUTER SCIENCE 61A October 29, 2015 1 Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs,

### Programming Languages

CSE 130 : Winter 2009 Programming Languages News PA 2 out, and due Mon 1/26 5pm Lecture 5: Functions and Datatypes t UC San Diego Recap: Environments Phone book Variables = names Values = phone number

### Programming Languages

CSE 130: Spring 2010 Programming Languages Lecture 3: Epressions and Types Ranjit Jhala UC San Diego A Problem fun -> +1 Can functions only have a single parameter? A Solution: Simultaneous Binding Parameter

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

CS 6A Scheme Fall 207 Discussion 7: October 25, 207 Solutions Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write

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

CS 61A Scheme Spring 2018 Discussion 7: March 21, 2018 1 Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme

### MIDTERM EXAMINATION - CS130 - Spring 2005

MIDTERM EAMINATION - CS130 - Spring 2005 Your full name: Your UCSD ID number: This exam is closed book and closed notes Total number of points in this exam: 231 + 25 extra credit This exam counts for 25%

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

Section 5.3 1 Recursion Recursion defining an object (or function, algorithm, etc.) in terms of itself. Recursion can be used to define sequences Previously sequences were defined using a specific formula,

### Patterns The Essence of Functional Programming

Patterns The Essence of Functional Programming Up to now we have defined functions in a very traditional way: function name + variable name parameters Read Chap 7 In functional programming we can exploit

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

CSc 372, Fall 1996 Mid-Term Examination Monday, October 21, 1996 READ THIS FIRST Do not turn this page until you are told to begin. This examination consists of nine problems and an extra credit section

### Functional programming with Common Lisp

Functional programming with Common Lisp Dr. C. Constantinides Department of Computer Science and Software Engineering Concordia University Montreal, Canada August 11, 2016 1 / 81 Expressions and functions

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

CS 6A Scheme Fall 208 Discussion 8: October 24, 208 Solutions Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write

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

News CSE 130: Spring 2012 Programming Languages On webpage: Suggested HW #1 PA #1 (due next Fri 4/13) Lecture 2: A Crash Course in ML Please post questions to Piazza Ranjit Jhala UC San Diego Today: A

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

(Func&onal (Programming (in (Scheme)))) Jianguo Lu 1 Programming paradigms Func&onal No assignment statement No side effect Use recursion Logic OOP AOP 2 What is func&onal programming It is NOT what you

### So what does studying PL buy me?

So what does studying PL buy me? Enables you to better choose the right language but isn t that decided by libraries, standards, and my boss? Yes. Chicken-and-egg. My goal: educate tomorrow s tech leaders

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

Computer Science CSC324 Wednesday February 13, 2013 St. George Campus University of Toronto Homework Assignment #3 Due: Thursday February 28, 2013, by 10 p.m. Silent Policy A silent policy takes effect

### Sample Final Exam Questions

91.301, Organization of Programming Languages Fall 2015, Prof. Yanco Sample Final Exam Questions Note that the final is a 3 hour exam and will have more questions than this handout. The final exam will

### CSCI-GA Scripting Languages

CSCI-GA.3033.003 Scripting Languages 12/02/2013 OCaml 1 Acknowledgement The material on these slides is based on notes provided by Dexter Kozen. 2 About OCaml A functional programming language All computation

### CSCI-GA Final Exam

CSCI-GA 2110-003 - Final Exam Instructor: Thomas Wies Name: Sample Solution ID: You have 110 minutes time. There are 7 assignments and you can reach 110 points in total. You can solve the exercises directly

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

CMSC 330: Organization of Programming Languages OCaml 2 Higher Order Functions Tuples Constructed using (e1,..., en) Deconstructed using pattern matching Patterns involve parens and commas, e.g., (p1,p2,

### Principles of Programming Languages 2017W, Functional Programming

Principles of Programming Languages 2017W, Functional Programming Assignment 3: Lisp Machine (16 points) Lisp is a language based on the lambda calculus with strict execution semantics and dynamic typing.

### Chapter 15. Functional Programming Languages

Chapter 15 Functional Programming Languages Chapter 15 Topics Introduction Mathematical Functions Fundamentals of Functional Programming Languages The First Functional Programming Language: Lisp Introduction

### Higher-Order Functions

Higher-Order Functions Tjark Weber Functional Programming 1 Based on notes by Pierre Flener, Jean-Noël Monette, Sven-Olof Nyström Tjark Weber (UU) Higher-Order Functions 1 / 1 Tail Recursion http://xkcd.com/1270/

### COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

COSE212: Programming Languages Lecture 3 Functional Programming in OCaml Hakjoo Oh 2017 Fall Hakjoo Oh COSE212 2017 Fall, Lecture 3 September 18, 2017 1 / 44 Why learn ML? Learning ML is a good way of

### CS 11 Ocaml track: lecture 2

Today: CS 11 Ocaml track: lecture 2 comments algebraic data types more pattern matching records polymorphic types ocaml libraries exception handling Previously... ocaml interactive interpreter compiling

### F28PL1 Programming Languages. Lecture 14: Standard ML 4

F28PL1 Programming Languages Lecture 14: Standard ML 4 Polymorphic list operations length of list base case: [] ==> 0 recursion case: (h::t) => 1 more than length of t - fun length [] = 0 length (_::t)

### An anecdote about ML type inference

An anecdote about ML type inference Andrew Koenig (ark@research.att.com) AT&T Bell Laboratories Murray Hill, New Jersey 07974 Introduction ML strongly supports functional programming; its programmers t

### An Explicit Continuation Evaluator for Scheme

Massachusetts Institute of Technology Course Notes 2 6.844, Spring 05: Computability Theory of and with Scheme February 17 Prof. Albert R. Meyer revised March 3, 2005, 1265 minutes An Explicit Continuation

### CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

CMSC 330: Organization of Programming Languages OCaml Imperative Programming CMSC330 Fall 2017 1 So Far, Only Functional Programming We haven t given you any way so far to change something in memory All

### Shell CSCE 314 TAMU. Functions continued

1 CSCE 314: Programming Languages Dr. Dylan Shell Functions continued 2 Outline Defining Functions List Comprehensions Recursion 3 A Function without Recursion Many functions can naturally be defined in

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

OCaml 1. Introduction Rapid introduction to what s in OCaml 2. Focus on Features Individually as Needed as Semester Progresses *Notes from Sorin Lerner at UCSD* History, Variants Meta Language Designed

### CSE 341 Sample Midterm #2

1. s For each ML expression in the left-hand column of the table below, indicate in the right-hand column its value. Be sure to (e.g., 7.0 rather than 7 for a real; Strings in quotes e.g. "hello"; true

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

Section 5.3 1 Recursion 2 Recursion Recursion defining an object (or function, algorithm, etc.) in terms of itself. Recursion can be used to define sequences Previously sequences were defined using a specific

### CSE341, Fall 2011, Midterm Examination October 31, 2011

CSE341, Fall 2011, Midterm Examination October 31, 2011 Please do not turn the page until the bell rings. Rules: The exam is closed-book, closed-note, except for one side of one 8.5x11in piece of paper.

### Lists. Michael P. Fourman. February 2, 2010

Lists Michael P. Fourman February 2, 2010 1 Introduction The list is a fundamental datatype in most functional languages. ML is no exception; list is a built-in ML type constructor. However, to introduce

### Programming Languages

CSE 130 : Winter 2013 Programming Languages Lecture 3: Crash Course, Datatypes Ranjit Jhala UC San Diego 1 Story So Far... Simple Expressions Branches Let-Bindings... Today: Finish Crash Course Datatypes

### Datatype declarations

Datatype declarations datatype suit = HEARTS DIAMONDS CLUBS SPADES datatype a list = nil (* copy me NOT! *) op :: of a * a list datatype a heap = EHEAP HEAP of a * a heap * a heap type suit val HEARTS

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

CS 490.40 Lectures 2-3 Introduction to OCaml Polyvios Pratikakis Based on slides by Jeff Foster History ML: Meta Language 1973, University of Edinburg Used to program search tactics in LCF theorem prover

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

Booleans (aka Truth Values) Programming Languages and Compilers (CS 421) Sasa Misailovic 4110 SC, UIUC https://courses.engr.illinois.edu/cs421/fa2017/cs421a # true;; - : bool = true # false;; - : bool

### CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

CMSC 330: Organization of Programming Languages OCaml Imperative Programming CMSC330 Spring 2018 1 So Far, Only Functional Programming We haven t given you any way so far to change something in memory

### SML. CSE 307 Principles of Programming Languages Stony Brook University

SML CSE 307 Principles of Programming Languages Stony Brook University http://www.cs.stonybrook.edu/~cse307 1 Functional Programming Function evaluation is the basic concept for a programming paradigm

### Currying fun f x y = expression;

Currying ML chooses the most general (least-restrictive) type possible for user-defined functions. Functions are first-class objects, as in Scheme. The function definition fun f x y = expression; defines

### 15. Functional Programming

15. Functional Programming 15.1 Introduction The design of the imperative languages is based directly on the von Neumann architecture Efficiency is the primary concern, rather than the suitability of the

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

Lecture #13: Type Inference and Unification Typing In the Language ML Examples from the language ML: fun map f [] = [] map f (a :: y) = (f a) :: (map f y) fun reduce f init [] = init reduce f init (a ::

### Combining Static and Dynamic Contract Checking for Curry

Michael Hanus (CAU Kiel) Combining Static and Dynamic Contract Checking for Curry LOPSTR 2017 1 Combining Static and Dynamic Contract Checking for Curry Michael Hanus University of Kiel Programming Languages

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

CMSC 330: Organization of Programming Languages Functional Programming with Lists CMSC330 Spring 2018 1 Lists in OCaml The basic data structure in OCaml Lists can be of arbitrary length Implemented as

### Chapter 3 Programming with Recursion

Plan Chapter 3 Programming with Recursion 1. Examples... 3.2 2. Correctness... 3.5 3. Construction methodology... 3.13 4. Forms of recursion... 3.16 Sven-Olof Nyström/IT Dept/Uppsala University FP 3.1