ML Built-in Functions

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

Logic - CM0845 Introduction to Haskell

Exercises on ML. Programming Languages. Chanseok Oh

Higher Order Functions in Haskell

Higher-Order Functions

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

Patterns The Essence of Functional Programming

CSCI-GA Scripting Languages

CSE3322 Programming Languages and Implementation

A quick introduction to SML

CSE341 Spring 2016, Midterm Examination April 29, 2016

Semester Review CSC 301

Handout 2 August 25, 2008

F28PL1 Programming Languages. Lecture 14: Standard ML 4

Principles of Programming Languages COMP251: Syntax and Grammars

Standard ML. Curried Functions. ML Curried Functions.1

A general introduction to Functional Programming using Haskell

CSE341 Spring 2016, Midterm Examination April 29, 2016

Datatype declarations

UNIVERSITETET I OSLO

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

Introduction to OCaml

CSci 4223 Principles of Programming Languages

EECS 700 Functional Programming

CSE 130 [Winter 2014] Programming Languages

CS 340 Spring 2019 Midterm Exam

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

UNIVERSITETET I OSLO

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

Functional Paradigm II

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

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

CSCE 314 Programming Languages. Functional Parsers

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

Haskell Overview II (2A) Young Won Lim 8/23/16

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

Recursive Definitions, Fixed Points and the Combinator

Warm-up and Memoization

Write: evens. evens [] ====> [] evens [1;2;3;4] ====> [2;4]

1. true / false By a compiler we mean a program that translates to code that will run natively on some machine.

Haskell Overview II (2A) Young Won Lim 8/9/16

CSE 341, Spring 2011, Final Examination 9 June Please do not turn the page until everyone is ready.

Principles of Programming Languages COMP251: Syntax and Grammars

Recursion. Q: What does this evaluate to? Q: What does this evaluate to? CSE 130 : Programming Languages. Higher-Order Functions

Lecture 12: Data Types (and Some Leftover ML)

CSE3322 Programming Languages and Implementation

Subtyping and Objects

Haskell Overview II (2A) Young Won Lim 9/26/16

Part I: Written Problems

Consider the following EBNF definition of a small language:

CSE341 Section 3. Standard-Library Docs, First-Class Functions, & More

CSCI-GA Final Exam

Functional Programming

Compiler Construction Lent Term 2015 Lectures 10, 11 (of 16)

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

Lecture #23: Conversion and Type Inference

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

Concepts of Programming Languages

CMSC330 Spring 2008 Final Exam

CMSC330 Fall 2014 Midterm 2 Solutions

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)

Talen en Compilers. Jurriaan Hage , period 2. November 13, Department of Information and Computing Sciences Utrecht University

Lecture 19: Functions, Types and Data Structures in Haskell

CSE341 Autumn 2017, Midterm Examination October 30, 2017

Lists. Michael P. Fourman. February 2, 2010

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

Inductive Data Types

CSc 372. Comparative Programming Languages. 11 : Haskell Higher-Order Functions. Department of Computer Science University of Arizona

ECE251 Midterm practice questions, Fall 2010

LECTURE 17. Expressions and Assignment

CSc 520 Principles of Programming Languages. Currying Revisited... Currying Revisited. 16: Haskell Higher-Order Functions

CSE 3302 Programming Languages Lecture 8: Functional Programming

CSE3322 Programming Languages and Implementation

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

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

02157 Functional Programming. Michael R. Ha. Lecture 2: Functions, Types and Lists. Michael R. Hansen

Compiler Theory. (Semantic Analysis and Run-Time Environments)

CS 314 Principles of Programming Languages

CMSC 330, Fall 2013, Practice Problem 3 Solutions

CSE341 Spring 2017, Midterm Examination April 28, 2017

CSE341, Fall 2011, Midterm Examination October 31, 2011

Syntax Analysis. Prof. James L. Frankel Harvard University. Version of 6:43 PM 6-Feb-2018 Copyright 2018, 2015 James L. Frankel. All rights reserved.

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

CSE341, Spring 2013, Final Examination June 13, 2013

PROGRAMMING IN HASKELL. Chapter 2 - First Steps

LL(k) Parsing. Predictive Parsers. LL(k) Parser Structure. Sample Parse Table. LL(1) Parsing Algorithm. Push RHS in Reverse Order 10/17/2012

CSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Functional Parsers

CSE399: Advanced Programming. Handout 2

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

Chapter 3 Linear Structures: Lists

Derivations of a CFG. MACM 300 Formal Languages and Automata. Context-free Grammars. Derivations and parse trees

CSE P 501 Exam 8/5/04

Functional Paradigm II

CMSC330 Spring 2014 Midterm 2 Solutions

CSE 341 : Programming Languages Midterm, Spring 2015

CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists. Zach Tatlock Winter 2018

Function definitions. CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists. Example, extended. Some gotchas. Zach Tatlock Winter 2018

