Mark Hennessy Dept. Computer Science NUI Maynooth 1 CaML continued CS 351 Programming Paradigms Dept. Computer Science NUI Maynooth.

Slides:



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

Higher-order functions in ML
Modern Programming Languages, 2nd ed.
A Third Look At ML 1. Outline More pattern matching Function values and anonymous functions Higher-order functions and currying Predefined higher-order.
Higher-order functions in OCaml. Higher-order functions A first-order function is one whose parameters and result are all "data" A second-order function.
CMSC 330: Organization of Programming Languages Tuples, Types, Conditionals and Recursion or “How many different OCaml topics can we cover in a single.
Writing functions in OCaml. Defining a simple function # let add (x, y) = x + y;; val add : int * int -> int = Notice what this says: –add is a value.
Reasoning About Code; Hoare Logic, continued
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
Procedures and Control Flow CS351 – Programming Paradigms.
CS 106 Introduction to Computer Science I 02 / 29 / 2008 Instructor: Michael Eckmann.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
CS 106 Introduction to Computer Science I 12 / 04 / 2006 Instructor: Michael Eckmann.
0 PROGRAMMING IN HASKELL Chapter 4 - Defining Functions.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
Mark Hennessy CS351 Dept Computer Science NUI Maynooth 1 Types CS351 – Programming Paradigms.
CS 106 Introduction to Computer Science I 03 / 23 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 28 / 2007 Instructor: Michael Eckmann.
28-Jun-15 Recursion. 2 Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example:
0 PROGRAMMING IN HASKELL Chapter 3 - Types and Classes.
Type Inference: CIS Seminar, 11/3/2009 Type inference: Inside the Type Checker. A presentation by: Daniel Tuck.
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
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,
Functional Programming Element of Functional Programming.
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.
Functional Languages. Why? Referential Transparency Functions as first class objects Higher level of abstraction Potential for parallel execution.
Haskell. 2 GHC and HUGS Haskell 98 is the current version of Haskell GHC (Glasgow Haskell Compiler, version 7.4.1) is the version of Haskell I am using.
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
Patterns in OCaml functions. Formal vs. actual parameters Here's a function definition (in C): –int add (int x, int y) { return x + y; } –x and y are.
Chapter 9: Functional Programming in a Typed Language.
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.
CSC 107 – Programming For Science. Today’s Goal  Discuss writing functions that return values  return statement’s meaning and how it works  When and.
1 CS161 Introduction to Computer Science Topic #9.
Prepared by: A. T. M. Monawer Success in EPT Listening & Speaking Reading Writing Listening &Speaking Reading Writing.
CMSC 330: Organization of Programming Languages Functional Programming with OCaml.
Curry A Tasty dish? Haskell Curry!. Curried Functions Currying is a functional programming technique that takes a function of N arguments and produces.
Chapter SevenModern Programming Languages1 A Second Look At ML.
Recursion ITI 1121 N. El Kadri. Reminders about recursion In your 1 st CS course (or its equivalent), you have seen how to use recursion to solve numerical.
Computer Eng. Software Lab II , Semester 2, Who I am: Andrew Davison CoE, WiG Lab Office Functional Programming.
1 Objective Caml (Ocaml) Aaron Bloomfield CS 415 Fall 2005.
CSE-321 Programming Languages Introduction to Functional Programming POSTECH March 8, 2006 박성우.
Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. L05-1 September 21, 2006http:// Types and Simple Type.
CSC 107 – Programming For Science. Today’s Goal  Write functions that take & return values  How parameters declared and how we call functions  What.
6-Jul-16 Haskell II Functions and patterns. Data Types Int + - * / ^ even odd Float + - * / ^ sin cos pi truncate Char ord chr isSpace isUpper … Bool.
Functional Programming
Functional Programming
PROGRAMMING IN HASKELL
Types CSCE 314 Spring 2016.
ML: a quasi-functional language with strong typing
A lightening tour in 45 minutes
Haskell.
CMSC 330: Organization of Programming Languages
Introduction to Functional Programming in Racket
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Objective caml Daniel Jackson MIT Lab for Computer Science 6898: Advanced Topics in Software Design March 18, 2002.
Announcements Quiz 6 HW7 due Tuesday, October 30
CSCE 314: Programming Languages Dr. Dylan Shell
Haskell Types, Classes, and Functions, Currying, and Polymorphism
Fundamentals of Functional Programming
Introduction to Functional Programming in Racket
PROGRAMMING IN HASKELL
CSE 341 Lecture 11 b closures; scoping rules
Functions and patterns
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Presentation transcript:

Mark Hennessy Dept. Computer Science NUI Maynooth 1 CaML continued CS 351 Programming Paradigms Dept. Computer Science NUI Maynooth

Mark Hennessy Dept. Computer Science NUI Maynooth2 Quick Recap Recall to define functions: let add a b = a + b;; let gt a b = if a >. b then a else b;; let rec addr a b = if b == 0 then 0 else a + addr a (b-1);; All of the functions written to date have been basic functions that don’t really harness the power of FP! Lets talk about, Lists, Higher Order Functions, Currying and Types.

