Problem Solving, Programming, and Calculation

Similar documents
List Functions, and Higher-Order Functions

Option Values, Arrays, Sequences, and Lazy Evaluation

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

An introduction introduction to functional functional programming programming using usin Haskell

Lambda Calculus and Type Inference

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

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

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

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

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

Shell CSCE 314 TAMU. Haskell Functions

Some Advanced ML Features

Functional Programming

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

Scheme: Data. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, April 3, Glenn G.

Introduction. chapter Functions

Lambda Calculus and Type Inference

The PCAT Programming Language Reference Manual

Haskell: Lists. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Friday, February 24, Glenn G.

CSC312 Principles of Programming Languages : Functional Programming Language. Copyright 2006 The McGraw-Hill Companies, Inc.

Scheme: Expressions & Procedures

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

Functional Programming. Pure Functional Programming

Scheme Tutorial. Introduction. The Structure of Scheme Programs. Syntax

CSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Haskell Functions

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

The SPL Programming Language Reference Manual

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

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

CS 320: Concepts of Programming Languages

Haskell 98 in short! CPSC 449 Principles of Programming Languages

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

CS 11 Haskell track: lecture 1

Functional Programming Languages (FPL)

It is better to have 100 functions operate one one data structure, than 10 functions on 10 data structures. A. Perlis

Compilation and Execution Simplifying Fractions. Loops If Statements. Variables Operations Using Functions Errors

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

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

Haskell: From Basic to Advanced. Part 2 Type Classes, Laziness, IO, Modules

PROGRAMMING IN HASKELL. Chapter 2 - First Steps

LECTURE 16. Functional Programming

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

Datatype declarations

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

Chapter 1. Fundamentals of Higher Order Programming

Lecture 19: Functions, Types and Data Structures in Haskell

CSCI-GA Scripting Languages

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

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

Lecture 2: SML Basics

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

Imperative programming in F#

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

Tail Calls. CMSC 330: Organization of Programming Languages. Tail Recursion. Tail Recursion (cont d) Names and Binding. Tail Recursion (cont d)

Typed Racket: Racket with Static Types

2/5/2018. Expressions are Used to Perform Calculations. ECE 220: Computer Systems & Programming. Our Class Focuses on Four Types of Operator in C

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

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

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

SMURF Language Reference Manual Serial MUsic Represented as Functions

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

Polymorphic lambda calculus Princ. of Progr. Languages (and Extended ) The University of Birmingham. c Uday Reddy

Topics Covered Thus Far. CMSC 330: Organization of Programming Languages. Language Features Covered Thus Far. Programming Languages Revisited

Types, Expressions, and States

Exercises on ML. Programming Languages. Chanseok Oh

Will introduce various operators supported by C language Identify supported operations Present some of terms characterizing operators

Modern Programming Languages. Lecture LISP Programming Language An Introduction

Thoughts on Assignment 4 Haskell: Flow of Control

Operators and Expressions:

SCHEME The Scheme Interpreter. 2 Primitives COMPUTER SCIENCE 61A. October 29th, 2012

Reasoning About Programs Panagiotis Manolios

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Functional Programming. Big Picture. Design of Programming Languages

CS 360: Programming Languages Lecture 10: Introduction to Haskell

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

CPS 506 Comparative Programming Languages. Programming Language Paradigm

CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Dan Grossman Spring 2011

The Typed Racket Guide

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

11/6/17. Functional programming. FP Foundations, Scheme (2) LISP Data Types. LISP Data Types. LISP Data Types. Scheme. LISP: John McCarthy 1958 MIT

Practical Haskell. An introduction to functional programming. July 21, Practical Haskell. Juan Pedro Villa-Isaza. Introduction.

CMSC 330: Organization of Programming Languages. Operational Semantics

Introduction to Haskell

Overview of C. Basic Data Types Constants Variables Identifiers Keywords Basic I/O

The Haskell HOP: Higher-order Programming

OCaml Data CMSC 330: Organization of Programming Languages. User Defined Types. Variation: Shapes in Java

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

SML A F unctional Functional Language Language Lecture 19

Imperative languages

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

Lecture 9. Assignment. Logical Operations. Logical Operations - Motivation 2/8/18

Programming Languages

