Standard ML. Curried Functions. ML Curried Functions.1

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

Exercises on ML. Programming Languages. Chanseok Oh

Chapter 2 SML, a Functional Programming Language

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

Lecture 2: The Basis of SML

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

Chapter 3 Linear Structures: Lists

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

Variables and Bindings

Chapter 3 Linear Structures: Lists

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

SOFTWARE VERIFICATION AND COMPUTER PROOF (lesson 1) Enrico Tassi Inria Sophia-Antipolis

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

CSci 4223 Principles of Programming Languages

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)

Handout 2 August 25, 2008

Standard ML. Data types. ML Datatypes.1

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

Datatype declarations

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

Principles of Programming Languages

15 150: Principles of Functional Programming. More about Higher-Order Functions

ML Built-in Functions

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

CSE 341 Section 5. Winter 2018

Defining Functions. CSc 372. Comparative Programming Languages. 5 : Haskell Function Definitions. Department of Computer Science University of Arizona

Chapter 7 Control I Expressions and Statements

Fall Lecture 3 September 4. Stephen Brookes

CSCI-GA Scripting Languages

Lecture 12: Data Types (and Some Leftover ML)

Recursive Definitions, Fixed Points and the Combinator

CSc 372, Fall 2006; Quiz 1 Time: 3:00; Value: 2 points. 1. What is a fundamental difference between the if-then-else constructs in Java (or C) and ML?

Recap: Functions as first-class values

Informatics 1 Functional Programming Lecture 7. Map, filter, fold. Don Sannella University of Edinburgh

02157 Functional Programming Lecture 2: Functions, Basic Types and Tuples

A quick introduction to SML

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

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

Higher Order Functions in Haskell

Fall 2018 Stephen Brookes. LECTURE 2 Thursday, August 30

Scope. Chapter Ten Modern Programming Languages 1

Lecture 19: Functions, Types and Data Structures in Haskell

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

CSE341, Fall 2011, Midterm Examination October 31, 2011

Introduction to Scheme

Functional Programming Lecture 1: Introduction

Functional Paradigm II

L3 Programming September 19, OCaml Cheatsheet

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

Haske k ll An introduction to Functional functional programming using Haskell Purely Lazy Example: QuickSort in Java Example: QuickSort in Haskell

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

CSc 372 Comparative Programming Languages

CSE341, Fall 2011, Midterm Examination October 31, 2011

Higher-Order Functions

Fundamentals of Artificial Intelligence COMP221: Functional Programming in Scheme (and LISP)

Functions & First Class Function Values

A lexical analyzer generator for Standard ML. Version 1.6.0, October 1994

An introduction introduction to functional functional programming programming using usin Haskell

CSc 372. Comparative Programming Languages. 8 : Haskell Function Examples. Department of Computer Science University of Arizona

News. CSE 130: Programming Languages. Environments & Closures. Functions are first-class values. Recap: Functions as first-class values

CSc 372. Comparative Programming Languages. 4 : Haskell Basics. Department of Computer Science University of Arizona

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

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

List Processing in SML

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

Lecture 13: Expression Evaluation

List Processing in SML

CSc 372 Comparative Programming Languages. 4 : Haskell Basics

F28PL1 Programming Languages. Lecture 12: Standard ML 2

INF121: Functional Algorithmic and Programming

Currying fun f x y = expression;

Lambda Calculus. Gunnar Gotshalks LC-1

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages

CSE341 Autumn 2017, Midterm Examination October 30, 2017

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

Begin at the beginning

9/21/17. Outline. Expression Evaluation and Control Flow. Arithmetic Expressions. Operators. Operators. Notation & Placement

CSE341: Programming Languages Lecture 4 Records, Datatypes, Case Expressions. Dan Grossman Spring 2013

CSc 520. Principles of Programming Languages 11: Haskell Basics

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

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

Principles of Programming Languages COMP251: Functional Programming in Scheme (and LISP)

Products and Records

Logical Methods in... using Haskell Getting Started

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

Introduction to Haskell

SMURF Language Reference Manual Serial MUsic Represented as Functions

INF 212 ANALYSIS OF PROG. LANGS FUNCTION COMPOSITION. Instructors: Crista Lopes Copyright Instructors.

CSE 341: Programming Languages

Inductive Data Types

Left to right design 1

The Haskell HOP: Higher-order Programming

UNIT 3

Chapter 1. Fundamentals of Higher Order Programming

UNIVERSITY OF EDINBURGH COLLEGE OF SCIENCE AND ENGINEERING SCHOOL OF INFORMATICS INFR08013 INFORMATICS 1 - FUNCTIONAL PROGRAMMING

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

Introduc)on To Standard ML

CS558 Programming Languages

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

Transcription:

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 infix & curried ML Curried Functions.2

