11/1/20151 GC16/3011 Functional Programming Lecture 5 Miranda patterns, functions, recursion and lists.

Slides:



Advertisements
Similar presentations
Functional Programming Lecture 10 - type checking.
Advertisements

4/6/20141 GC16/3011 Functional Programming Lecture 8 Designing Functional Programs (2)
More ML Compiling Techniques David Walker. Today More data structures lists More functions More modules.
CMSC 330: Organization of Programming Languages Tuples, Types, Conditionals and Recursion or “How many different OCaml topics can we cover in a single.
ML Lists.1 Standard ML Lists. ML Lists.2 Lists  A list is a finite sequence of elements. [3,5,9] ["a", "list" ] []  ML lists are immutable.  Elements.
Extended Static Checking for Haskell (ESC/Haskell) Dana N. Xu University of Cambridge advised by Simon Peyton Jones Microsoft Research, Cambridge.
ML Datatypes.1 Standard ML Data types. ML Datatypes.2 Concrete Datatypes  The datatype declaration creates new types  These are concrete data types,
Comp 205: Comparative Programming Languages Functional Programming Languages: More Haskell Nested definitions Lists Lecture notes, exercises, etc., can.
Comp 205: Comparative Programming Languages Higher-Order Functions Functions of functions Higher-order functions on lists Reuse Lecture notes, exercises,
5/10/20151 GC16/3011 Functional Programming Lecture 13 Example Programs 1. Evaluating arithmetic expressions 2. lists as functions.
0 PROGRAMMING IN HASKELL Chapter 7 - Higher-Order Functions.
Higher-Order Functions Koen Lindström Claessen. What is a “Higher Order” Function? A function which takes another function as a parameter. Examples map.
Comp 205: Comparative Programming Languages User-Defined Types Enumerated types Parameterised types Recursive types Lecture notes, exercises, etc., can.
Introduction to ML - Part 2 Kenny Zhu. What is next? ML has a rich set of structured values Tuples: (17, true, “stuff”) Records: {name = “george”, age.
Introduction to ML Last time: Basics: integers, Booleans, tuples,... simple functions introduction to data types This time, we continue writing an evaluator.
0 PROGRAMMING IN HASKELL Chapter 3 - Types and Classes.
Cse536 Functional Programming 1 7/14/2015 Lecture #2, Sept 29, 2004 Reading Assignments –Begin Chapter 2 of the Text Home work #1 can be found on the webpage,
Comp 205: Comparative Programming Languages Functional Programming Languages: More Lists Recursive definitions List comprehensions Lecture notes, exercises,
Comp. Eng. SW Lab II: FP with Scheme 1 Computer Eng. Software Lab II , Semester 2, Who I am: Andrew Davison CoE, WiG Lab Office.
10/12/20151 GC16/3C11 Functional Programming Lecture 3 The Lambda Calculus A (slightly) deeper look.
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using.
Built-in Data Structures in Python An Introduction.
Functional Programming With examples in F#. Pure Functional Programming Functional programming involves evaluating expressions rather than executing commands.
1-Nov-15 Haskell II Functions and patterns. Data Types Int + - * / ^ even odd Float + - * / ^ sin cos pi truncate Char ord chr isSpace isUpper … Bool.
A Second Look At ML 1. Outline Patterns Local variable definitions A sorting example 2.
A Third Look At ML Chapter NineModern Programming Languages, 2nd ed.1.
CSE 230 The -Calculus. Background Developed in 1930’s by Alonzo Church Studied in logic and computer science Test bed for procedural and functional PLs.
What is a Type? A type is a name for a collection of related values. For example, in Haskell the basic type Bool contains the two logical values: True.
12/2/20151 GC16/3011 Functional Programming Lecture 2 The Lambda Calculus: A Simple Introduction.
Types and Programming Languages Lecture 11 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Error Example - 65/4; ! Toplevel input: ! 65/4; ! ^^ ! Type clash: expression of type ! int ! cannot have type ! real.
Chapter SevenModern Programming Languages1 A Second Look At ML.
CSED101 INTRODUCTION TO COMPUTING FUNCTION ( 함수 ) 유환조 Hwanjo Yu.
3/5/20161 GC16/3011 Functional Programming Lecture 11 Algebraic Types.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
1 PROGRAMMING IN HASKELL Lecture 2 Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and a few other sources)
Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. L03-1 September 14, 2006http:// -calculus: A Basis for.
© M. Winter COSC 4P41 – Functional Programming Some functions id :: a -> a id x = x const :: a -> b -> a const k _ = k ($) :: (a -> b) -> a -> b.
CS314 – Section 5 Recitation 9
PROGRAMMING IN HASKELL
Types CSCE 314 Spring 2016.
ML: a quasi-functional language with strong typing
Functional Programming Lecture 12 - more higher order functions
Functions and patterns
PROGRAMMING IN HASKELL
Functional Programming
PROGRAMMING IN HASKELL
Higher-Order Functions
PROGRAMMING IN HASKELL
CSE 3302 Programming Languages
PROGRAMMING IN HASKELL
Arrays, For loop While loop Do while loop
Extended Static Checking for Haskell (ESC/Haskell)
What would be our focus ? Geometry deals with Declarative or “What is” knowledge. Computer Science deals with Imperative or “How to” knowledge 12/25/2018.
CSCE 314: Programming Languages Dr. Dylan Shell
Haskell Types, Classes, and Functions, Currying, and Polymorphism
What would be our focus ? Geometry deals with Declarative or “What is” knowledge. Computer Science deals with Imperative or “How to” knowledge 2/23/2019.
PROGRAMMING IN HASKELL
CSE-321 Programming Languages Introduction to Functional Programming
CSE 3302 Programming Languages
Functions and patterns
CSCE 314: Programming Languages Dr. Dylan Shell
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Functional Programming
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
GC16/3011 Functional Programming Lecture 9 Higher Order Functions
Presentation transcript:

11/1/20151 GC16/3011 Functional Programming Lecture 5 Miranda patterns, functions, recursion and lists

11/1/20152 Contents  Offside rule / where blocks / evaluation  Partial / polymorphic functions  Patterns and pattern-matching  Recursive functions  Lists  Functions using lists  Recursive functions using lists

11/1/20153 Functions  The offside rule and “where” blocks  Mapping from source to target type domains  Application associates to the left!  Function evaluation  normal order, lazy  main = fst (34, (23 / 0) )

11/1/20154 Functions  Partial functions  those which have no result (i.e. return an error) for some valid values of valid input type  f (x,y) = x / y  Polymorphic functions  fst, snd  g (x,y) = (-y, x)  three x = 3

11/1/20155 Patterns  Tuple patterns  (x,y,z) = (3, “hello”, (34,True,[3]))  as a test  as a definition  Patterns for function definitions not True = False not False = True  Top-down evaluation semantics f 3 = 45 f 489 = 3 f any = 345*219

11/1/20156 Patterns  Non-exhaustive patterns f True = False  Patterns can destroy laziness fst (x,0) = x fst (x,y) = x  Can a pattern contain a redex?  No! A pattern must be a constant expression  (special exception – “(n + 1)” in Miranda)  Duplicate parameter names (Miranda only)

11/1/20157 Recursive Functions  Recursion is the ONLY way to program loops in a functional language  Function calls itself inside its own body  Very powerful – very flexible  In imperative languages, can be slow  In functional languages, highly optimised

11/1/20158 Recursive Functions  Beware: loop_forever x = loop_forever x  Must have:  Terminating condition  Changing argument  …that converges on the terminating condition! f :: num -> [char] f 0 = “” f n = “X” ++ (f (n – 1))

11/1/20159 Recursive Functions  Stack recursion  f 3  “X” ++ (f (3 – 1))  “X” ++ (f 2)  “X” ++ (“X” ++ (f (2-1)))  “X” ++ (“X” ++ (f 1))  “X” ++ (“X” ++ (“X” ++ (f (1-1))))  “X” ++ (“X” ++ (“X” ++ “”))  “X” ++ (“X” ++ “X”)  “X” ++ “XX”  “XXX”

11/1/ Recursive Functions  Accumulative recursion plus :: (num, num) -> num plus :: (num, num) -> num plus (x, 0) = x plus (x, 0) = x plus (x, y) = plus (x+1, y-1) plus (x, y) = plus (x+1, y-1)

11/1/ Type Synonyms  f :: (([char],num,[char]),(num,num,num)) -> bool  str = = [char]  coord = = (num, num, num)  f :: ((str,num,str), coord) -> bool

11/1/ LISTS  Lists are another way to collect together related data  But lists are special - they are RECURSIVE  Data can be recursive, just like functions!

11/1/ LISTS  A list of type  is either:  Empty, or  An element of type  together with a list of elements of the same type  List of num:  []  (34: []) [34]  (34: (13: [])) [34, 13]

11/1/ Functions using lists bothempty :: ([*],[**]) -> bool bothempty ([], []) = True bothempty anything = False myhd :: [*] -> * myhd [] = error “take head of empty list” myhd (x : rest) = x

11/1/ Recursive functions using lists sumlist :: [num] -> num sumlist [] = 0 sumlist (x : rest) = x + (sumlist rest) length :: [*] -> num length [] = 0 length (x : rest) = 1 + (length rest)

11/1/ Exercise  Can you write a function “threes” which takes as input a list of whole numbers and produces as output a count of how many 3s occur in the input?

11/1/ Summary  Offside rule / where blocks / evaluation  Partial / polymorphic functions  Patterns and pattern-matching  Recursive functions  Lists  Functions using lists  Recursive functions using lists