A general introduction to Functional Programming using Haskell

3. Functional Programming. Oscar Nierstrasz

Topic 1: Introduction

COP4020 Programming Assignment 1 - Spring 2011

Operators. Lecture 3 COP 3014 Spring January 16, 2018

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

Transcription:

Problem Solving, Programming, and Calculation Björn Lisper School of Innovation, Design, and Engineering Mälardalen University bjorn.lisper@mdh.se http://www.idt.mdh.se/ blr/ Problem Solving, Programming, and Calculation (revised 2017-01-24)

Overview Basic concepts of functional programming: computation by calculation, values, expressions, types, declarations First glance of F#: introduction, basic language elements Some simple functional programming examples/exercises Problem Solving, Programming, and Calculation (revised 2017-01-24) 1

Computation by Calculation In ordinary programming languages, computation alters state by writing new values to program variables: State before: x = 3,y = 4 x = 17 + x + y State after: x = 24,y = 4 This means you perform computations in sequence There is a control flow that decides this sequence: for(i=0,i++,i<17) { if (j > i) then x++ else y++; x = y + j; } Problem Solving, Programming, and Calculation (revised 2017-01-24) 2

In (pure) functional languages, computation is simply calculation: 3*(9 + 5) = 3*14 = 42 Forget about assignments, sequencing, loops, if-then-else,... Forget everything you know about programming! (Well, almost) You might wonder how to do useful stuff just by calculating, we ll see later... Problem Solving, Programming, and Calculation (revised 2017-01-24) 3

In functional languages we have: functions recursion (instead of loops) expressive data types (much more than numbers) advanced data structures and calculation on top of that Problem Solving, Programming, and Calculation (revised 2017-01-24) 4

Functions Functions, mathematically: sets of pairs where no two pairs can have the same first component: f = {(1,17),(2,32),(3,4711)} f(2) = 32 Or, given the same argument the function always returns the same value (cannot have f(2) = 32 and f(2) = 33 at the same time) Functions model determinism: that outputs depend predictably on inputs Something we want to hold for computers as well... Problem Solving, Programming, and Calculation (revised 2017-01-24) 5

Defining a function by writing all pairs can be very tedious Often defined by simple rules instead simple(x,y,z) = x (y +z) These rules express abstraction: that a similar pattern holds for many different inputs ( For all x,y,z, simple(x,y,z) equals x (y +z) ) Abstraction makes definitions shorter and easier to grasp A good property also for software, right? Problem Solving, Programming, and Calculation (revised 2017-01-24) 6

Recursion Mathematical functions are often specified by recursive rules Recursion means that a defined entity refers to itself in the definition This seems circular, but can make sense Example: the factorial function! on natural numbers 0! = 1 n! = n (n 1)!, n > 0 Recursion corresponds to loops in ordinary programming Problem Solving, Programming, and Calculation (revised 2017-01-24) 7

Pure Functional Languages Pure functional languages implement mathematical functions A functional language is pure if there are no side-effects A side effect means that a function call does something more than just return a value An example in C: int f(int x) { n++; return(n+x); } Problem Solving, Programming, and Calculation (revised 2017-01-24) 8

Side effect forf: global variablenis incremented for each call This means thatfreturns different values for different calls, even when called with the same argument Much harder to reason mathematically about such functions: for instance, f(17) + f(17) 2*f(17) Side effects require a more complex model, and thus makes it harder to understand the software Problem Solving, Programming, and Calculation (revised 2017-01-24) 9

In pure functional languages, functions are specified by side-effect free rules (declarations) In F#: let simple x y z = x*(y + z) Each rule defines a calculation for any actual arguments: simple 3 9 5 = 3*(9 + 5) = 3*14 = 42 Just put actual arguments into the right-hand side and go! Compare this with an execution model that must account for side-effects Problem Solving, Programming, and Calculation (revised 2017-01-24) 10

Exercise Calculatesimple (simple 2 3 4) 5 6 Note that we can do the calculation in different order Do we get the same result? More on this later... Problem Solving, Programming, and Calculation (revised 2017-01-24) 11