Side Note - Operators A function of two arguments can be treated using infix notation fun d(x,y) = Math.sqrt(x*x+y*y); val d = fn : real * real -> real - d; val it = fn : real * real -> real - d(1.0,3.0); val it = 3.16227766017 : real Convert to infix - infix d; infix d - 1.0 d 3.0; val it = 3.16227766017 : real - 1.0 d 3.0 d 2.0 d 5.0; val it = 6.2449979984 : real ML Curried Functions.3

Operators - a bit more Access - d; stdin:40.1 Error: expression or pattern begins with infix identifier "d" - op d; val it = fn : real * real -> real - op d(1.0,3.0); val it = 3.16227766017 : real Infix declaration can come before function definition - infix d; infix d - fun x d y = Math.sqrt(x*x + y*y); val d = fn : real * real -> real ML Curried Functions.4

Curried Functions Side Note: Infix function declaration Curried Function declaration o Declaring via "fun" o Function Calling Order o Examples o Mixing infix & curried ML Curried Functions.5

Curried Functions Any function of two arguments (a*b)->g can be expressed as a curried function of one argument a->(b->g) Example - fun prefix (pre,post) = pre^post; val prefix = fn : string * string -> string The curried version - using function as return value - fun prefix pre = fn post => pre^post; val prefix = fn : string -> string -> string Reminder: Arrow associate to the right. The next type is equivalent to the last one val prefix = fn : string -> (string -> string) ML Curried Functions.6

Partial Application You don t have to give the next arguments! - prefix "Dr. "; val it = fn : string -> string - it "Tomer"; val it = "Dr. Tomer" : string As Always, functions are values - val doctorify = prefix "Dr. "; val doctorify = fn : string -> string - doctorify "Jackal"; val it = "Dr. Jackal" : string Observation prefix : prefix "Dr. " : prefix "Dr." "Tomer": string -> string -> string string -> string string ML Curried Functions.7

Curried Functions Side Note: Infix function declaration Curried Function declaration Declaring via "fun" o Function Calling Order o Examples o Mixing infix & curried ML Curried Functions.8

Curried - Syntactic Sugar Instead of using anonymous functions, A fun declaration may have several arguments, separated by spaces, for a curried function. - fun prefix pre post = pre^post; val prefix = fn: string -> string -> string Is equivalent to - fun prefix pre = fn post => pre^post; ML Curried Functions.9

Curried Functions Side Note: Infix function declaration Curried Function declaration Declaring via "fun" Function Calling Order o Examples o Mixing infix & curried ML Curried Functions.10

Function Calls Function call - (prefix "Dr. ") "Tomer"; val it = "Dr. Tomer" : string - prefix "Dr. " "Tomer"; val it = "Dr. Tomer" : string The rule is A function call Abbreviates F E1 E2...En (...((F E1) E2)...)En The same ML Curried Functions.11

Curried Functions Side Note: Infix function declaration Curried Function declaration Declaring via "fun" Function Calling Order Examples o Mixing infix & curried ML Curried Functions.12

General-Purpose Functions - Sections Applying infix operator only on one operand - specific case - fun add5 y = op+ (5, y); val add5 = fn: int -> int - add5 2; val it = 7 : int - fun mul5 y = op* (5, y); val mul5 = fn: int -> int Now generalize the operator and operand - fun something5 (f:int*int->int) y = f (5, y); val something5 = fn: (int*int->int) -> int -> int - val add5 = something5 op+; val add5 = fn: int -> int - fun intsec x (f:int*int->int) y = f(x,y); val intsec = fn : int -> (int * int -> int) -> int -> int ML Curried Functions.13

Recursive Curried Functions Recursion - fun times n m = if m=0 then 0 = else n + times n (m-1); val times = fn : int -> int -> int - times 4 5; val it = 20 : int - val times_4 = times 4; val times_4 = fn : int -> int - times_4(8); val it = 32 : int times_4 is actually fn m => if m=0 then 0 else 4 + times 4 (m-1) ML Curried Functions.14

Curried Functions Side Note: Infix function declaration Curried Function declaration Declaring via "fun" Function Calling Order Examples Mixing infix & curried ML Curried Functions.15

Composition Operator Composition - infix o; - fun (f o g) x = f (g x); val o = fn : ('a -> 'b) * ('c -> 'a) -> 'c -> 'b - Math.sqrt o Math.sqrt; val it = fn : real -> real - it (16.0); val it = 2.0 : real Ord converts a char to its ascii - (fn x => x - ord #"0") o ord; val it = fn : char -> int - it #"1"; val it = 1 : int ML Curried Functions.16

Curried Functions Side Note: Infix function declaration Curried Function declaration Declaring via "fun" Function Calling Order Examples Mixing infix & curried ML Curried Functions.17