CMSC 330, Fall 2013, Practice Problems 3

Functional Programming. Big Picture. Design of Programming Languages

Introduction to Functional Programming in Racket. CS 550 Programming Languages Jeremy Johnson

Transcription:

ML Built-in Functions Since ML is a functional programming language, many of its built-in functions are concerned with function application to objects and structures. In ML, built-in functions are curried they expect their arguments as a sequence of objects separated by spaces NOT as a tuple.

The map Function The map function accepts two parameters: a function and a list of objects. It will apply the given function to each object on the list. Example: - map (fn x => x + 2) [1,2,3]; val it = [3,4,5] : int list also works with built-in functions and operators such as the negation function ~ : int -> int - map ~ [1,2,3]; val it [~1,~2,~3] : int list

The map Function map can also be applied to a list of structures. - map (fn (a,b) => a + b) [(1,2),(3,4)]; val it = [3,7] : int list

The foldr Function The foldr function works similar to the map function, but instead of producing a list of values it only produces a single output value. Syntax: foldr <binary function> <initial value of output> <list> Semantics: - foldr f c [x1, x2,..., xn-1, xn]; is the same as saying - f(x1, f(x2,... f(x-1,f(xn,c))...)); foldr folds a list of values into a single value starting with the rightmost element. foldr start at the rightmost object xn of the list with initial value c

The foldr Function Example: - foldr (fn (a,b) => a+b) 2 [1,2,3]; fn(1,fn(2,fn(3,2))); val it = 8 : int

The foldl Function You guessed it! Works exactly the same as the foldr function except that it start computing at the leftmost element: - foldl f c [x1, x2,..., xn-1, xn]; is the same as saying - f(xn, f(xn-1,... f(x2,f(x1,c))...)); foldl folds a list of values into a single value starting with the leftmost element. Example: - foldl (fn (a,b) => a+b) 2 [1,2,3]; => fn(3,fn(2,fn(1,2))); val it = 8 : int

foldr and foldl In most cases foldr and foldl will produce the same results, but consider the following: - foldr (fn (a,b) => a^b) ef [ ab, cd ]; => fn( ab,fn( cd, ef )) => ab ^( cd ^ ef ) => ab ^ cd ^ ef => abcdef val it = abcdef : string - foldl (fn (a,b) => a^b) ef [ ab, cd ]; => fn( cd,fn( ab, ef )) => cd ^( ab ^ ef ) => cd ^ ab ^ ef => cdabef val it = cdabef : string foldr and foldl will only produce the same results if the mapped function is commutative.

Partial Evaluation l We can create new functions from curried library functions using partial evaluation: - val listinc = map (fn x => x+1); val listinc = fn : int list -> int list - listinc [1,2,3]; val it = [2,3,4] : int list

Recursion and Curried Functions (* original non-curried function *) fun filter ([ ],e) = [ ] filter (x::xs,e) = if x < e then x::filter(xs,e) else filter(xs,e); (* curried function in traditional notation *) fun filtercl [ ] = (fn e => [ ]) filtercl (x::xs) = (fn e => if x < e then x :: filtercl xs e else filtercl xs e); (* curried function in short hand notation *) fun filterc [ ] e = [ ] filterc (x::xs) e = if x < e then x :: filterc xs e else filterc xs e; Note: all parentheses are mandatory in the above examples.

Homework Assignment #7 see website Midterm coming up on Sakai covers chaps 1 through 9

Review Week 1 Chapter 1: Programming Languages features of languages, classes of languages Chapter 2: Defining Program Syntax grammars, derivations, formal definition of languages, sentences Week 2 Chapter 3: Where Syntax Meets Semantics parse trees as semantics, ambiguous grammars Chapter 4: Language Systems structure of IDE/compiler, difference between compiler/interpreter Week 3 Chapter 5: A First Look At ML basic expression, tuples, lists Chapter 6: Types ** a type is a set of values ** Week 4 Chapter 7: A Second Look At ML patterns Chapter 8: Polymorphism overloading, parameter coercion, parametric polymorphism, subtype polymorphism Week 5 Chapter 9: A Third Look At ML higher-order programming: *** functions as parameters or return values ***

Review l Consider the curried function fun foo (a:string) = (fn (b:string) => (a,b)); l What is the value and type of the following computations: 1. foo 100 101 ; 2. val q = foo "happy"; q "really happy"; l Rewrite this function in the abbreviated curried style.

Review l Convert the following function fun pow(b,m) = if m = 0 then 1 else b*pow(b,m-1); 1. to a function using patterns 2. to a function using currying 3. to function using patterns and currying

Review l Write a curried function hdmap that takes a function and a list of integers and applies the function to the first element of the list. If the list is empty return ~1, hdmap = fn : (int -> int) -> int list -> int l Show that your function works by computing: hdmap (fn x => x + 1) [3,4]