The mathematical view makes it possible to prove properties of programs When calculating, all intermediate results are mathematically equal: simple 3 9 5 =3*(9 + 5) =3*14 =42 For instance, prove thatsimple x y z =simple x z y for allx,y,z: simple x y z = x*(y + z) = x*(z + y) = simple x z y We cannot do this for functions with side-effects Problem Solving, Programming, and Calculation (revised 2017-01-24) 12

Expressions, Values, and Types Calculation is performed on expressions: simple (simple 2 3 4) 5 6 Expressions are calculated into values: simple (simple 2 3 4) 5 6 = 154 Values are also expressions, which cannot be calculated any further Problem Solving, Programming, and Calculation (revised 2017-01-24) 13

Types Many programming languages have types Types represent sets of values (like integers) Types help avoiding certain programming errors, like adding an integer with a character A programming language can be: strongly typed: every program part must have a legal type weakly typed: every program part can have a legal type, but need not untyped: no types exist Problem Solving, Programming, and Calculation (revised 2017-01-24) 14

A First Introduction of F# F# is a strongly typed, functional language It uses eager evaluation (compute function arguments fully before making the call), but also supports lazy evaluation and computation by demand (don t compute anything before it s needed) It is higher order (functions are ordinary data) It is not pure (side effects possible), but encourages pure programming It supports imperative and object-oriented programming It has a polymorphic type system, with type inference, subtyping, and overloading of common operators It has syntactic facilities to help write clear and understandable programs Problem Solving, Programming, and Calculation (revised 2017-01-24) 15

F# implementation F# comes from Microsoft Research Current version is 4.0 (the lab computers have 2.0) Included in VS 2012 (and up), can also be freely downloaded Can also be run on Mac/linux under Mono v. 4.2.0 or later Three ways to run F# compiler: In Visual Studio fsc (v. 2.0) orfsharpc (v. 3.1) batch compiler fsi (v. 2.0) orfsharp1 (v. 3.1) interactive command line compiler Problem Solving, Programming, and Calculation (revised 2017-01-24) 16

Basic Language Elements of F# A first introduction: Values Types (atomic values, composed values) Operators and predefined functions Other syntactic forms Problem Solving, Programming, and Calculation (revised 2017-01-24) 17

Numerical Types F# has a number of numerical types, some important examples: int 32-bit integers (1,-3,54873,...) float double precision (64-bit) floats (1.0,3.14159,3.2e3,...) int64 64-bit integers (1L,-3L,54873L,...) single single precision (32-bit) floats (1.0f,3.14159f,3.2e3f,...) Problem Solving, Programming, and Calculation (revised 2017-01-24) 18

Functions and Operators on Numerical Types The usual ones: +,-,*,/,%(modulus): all numeric types Operands must have same type: 2 + 3 OK,2 + 3.0 not OK ** (power): all floating-point types Bitwise operators: all integer types Most typical math functions (trigonometric, exponent, etc) Type conversion functions: same name as type converted to: int 17.3 = 17 int 17L = 17 Problem Solving, Programming, and Calculation (revised 2017-01-24) 19

Numerical Expressions Numerical expressions look like in most languages: x + 7*y 3.14159/(x + 1.0) - 33.0 (x + 7*y is the same asx + (7*y), since*binds stronger than+) Problem Solving, Programming, and Calculation (revised 2017-01-24) 20

Characters Typechar for characters Syntax: a, b, \n (newline),...) Characters are elements in strings More on strings later Problem Solving, Programming, and Calculation (revised 2017-01-24) 21

Booleans Typebool for the two booleans valuestrue,false Boolean operators and functions: && (and), (or),not Relational operator returning a boolean value: =,<>,<,<=,>=,> Can compare elements from any comparable type (more on this later) Problem Solving, Programming, and Calculation (revised 2017-01-24) 22

Conditional F# has a conditional if-then-else expression: if true then x else y = x if false then x else y = y So we can write expressions like if x > 0 then x else -x However, the two branches must have the same type Thus,if x > 0 then 17 else a is illegal Problem Solving, Programming, and Calculation (revised 2017-01-24) 23

Functions Functions take a number of arguments and return a result Some predefined ones, and you can define your own A little unusual syntax: no parentheses around arguments sqrt 17.0 exp 17.0 3.0 (There are good reasons for this syntax, more on this later) Problem Solving, Programming, and Calculation (revised 2017-01-24) 24

