Exercises on ML. Programming Languages. Chanseok Oh

Similar documents
A Third Look At ML. Chapter Nine Modern Programming Languages, 2nd ed. 1

Datatype declarations

Standard ML. Curried Functions. ML Curried Functions.1

SML A F unctional Functional Language Language Lecture 19

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.

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

A quick introduction to SML

CSE 341 Section 5. Winter 2018

CSC324- TUTORIAL 5. Shems Saleh* *Some slides inspired by/based on Afsaneh Fazly s slides

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

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

According to Larry Wall (designer of PERL): a language by geniuses! for geniuses. Lecture 7: Haskell. Haskell 98. Haskell (cont) - Type-safe!

Standard ML. Data types. ML Datatypes.1

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

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

ML Built-in Functions

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

Programming Languages

Higher-Order Functions

Homework 5. Notes. Turn-In Instructions. Reading. Problems. Handout 19 CSCI 334: Spring (Required) Read Mitchell, Chapters 6 and 7.

A Brief Introduction to Standard ML

CSCI-GA Scripting 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

Introduction to SML Basic Types, Tuples, Lists, Trees and Higher-Order Functions

CSE341: Programming Languages Lecture 7 First-Class Functions. Dan Grossman Winter 2013

Begin at the beginning

Lists. Michael P. Fourman. February 2, 2010

Haskell Types, Classes, and Functions, Currying, and Polymorphism

CSE341: Programming Languages Lecture 9 Function-Closure Idioms. Dan Grossman Winter 2013

A Fourth Look At ML. Chapter Eleven Modern Programming Languages, 2nd ed. 1

Functional Programming and Haskell

Lecture 2: The Basis of SML

CSE 130 [Winter 2014] Programming Languages

CS 320: Concepts of Programming Languages

Solution sheet 1. Introduction. Exercise 1 - Types of values. Exercise 2 - Constructors

Programming Languages

CVO103: Programming Languages. Lecture 5 Design and Implementation of PLs (1) Expressions

A general introduction to Functional Programming using Haskell

CSCC24 Functional Programming Typing, Scope, Exceptions ML

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

Programming Languages

L3 Programming September 19, OCaml Cheatsheet

Functional Paradigm II

So what does studying PL buy me?

Recap: ML s Holy Trinity

Programming Languages

General Computer Science (CH ) Fall 2016 SML Tutorial: Practice Problems

Programming Languages

The type checker will complain that the two branches have different types, one is string and the other is int

Functors signature Order = sig functor name type elem (structname:signature) = structure definition; val le : elem*elem -> bool end; elem

CS 320: Concepts of Programming Languages

Logic - CM0845 Introduction to Haskell

CSCI-GA Final Exam

Fall Lecture 3 September 4. Stephen Brookes

Recap: ML s Holy Trinity. Story So Far... CSE 130 Programming Languages. Datatypes. A function is a value! Next: functions, but remember.

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

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

CSE 130 Programming Languages. Datatypes. Ranjit Jhala UC San Diego

CSC324 Functional Programming Typing, Exceptions in ML

Lecture 19: Functions, Types and Data Structures in Haskell

Chapter 3 Linear Structures: Lists

n n Try tutorial on front page to get started! n spring13/ n Stack Overflow!

List Processing in SML

Chapter 2 SML, a Functional Programming Language

2.1. Expressions. Chapter 2 SML, a Functional Programming Language. Integers. Interacting with ML

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

CSE 130 Programming Languages. Lecture 3: Datatypes. Ranjit Jhala UC San Diego

Lecture 12: Data Types (and Some Leftover ML)

Australian researchers develop typeface they say can boost memory, that could help students cramming for exams.

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

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

CMSC 330: Organization of Programming Languages

CSE341, Fall 2011, Midterm Examination October 31, 2011

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

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

Handout 2 August 25, 2008

List Processing in SML

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

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

PROGRAMMING IN HASKELL. Chapter 5 - List Comprehensions

CS558 Programming Languages

Overloading, Type Classes, and Algebraic Datatypes