Mark Hennessy Dept. Computer Science NUI Maynooth3 Higher Order Functions A function is said to be a higher order function ( or functional form ) if it takes a function as an argument or returns a function as a result. Eg. let rec addr a b c = a b c;; I want addr to perform identically to addr on the previous slide! What do I do?

Mark Hennessy Dept. Computer Science NUI Maynooth4 Lists The builtin function map allows us to apply a function a list. Lists are very useful data structures in FP. To declare a list use the [ and ] E.g let l = [1;2;3;4];; Declares a list of 4 ints

Mark Hennessy Dept. Computer Science NUI Maynooth5 Lists cont… To append to a list use operator: let l = [3;4];; To check for an empty list you can check to see if the list is == [] or you can use list_length: list_length [1;] = 1 Iterating over a list gives us a powerful way of manipulating data.

Mark Hennessy Dept. Computer Science NUI Maynooth6 Lists cont… To return the value of the first element in the list use hd hd [1;2;3;4];; gives 1 To return the tail of the list ( everything but the head ) use tl tl [1;2;3;4];; gives [2;3;4] Every list is actually a ‘perfect’ list ie there is an implied [] at the end of the list. What does tl [1;] return?

Mark Hennessy Dept. Computer Science NUI Maynooth7 Iterating over a List It is quite easy let rec iter list = if list == [] then (* do something/exit *) else (* use the head *) (hd list); iter (tl list);;

Mark Hennessy Dept. Computer Science NUI Maynooth8 Higher Order Functions and Lists We can write a function and apply it to every element in a list quite easily: let plusone x = x+1;; map plusone [1;2;3;4];; What is the result? [2;3;4;5]

Mark Hennessy Dept. Computer Science NUI Maynooth9 Currying Named after the logician Haskell Curry. This is the process whereby a function that takes many arguments is replaced by a function that expects a single argument and returns a function that expects the remaining arguments! Confused?

Mark Hennessy Dept. Computer Science NUI Maynooth10 Currying Example let myadd a b = a+b;; let inc = myadd 3;; inc 4;; What is the expected answer? 7! With currying, all functions “really” only take exactly one argument.

Mark Hennessy Dept. Computer Science NUI Maynooth11 Currying cont… let curryplus a = function b -> a + b;; int -> int -> int = I have defined an anonymous inner function to help with the addition. What is the result of curryplus 6;; Answer: int -> int = What the hell does this mean? We’ll discuss that in a minute!

Mark Hennessy Dept. Computer Science NUI Maynooth12 Currying cont… Currying allows the ability to partially apply a function! curryplus 6 on its own cannot obviously add 6 to anything but it is not an error to call curryplus. What is the result of curryplus 6 (sub 10 4);; Now, if I do this let curryplus6 = curryplus 6;; curryplus6 (curryplus 6 (sub 10 4));; Any guesses for the answer?

Mark Hennessy Dept. Computer Science NUI Maynooth13 CaML Type System let triadd(a,b,c) = a+b+c;; int * int * int -> int let add3 a b c = a+b+c;; int -> int -> int -> int The first takes a triple and returns an int, the second takes 3 ints and returns an int. So far so easy!

Mark Hennessy Dept. Computer Science NUI Maynooth14 CaML Type System What about let P x = x;; We get ‘a -> ‘a = What this means is that we take in some unknown type x, which is represented by ‘a and we return the exact same unknown type. What about let Q x = y;; ?

Mark Hennessy Dept. Computer Science NUI Maynooth15 CaML Type System CaML therefore allows a certain flexibility with types. As mentioned before we can use polymorphism with some of the inbuilt operators. In CaML, the = operator is polymorphic, it will work for many types. Consider: let Q x y = if x = y then “True” else “False”;; Q 4 5;; Q 5 5;; Q ;; Q “CS351” “crap”;;

Mark Hennessy Dept. Computer Science NUI Maynooth16 CaML Type System CaML can figure out the types for most expressions. let prog a b = if a > b then a(* Line 1 *) else “Not Greater”;;(* Line 2 *) Anything wrong with this program? No! The > operator is polymorphic for a and b on L1 The else part on L2 says the return type is string. What is the function header? What happens with prog 4 5;;

Mark Hennessy Dept. Computer Science NUI Maynooth17 CaML Type System The previous example shows how CaML infers the types in an expression. It also checks for type consistency. Consider: let prog a b = if a > b then 1 else “Not Equal” Here we have an error! The two returns are inconsistent being of different types, hence we get errors reported!

Mark Hennessy Dept. Computer Science NUI Maynooth18 CaML Type System CaML checks types via the following general rules: 1. All occurrences of the same identifier (subject to scope rules) must have the same type. 2. A programmer-defined function has type ’a -> ’b, where ’a is the type of the function’s parameter and ’b is the type of its result. 3. When a function is called, the type of the argument that is passed must be the same as the type of the parameter in the function’s definition. The result type is the same as the type of the result in the function’s definition.

Mark Hennessy Dept. Computer Science NUI Maynooth19 Reasoning about Types Recall let P x = x;; ‘a -> ‘a = let Q x y = x;; ‘a -> ‘b -> ‘a = What about the type of let R x y z = x z (y z);; (‘a->’b->’c) -> (‘a->’b) -> ‘a -> ‘c =