The space between function and argument can be seen as a special operator: function application Function application binds harder than any other operator Thus,f x + y means(f x) + y, notf (x + y) (Common beginner s mistake to forget this) Problem Solving, Programming, and Calculation (revised 2017-01-24) 25

Declarations You can define your own entities Entities of any type can be defined by a let declaration let pi = 3.14159 definespi to be a floating-point constant let simple x y z = x*(y+z) definessimple to be a function in three arguments Problem Solving, Programming, and Calculation (revised 2017-01-24) 26

Types of Defined Entities Note that we did not give any types in the definitions F# manages to find types automatically! This is called type inference A very simple example: let pi = 3.14159 Here,pi will automatically obtain the typefloat since3.14159 can only have type float Problem Solving, Programming, and Calculation (revised 2017-01-24) 27

Explicit Type Declarations However, we can also give explicit types (sometimes useful) e : t declareseto have typet For instance: let f x = (x + x) : float forces the right-hand side of the declaration to have typefloat. (If we just writelet f x = (x + x), then F# will assume that + is plus on the typeint.) What aboutlet f x = (x + x) : char? Problem Solving, Programming, and Calculation (revised 2017-01-24) 28

Recursive Definitions As a first exercise, recall the factorial function: 0! = 1 n! = n (n 1)!, n > 0 Define it in F#! (Answer on next slide) Problem Solving, Programming, and Calculation (revised 2017-01-24) 29

Factorial in F# let rec fac n = if n = 0 then 1 else n*fac (n-1) Notelet rec: recursive definition,fac in right-hand side must be same as in left-hand side. More in this later Exercise: Calculatefac 3 Problem Solving, Programming, and Calculation (revised 2017-01-24) 30

fac 3 = if 3 = 0 then 1 else 3*fac (3-1) = if false then 1 else 3*fac (3-1) = 3*fac (3-1) = 3*fac 2 = 3*(if 2 = 0 then 1 else 2*fac (2-1)) = 3*(if false then 1 else 2*fac (2-1)) = 3*2*fac (2-1)...etc... = 3*2*1*fac (1-1) = 3*2*1*fac 0 = 3*2*1*1 = 6 Eventually we ll reach the base case fac 0. This is what makes recursion work here! Problem Solving, Programming, and Calculation (revised 2017-01-24) 31

Pattern Matching Another way to write the factorial function: use patterns F# has a case construct match expr with pattern1 -> expr1 pattern2 -> expr2... Every case has a pattern for the argument Cases are checked in order, the first that matches the argument is selected For atomic types like numeric types, the possible patterns are constants (matching exactly that constant), or variables (matching any value) Problem Solving, Programming, and Calculation (revised 2017-01-24) 32

So we can definefac like this instead: let rec fac n = match n with 0 -> 1 _ -> n*fac (n-1) Ifnis0return1, otherwise returnn*fac (n-1) _ is a wildcard, matches anything Problem Solving, Programming, and Calculation (revised 2017-01-24) 33

More on Pattern Matching Note thatmatch... with is just another expression, it is evaluated and returns a result Pattern matching is really useful with structured data, where it can be used to conventiently pick out parts of data structures. More on this later Problem Solving, Programming, and Calculation (revised 2017-01-24) 34

Nontermination and Errors Now what aboutfac (-1)? fac(-1) = (-1)*fac(-2) = (-1)*(-2)*fac(-3) = Infinite recursion! Will never terminate. For computations that never return anything, we use the notation for the resulting value Thus,fac(-1) = Rememberfac is really just defined for natural numbers, not for negative numbers It s good practice to have controlled error handling of out-of-range arguments Problem Solving, Programming, and Calculation (revised 2017-01-24) 35

The F# failwith Function F# has anfailwith function, which when executed prints a string and stops the execution E.g., failwith "You cannot input this number to this function" (Strings in F# are written within quotes, like"hello world") Problem Solving, Programming, and Calculation (revised 2017-01-24) 36

A version offac with error handling: let rec fac n = match n with 0 -> 1 _ -> if n > 0 then n*fac (n-1) else failwith "Negative argument to fac" Would we be able to write this case-by case (n > 0,n = 0,n < 0)? Yes, pattern-matching can be extended with guards. More on this later Problem Solving, Programming, and Calculation (revised 2017-01-24) 37

Factorial Old Style How would you implement the factorial function in an imperative language, with loops and program variables that can be assigned new values? Problem Solving, Programming, and Calculation (revised 2017-01-24) 38

Factorial Old Style (II) My suggestion would be something like this (in C): int fac (int n) { int acc = 1; while (n \= 0) { acc = n*acc; n = n-1; } return(acc); } Notice howacc is used to successively accumulate the product. Could we do something similar in a pure functional language? Problem Solving, Programming, and Calculation (revised 2017-01-24) 39

Factorial with Accumulating Arguments The answer is yes: let rec fac1 acc n = match n with 0 -> acc _ -> fac1 (n*acc) (n-1) let fac n = fac1 1 n This solution uses a help functionfac1 with two arguments The second argument is an accumulating argument, where we successively collect the result. It plays the same role asacc in the C loop Exercise: calculatefac 3 with this new definition Problem Solving, Programming, and Calculation (revised 2017-01-24) 40

McCarthy s Transformation The derivation offac with accumulating arguments from the C loop is an example of McCarthy s Transformation. This transformation is a systematic method to transform imperative programs into pure functional programs Theorem: any imperative program that can be expressed as a flowchart can be transformed into a pure functional program computing the same thing So imperative programming is just a special case of functional programming, and loops are just a special case of recursion Problem Solving, Programming, and Calculation (revised 2017-01-24) 41

Local Definitions Thefac version with accumulating argument uses a help functionfac1 This function is globally defined However, only used byfac We may want to hide it in the definition offac F# has alet (rec)... in...-construct for local definitions: fac n = let rec fac1 acc n = match n with 0 -> acc _ -> fac1 (n*acc) (n-1) in fac1 1 n Definesfac1 locally in the expression after in. Not visible outside! Problem Solving, Programming, and Calculation (revised 2017-01-24) 42

let (rec)... in-expressions are ordinary expressions and return values, can be used wherever ordinary expressions can be used: 17 + let x = fac 3 in x + 3*x = 17 + let x = 6 in x + 3*x = 17 + (6 + 3*6) = 41 Also note that the defined entity (x here) only needs to be computed once saves work! Solet can be used also to save work when the same result is to be used many times Problem Solving, Programming, and Calculation (revised 2017-01-24) 43

Function Types A function that takes an argument of typeaand returns a value of typeb has the function typea -> b For instance,fac : int -> int (Note resemblance with mathematical notation) What about functions with several arguments? let simple x y z = x*(y+z) simple : int -> int -> int -> int Last type is result type, preceding types are the respective argument types (We ll explain later why multi-argument function types look this way) Problem Solving, Programming, and Calculation (revised 2017-01-24) 44

Types for Data Structures F# has two important predefined types for data structures: tuples and lists (Also some more types for this, but we ll leave them for now) Lists are very important data structures in functional programming They are sequences of elements Lists can be arbitrarily long, but (in F#) all elements must be of the same type Ifais a type, thena list is the type list ofa E.g. int list,char list,(int list) list, (float -> int) list A list can contain elements of any type (as long as all have the same type) Problem Solving, Programming, and Calculation (revised 2017-01-24) 45

Constructing Lists Lists are built from: the empty list: [] the cons operator, which puts an element in front of a list: :: Example: 1::(2::(3::[])) :: and[] are called constructors: they construct data structures (Constants like17 and x are also constructors, but they only construct themselves) (Don t mix up with constructors in object-oriented langages) Problem Solving, Programming, and Calculation (revised 2017-01-24) 46

1::2::3::[] is same as1::(2::(3::[])) ( :: is right-associative) [1;2;3] is another shorthand for1::(2::(3::[])) The first element of a nonempty list is the head: List.head [1;2;3] = 1 (List.head is a function from the List module, thus the prefixlist. More on modules later) The list of the remaining elements is the tail List.tail [1;2;3] = [2;3] Also a functionlist.isempty to check if a list is empty or not: List.isEmpty [] = true Problem Solving, Programming, and Calculation (revised 2017-01-24) 47

To the left is a graphical picture of[1;2;3] as an expression tree: :: cons 1 1 :: cons 2 2 :: cons 3 3 [] nil Underneath, there is a linked data structure (shown to the right) In conventional languages you d have to manage the links yourselves. Functional programming runtime systems handle them automatically Problem Solving, Programming, and Calculation (revised 2017-01-24) 48

Some List Functions F# has many builtin functions on lists in thelist module. Let s look at some and try to program them, as an exercise List.length xs, computes the length of the listxs List.length [ a ; b ; c ] = 3 List.sum xs, sums all the numbers inxs List.sum [1;2;3] = 6 (Solutions on next slide) Problem Solving, Programming, and Calculation (revised 2017-01-24) 49

let rec length l = if List.isEmpty l then 0 else 1 + length (List.tail l) let rec sum l = match l with if List.isEmpty l then 0 else List.head l + sum (List.tail l) Problem Solving, Programming, and Calculation (revised 2017-01-24) 50

With Pattern Matching let rec length l = match l with [] -> 0 (x::xs) -> 1 + length xs let rec sum l = match l with [] -> 0 (x::xs) -> x + sum xs The pattern(x::xs) matches any list that is constructed with a cons (that is, any non-empty list) x gets bound to the head, andxs to the tail Problem Solving, Programming, and Calculation (revised 2017-01-24) 51

An Observation onsum We have: sum [1,2,3] =sum 1::(2::(3::[])) = 1+(2+(3+0)) = 6 Note the similarity between tree for list1::(2::(3::[])) and sum expression for1+(2+(3+0)): :: + 1 :: 1 + 2 :: 2 + 3 [] 3 0 Problem Solving, Programming, and Calculation (revised 2017-01-24) 52

sum basically replaces:: with+and then calculates the result This is a common pattern! We ll get back to this when we treat higher-order functions Also: in a sense, data structures (like lists) in F# are just expressions where the operators (= constructors) cannot be further evaluated: thus, the constructors are left in the result (where they build the data structure) Problem Solving, Programming, and Calculation (revised 2017-01-24) 53

Tuples Tuples are similar to records, or objects A tuple is like a container for data with a fixed number of slots An example: ( a,17,3.14159) This is a three-tuple whose first component is a character, the second an integer, and the third a floating-point number It has the tuple typechar * int * float Tuples can contain any type of data, for instance: (fac,(17, x )) : (int -> int) * (int * char) Thus, there are really infinitely many tuple types Problem Solving, Programming, and Calculation (revised 2017-01-24) 54

Tuples (II) Tuple types are similar to cartesian products on sets: char int * char c (17, c ) 17 int Similar to vector spaces. An n-tuple is like a vector with n elements, although the elements do not have to be numbers Problem Solving, Programming, and Calculation (revised 2017-01-24) 55

An Example of the Use of Tuples Use tuples with two floats to represent 2D-vectors Define functionsvadd,vsub,vlen to add, subtract, and compute the length of vectors: vadd,vsub : (float * float) -> (float * float) -> (float * float) vlen : (float * float) -> float (Solutions on next slide) Problem Solving, Programming, and Calculation (revised 2017-01-24) 56

let vadd (x1,y1) (x2,y2) = (x1+x2,y1+y2) : float * float let vsub (x1,y1) (x2,y2) = (x1-x2,y1-y2) : float * float let vlen (x,y) = sqrt (x*x + y*y) : float Note the pattern-matching to get the components of the argument tuples (Why did we have to type the function bodies?) Problem Solving, Programming, and Calculation (revised 2017-01-24) 57

Another Matching Example with Tuples take n xs, returns list of firstnelements from the listxs take 2 [ a ; b ; c ] = [ a ; b ] Let s define it using thematch... with construct! (Solution on next slide) Problem Solving, Programming, and Calculation (revised 2017-01-24) 58

let rec take n xs = match (n,xs) with (0,_) -> [] (_,[]) -> failwith "taking too many elements" (_,x::xs) -> x :: take (n-1) xs Note how we constructed a tuple just to be able to match on both arguments totake The second, local xs obscures the outer xs in the recursive call take (n-1) xs Excercise: provide some error handling for the casen < 0! Problem Solving, Programming, and Calculation (revised 2017-01-24) 59