CSE341 Autumn 2017, Midterm Examination October 30, 2017

CS558 Programming Languages

Principles of Programming Languages

This example highlights the difference between imperative and functional programming. The imperative programming solution is based on an accumulator

CSE341, Fall 2011, Midterm Examination October 31, 2011

2.1. Expressions. Chapter 2 ML, a Functional Programming Language. Integers. Interacting with ML

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

F28PL1 Programming Languages. Lecture 14: Standard ML 4

CS 360: Programming Languages Lecture 12: More Haskell

CSE341: Programming Languages Lecture 9 Function-Closure Idioms. Dan Grossman Fall 2011

Functional Programming

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

Lecture #23: Conversion and Type Inference

Introduc)on To Standard ML

Topic 5: Higher Order Functions

Topic 5: Higher Order Functions

Conversion vs. Subtyping. Lecture #23: Conversion and Type Inference. Integer Conversions. Conversions: Implicit vs. Explicit. Object x = "Hello";

Transcription:

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 x y, min x z)) [1,2,3] (0, 10); stdin:66.1-66.70 Error: operator and operand don't agree [tycon mismatch] operator domain: 'Z * 'Y -> 'Y operand: in expression: int -> int -> int -> int * int foldr (fn x => (fn <pat> => <exp>))

Standard ML Thanks to the type inference, - Strongly typed - cf. C++ is arguably weakly typed. - Statically typed

Type of an expression Expressions that are not functions - ~1; ~ : unary minus val it = ~1 : int - true orelse false; val it = true : bool - 1.0 + 4.5; val it = 5.5 : real - #"c"; #"ch" error val it = #"c" : char - "some" ^ "string"; val it = "somestring" : string

Type of an expression Lists (still not functions) - []; val it = [] : 'a list (polymorphic, 'a can be any type) - nil; val it = [] : 'a list - [ #"a", #"b" ]; val it = [ #"a", #"b" ] : char list - [ [ 1, 2 ], [], [3] ]; val it = [ [1,2], [], [3] ] : int list list = ((int list) list)

Then, what about the function expressions? Remember, functions are fist-class citizens in ML. Can be assigned to (mutable) variables, passed to a function as arguments, or returned by a function, etc. They can be treated like other values. They are really values. - fn x => 2 * x; val it = fn : int -> int - print; val it = fn : string -> unit They also have types!

Binding names to function expressions - Functions (or values) can be bound to names. - val x = 3; val x = 3 : int - val double = fn x => 2 * x; val double = fn : int -> int Alternative syntax to define a function: - fun double x = 2 * x; val double = fn : int -> int

Function Type - fun max a b = if a > b then a else b; val max = fn : int -> int -> int Why two arrows? One might just say: Well, that looks like a stupid notation. I ll just think of max as a function taking 2 integer arguments and returning an integer.

Function Type - fun max (a, b) = if a > b then a else b; val max = fn : int * int -> int Why an asterisk now? One might just say: That s annoying. I don t care. I ll just think of max as a function with 2 arguments.

