Programming language seminar The F# language. Peter Sestoft Wednesday Course plan

Similar documents
Appendix A Crash Course in F#

02157 Functional Programming. Michael R. Ha. Lecture 1: Introduction and Getting Started. Michael R. Hansen

02157 Functional Programming Lecture 1: Introduction and Getting Started

02157 Functional Programming. Michael R. Ha. Disjoint Unions and Higher-order list functions. Michael R. Hansen

02157 Functional Programming Tagged values and Higher-order list functions

Some Advanced ML Features

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

Lists. Michael P. Fourman. February 2, 2010

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

02157 Functional Programming. Michael R. Ha. Tagged values and Higher-order list functions. Michael R. Hansen

Datatype declarations

Exercises on ML. Programming Languages. Chanseok Oh

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

ITT8060: Advanced Programming (in F#)

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

Introduction to OCaml

CSCI-GA Scripting Languages

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

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

Functional Programming Using F#

Introduction to ML. Mooly Sagiv. Cornell CS 3110 Data Structures and Functional Programming

CS 321 Programming Languages

Introduction to OCaml

Tomas Petricek. F# Language Overview

Programs as Data 6 Imperative languages, environment and store, micro-c

CSE 130 [Winter 2014] Programming Languages

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

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

An introduction introduction to functional functional programming programming using usin Haskell

Introduction to SML Getting Started

F28PL1 Programming Languages. Lecture 14: Standard ML 4

Handout 2 August 25, 2008

Introduction to ML. Mooly Sagiv. Cornell CS 3110 Data Structures and Functional Programming

The List Datatype. CSc 372. Comparative Programming Languages. 6 : Haskell Lists. Department of Computer Science University of Arizona

Index. Cambridge University Press Functional Programming Using F# Michael R. Hansen and Hans Rischel. Index.

CSE 307: Principles of Programming Languages

CSE 307: Principles of Programming Languages

CSE341: Programming Languages Lecture 6 Nested Patterns Exceptions Tail Recursion. Zach Tatlock Winter 2018

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

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

CS 11 Ocaml track: lecture 2

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

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

CSci 4223 Principles of Programming Languages

COSE212: Programming Languages. Lecture 4 Recursive and Higher-Order Programming

Imperative programming in F#

Introduction to ML. Mooly Sagiv. Cornell CS 3110 Data Structures and Functional Programming

The Typed Racket Guide

Lecture 4: Higher Order Functions

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

List Functions, and Higher-Order Functions

CSE341: Programming Languages Lecture 11 Type Inference. Dan Grossman Spring 2016

02157 Functional Programming Finite Trees (I)

CSCC24 Functional Programming Typing, Scope, Exceptions ML

CIS 120 Midterm I February 16, 2015 SOLUTIONS

Programs as data first-order functional language type checking

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

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

Typed Racket: Racket with Static Types

Programming Languages and Techniques (CIS120)

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

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

Overloading, Type Classes, and Algebraic Datatypes

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

Topic 5: Examples and higher-order functions

Software System Design and Implementation

CMSC 330, Fall 2013, Practice Problems 3

Introduction to Functional Programming

Chapter 3 Linear Structures: Lists

Inductive Data Types

Programs as data Parsing cont d; first-order functional language, type checking

Scheme as implemented by Racket

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

CS558 Programming Languages

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

CSE 130: Programming Languages. Polymorphism. Ranjit Jhala UC San Diego

Informatics 1 Functional Programming Lecture 4. Lists and Recursion. Don Sannella University of Edinburgh

CSCI-GA Final Exam

Lecture 12: Data Types (and Some Leftover ML)

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

Chapter 3 Linear Structures: Lists

Programs as Data 8 A stack machine for micro-c; compiling micro-c to stack machine code

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

CSE341 Spring 2016, Midterm Examination April 29, 2016

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

Standard ML. Curried Functions. ML Curried Functions.1

A quick introduction to SML

Programming Languages

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

Option Values, Arrays, Sequences, and Lazy Evaluation

Introduction to Objective Caml

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

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

Why OCaml? Introduction to Objective Caml. Features of OCaml. Applications written in OCaml. Stefan Wehr

Types and Type Inference

Lecture #23: Conversion and Type Inference

Cunning Plan. One-Slide Summary. Functional Programming. Functional Programming. Introduction to COOL #1. Classroom Object-Oriented Language

CSE 341 Section 5. Winter 2018

3. Functional Programming. Oscar Nierstrasz

Lists. Adrian Groza. Department of Computer Science Technical University of Cluj-Napoca

Transcription:

Programming language seminar 2011 The F# language Peter Sestoft Wednesday 2011-08-31 www.itu.dk 1 Course plan Four cycles, each containing a mini-project: F# Types and type inference Scala and Scala actors Programming language technology inside a highperformance spreadsheet implementation, more F# Final course report, individual, 10 pages Either continuation of a mini-project Or some other topic/language/... that you find interesting and want to explore Wednesdays (0900-1200) lectures or consultations, see homepage Homepage http://www.itu.dk/courses/splg/e2011/ www.itu.dk 2 1

Sources Hansen+Rischel: Functional programming with F# (DTU, draft 2011) PDF distributed only to this course; do not redistribute Sestoft: Programming Language Concepts for Software Developers (PLCSD), ITU August 2011 Complete PDF on course homepage Materials on Scala and Scala actors Sestoft: Spreadsheet technology (ITU, later 2011) Homepage http://www.itu.dk/courses/splg/e2011/ Schedule Reading materials Exercises Occasional example programs from lectures www.itu.dk 3 Why F# and Scala 2

F# Developed by Don Syme at Microsoft Research, Cambridge UK A functional language in the ML family With many additions and innovations Dimension types Object-oriented features Workflows (asynchronous computing) Runs on the.net platform (like C#, VB.NET) For Windows, Linux and MacOS Included in Visual Studio 2010 Or from http://msdn.microsoft.com/en-us/fsharp and use Mono 2.10 or.net redistributable www.itu.dk 5 F# values, declarations and types let res = 3+4;; let y = sqrt 2.0;; Expression let large = 10 < res;; Declaration y > 0.0 && 1.0/y > 7.0;; if 3 < 4 then 117 else 118;; let rektor = "Mads " + "Tofte";; Interactive system top-level fsi Strongly typed, types are inferred Immutability: let is binding not assignment 3

F# function definitions Function name Parameter let circlearea r = System.Math.PI * r * r;; let mul2 x = 2.0 * x;; > circlearea 10.0;; val it : float = 314.1592654 A function that finds the average of two floating-point numbers? www.itu.dk 7 F# recursive function definitions let rec fac (n : int) : int = if n=0 then 1 else n * fac(n-1);; Same in Java/C#/C/C++: int fac(int n) { if (n==0) return 1; else return n * fac(n-1); } www.itu.dk 8 4

F# type constraints let islarge x = 10 < x;; val islarge : int -> bool let islarge (x : float) : bool = 10.0 < x;; val islarge : float -> bool What if we give a wrong type constraint? www.itu.dk 9 F# pattern matching let rec fac n = match n with 0 -> 1 _ -> n * fac(n-1);; A pattern can be a variable a constant a wildcard (_) a constructor application x :: xr a list [] or [x] or x::y::xr a tuple (x, y) or (2, 29) or ([], x::xr) www.itu.dk 10 5

F# pairs and tuples let p = (2, 3);; let w = (2, true, 3.4, "blah");; let add (x, y) = x + y;; let noon = (12, 0);; let talk = (15, 15);; let earlier ((h1, m1), (h2, m2)) = h1<h2 (h1=h2 && m1<m2);; www.itu.dk 11 F# lists let x1 = [7; 9; 13];; let x2 = 7 :: 9 :: 13 :: [];; let equal = (x1 = x2);; What type would this list have: int list string list let ss = ["Dear"; title; name; "you have "];; let junkmail2 = String.concat " " ss;; let directory = [("IT dept", 5012); ("Kasper", 5170)];; www.itu.dk 12 6

List append (@) let x1 = [7; 9; 13];; let x3 = [47; 11];; let x1x3 = x1 @ x3;; Result is [7; 9; 13; 47; 11] x1 7 9 13 x3 47 11 x1x3 7 9 13 F# data (lists, pairs, ) are immutable This makes list sharing unobservable www.itu.dk 13 F# defining functions on lists let rec sum xs = match xs with [] -> 0 x::xr -> x + sum xr;; How compute the length of a list? How compute the average of a list? www.itu.dk 14 7

F# record types and records type phonerec = { name : string; phone : int };; let x = { name = "Kasper"; phone = 5170 };; x.name;; x.phone;; A record type for course information: title, teacher, semester? www.itu.dk 15 F# exceptions: raise and catch exception IllegalHour;; let mins h = if h < 0 h > 23 then raise IllegalHour else h * 60;; try (mins 25) with IllegalHour -> -1;; www.itu.dk 16 8

failwith raises the Failure exception let mins h = if h < 0 h > 23 then failwith "Illegal hour" else h * 60;; let mins h = if h < 0 h > 23 then failwithf "Illegal hour, h=%d" h else h * 60;; mins 25;; [...] FailureException: Illegal hour, h=25 Formatted failwith Like C printf www.itu.dk 17 F# algebraic datatypes Algebraic datatype, discriminated union A person is either a teacher or a student: type person = Student of string Teacher of string * int;; let people = [Student "Niels"; Teacher("Peter", 5083)];; let getphone person = match person with Teacher(name, phone) -> phone Student name -> failwith "no phone";; A type to represent weekdays? How would you do person/student/teacher in Java/C#? www.itu.dk 18 9

F# curried functions let addp (x, y) = x + y;; let res1 = addp(17, 25);; Type? let addc x y = x + y;; let res2 = addc 17 25;; Type? Function application is left associative: addc x y means (addc x) y The function type arrow is right associative: int -> int -> int means int -> (int -> int) What would (int -> int) -> int mean? www.itu.dk 19 Anonymous functions in F# A higher-order function takes another function as argument let rec map f xs = match xs with [] -> [] x::xr -> f x :: map f xr ( a-> b) -> ( a list -> b list) let mul2 x = 2.0 * x;; map mul2 [4.0; 5.0; 89.0];; [8.0; 10.0; 178.0] Anonymous functions map (fun x -> 2.0 * x) [4.0; 5.0; 89.0] [false; false; true] map (fun x -> x > 10.0) [4.0; 5.0; 89.0] www.itu.dk 20 10

Higher-order functions in C# C# delegate (function) types delegate R Func<R>() delegate R Func<A1,R>(A1 x1) delegate R Func<A1,A2,R>(A1 x1, A2 x2) unit -> int bool -> int int * bool -> string F# Func<int> Func<bool,int> Func<int,bool,string> C# 3.0 Anonymous function/method expressions fun (x:int) -> x>10 fun x -> x>10 fun x -> x*x F# (int x) => x>10 x => x>10 x => x*x C# 3.0 21 F# polymorphic functions let rec len xs = match xs with [] -> 0 x::xr -> 1 + len xr;; val len : 'a list -> int len [7; 9; 13] len [true; true; false; true] len ["foo"; "bar"] len [("Peter", 47)] The function doesn t look at the list elements so the function is polymorphic and works in any type of list Same as a generic method in Java or C# static int Count<T>(IEnumerable<T> xs) {... } www.itu.dk 22 11

F# polymorphic types type 'a tree = Lf Br of 'a * 'a tree * 'a tree Br(42, Lf, Lf) Br("quoi?", Lf, Lf) Br(("Peter", 47), Lf, Lf) The datatype has same structure regardless of node value type What type instances here? Same as a generic type in Java or C#: class ArrayList<T> {... } interface IEnumerable<T> {... } struct Pair<T,U> {... } delegate R Func<A,R>(A x); www.itu.dk 23 Polymorphic functions on polymorphic types let rec preorder1 t = match t with Lf -> [] Br(v, t1, t2) -> v :: preorder1 t1 @ preorder1 t2 Return the tree s node values in pre-order first root, then left subtree, then right subtree Works on any type of tree What is the type of this function? www.itu.dk 24 12

Accumulating parameters The append (@) operation may be slow A faster version of preorder, no append! let rec preo t acc = Accumulating match t with parameter Lf -> acc Br(v, t1, t2) -> v :: preo t1 (preo t2 acc);; let preorder2 t = preo t [];; This works because preo t acc = preorder1 t @ acc So preorder2 t = preo t [] = preorder1 t www.itu.dk O(n) versus O(n 2 ) Can be 1000 x faster Try #time;; in F# Uniform iteration over a list let rec sum xs = match xs with [] -> 0 x::xr -> x + sum xr let rec prod xs = match xs with [] -> 1 x::xr -> x * prod xr Generalizing 0/1 to e, and +/* to f: let rec foldr f xs e = match xs with [] -> e x::xr -> f x (foldr f xr e) int list -> int The foldr function replaces :: by f, and [] by e: foldr (x 1 ::x 2 :: ::x n ::[]) e = x 1 (x 2 (... (x n e) )) www.itu.dk ('a -> 'b -> 'b) -> 'a list -> 'b -> 'b List.foldBack in F# 13

Many functions definable using foldr len xs = foldr (fun _ res -> 1+res) xs 0 sum xs = foldr (fun x res -> x+res) xs 0 prod xs = foldr (fun x res -> x*res) xs 1 map g xs = foldr (fun x res -> g x :: res) xs [] listconcat xss = foldr (fun xs res -> xs @ res) xss [] strconcat ss = foldr (fun s res -> s ^ res) ss " filter p xs = list of those x in xs for which p x is true forall p xs = p x is true for all x in xs exists p xs = p x is true for some x in xs www.itu.dk 27 Composing functions, pipe Given list xs, throw away small numbers, square the remaining numbers, and compute their sum: sum (map (fun x -> x*x) (filter (fun x -> x>10) xs)) Somewhat difficult to read: inside-out Idea: Define infix higher-order function > x > f = f x Now the list operations combine naturally: xs > filter (fun x -> x>10) > map (fun x -> x*x) > sum www.itu.dk 28 14

F# mutable references A reference is a cell that can be updated let r = ref 177!r (r :=!r+1;!r) Create int reference Dereference!r Assign to reference Useful for generation of new names etc: let nextlab = ref -1;; let newlabel () = (nextlab := 1 +!nextlab; "L" + string (!nextlab));; newlabel();; newlabel();; newlabel();; www.itu.dk Reading and exercises Install F# and experiment with it Either Visual Studio 2010 (a monster, available via your MSDNAA account) Or using Mono from www.mono-project.com Read Hansen & Rischel Chapters 1 to 5 There are exercises in Hansen & Rischel Do them as needed Or do the first steps of miniproject 1 www.itu.dk 30 15