Tuples - (10, #"c"); val it = (10, #"c") : int * char - (1, [[ ]], "str"); val it = (1,[[]],"str") : int * 'a list list * string - fun max (a, b) = if This is not a function taking 2 arguments. Why? val max = fn : int * int -> int = (int * int) -> int

Function Type - fun max a b = if - val max = fn a => fn b => if - val max = (fn a => (fn b => if )) - fun max a = fn b => if val max = fn : int -> int -> int int -> (int -> int)

Currying - val max a b = if a > b then a else b; val max = fn : int -> int -> int λa. ( λb. ( if a > b then a else b )) - val at_least_two = max 2; val at_least_two = fn : int -> int (λa. λb. ( if a > b then a else b )) 2 = λb. ( if 2 > b then 2 else b ) - val two = at_least_two 0; val two = 2 : int (λb. ( )) 0 = ( if 2 > 0 then 2 else 0 ) = 2

Currying - 2 + 3; val it = 5 : int - op+; val it = fn : int * int -> int - op+ (2, 3); val it = 5 : int - plus 2 3; - plus 2;

Currying - fun curry f a b = f (a, b); val curry = fn : ('a * 'b -> 'c) -> 'a -> 'b -> 'c - op+; val it = fn : int * int -> int - curry op+; val it = fn : int -> int -> int - val iszero = curry op= 0; val iszero = fn : int -> bool - List.filter (iszero o (curry op+ 1)) [0,1,0,~1,0,~1]; val it = [~1,~1] : int list

Frequent mistakes - max -1 3; Error: operator and operand don't agree (max - 1) 3 - max ~1 3; val it = 3 : int ~ is also a function of int -> int. - ~; val it = fn : int -> int

Frequent mistakes - fun op o = Error: - 4 / 2; Error: operator and operand don't agree - op /; val it = fn : real * real -> real - 8.0 / 2.5; - op div; val it = fn : int * int -> int - 4 div 2;

Frequent mistakes (Cont d) - fun fact n = if n=0 then 1 else fact n-1; val fact = fn : int -> int - fact 0; val it = 1 : int - fact 3; ( does not terminate.) - fun fact n = if n=0 then 1 else (fact n)-1; - fun fact n = if n=0 then 1 else fact (n-1);

Beware! - fun fact n = if n=0 then 1 else fact n-1; val fact = fn : int -> int At this point, you ve noticed the mistake, and you decide to redefine the function. - val fact = fn n => if n=0 then 1 else fact (n-1); - fact 3; ( still does not terminate.)

Beware! (Cont d) - val fact = fn n => if n=0 then 1 else fact (n-1); This did not generate an error only because fact was previously defined. - val f = fn () => f (); : unbound variable or constructor: f - val rec fact = fn n => if n=0 then 1 else fact (n-1); fun f n =... is like val rec f = fn n =>

Exercises 'a list -> int -> 'a list -> int ('a list -> int) -> 'a list -> int ('a -> 'a -> int) -> 'a list -> 'a list 'a -> ('a * 'b) list -> 'b map : ('a -> 'b) -> 'a list -> 'b list map ord [#"A", #"B"];

Exercises filter : ('a -> bool) -> 'a list -> 'a list filter even [1, 2, 3, 4] [2, 4] filter (not o even) [1, 2, 3, 4] [1, 3] fun lt = curry op< fun gt a b = curry op> filter (lt 3) [1, 2, 3, 4, 5] [4, 5] filter (not o (lt 3)) [1, 2, 3, 4, 5] [1, 2, 3] filter (gt 3) [1, 2, 3, 4, 5] [1, 2] filter (not o (gt 3)) [1, 2, 3, 4, 5] [3, 4, 5]

Exercises foldl : ('a * 'b -> 'b) -> 'b -> 'a list -> 'b foldl op+ 0 [~5, 5, 10]; foldl op+ 0 (map abs [~5, 5, 10]); foldl (fn (lst, len) => length lst + len) 0 [[], [1], [1,2,3]];

Quicksort (Polymorphic) fun qsort _ [] = [] qsort lt (x :: xs) = (qsort lt (filter (not o (lt x)) xs)) @ [x] @ (qsort lt (filter (lt x) xs)); <= x sorted x > x sorted

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 x y, min x z)) [1,2,3] (0, 10); stdin:66.1-66.70 Error: operator and operand don't agree [tycon mismatch] operator domain: 'Z * 'Y -> 'Y operand: in expression: int -> int -> int -> int * int foldr (fn x => (fn <pat> => <exp>))

Pattern Matching let val (fst, _, trd) = some_tuple val e1 :: e2 :: tail = [3] @ some_list in if [1 + 4, e1] <> [1 + e1, e2] then ( case (e2, fst) :: [(10, trd)] of [(3, _), _] => 0 _ :: [(10, "str")] => 1 (e2, "name") :: [_] => 2 _ => 3 ) else 10 end

Pattern Matching (Cont d) fun silly [] = 0 silly (2 :: _ :: 3 :: xs) = 3 + silly xs silly (_ :: xs) = 2 * silly xs