Functional Programming Lecture 2 - Functions

Slides:



Advertisements
Similar presentations
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 6 Functions.
Advertisements

C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
Eight compound procedures and higher-order procedures.
CIS 101: Computer Programming and Problem Solving Lecture 5 Usman Roshan Department of Computer Science NJIT.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 6: Functions by.
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,
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
Functional Programming Lecture 4 - More Lists Muffy Calder.
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.
Chapter 6: Functions Starting Out with C++ Early Objects
Lecture 5 Introduction to Programming in C Arne Kutzner Hanyang University / Seoul Korea.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
Making Decisions (True or False) Relational Operators >greater than =greater than or equal to
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
Introduction to Python and programming Ruth Anderson UW CSE 140 Winter
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 6 Functions.
Starting Out with C++ Early Objects ~~ 7 th Edition by Tony Gaddis, Judy Walters, Godfrey Muganda Modified for CMPS 1044 Midwestern State University 6-1.
Function Definition by Cases and Recursion Lecture 2, Programmeringsteknik del A.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 6 Functions.
3.4 Solving Equations with Variables on Both Sides Objective: Solve equations that have variables on both sides.
Lecture 2 Functions. Functions in C++ long factorial(int n) The return type is long. That means the function will return a long integer to the calling.
CSED101 INTRODUCTION TO COMPUTING FUNCTION ( 함수 ) 유환조 Hwanjo Yu.
Copyright Curt Hill The Assignment Operator and Statement The Most Common Statement you will use.
Guards1. 2 Let’s test this function. Main> maxi Here is a trace of the function: n m maxi 3 2 Guards or conditions are used to express various cases.
Lecture 4 – Function (Part 1) FTMK, UTeM – Sem /2014.
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Chapter 6 Functions. 6-2 Topics 6.1 Modular Programming 6.2 Defining and Calling Functions 6.3 Function Prototypes 6.4 Sending Data into a Function 6.5.
Boolean Algebra.
Copyright © Curt Hill The Assignment Operator and Statement The Most Common Statement you will use.
2.3 Output Formatting. Outputting Format Specify the number of spaces, “c”, used to print an integer value with specifier %cd, e.g., %3d, %4d. E.g. printf.
CS314 – Section 5 Recitation 9
Chapter # 2 Part 2 Programs And data
ECE 1304 Introduction to Electrical and Computer Engineering
Functions.
CSc 120 Introduction to Computer Programing II Adapted from slides by
EQUATION IN TWO VARIABLES:
Matlab Training Session 4: Control, Flow and Functions
… and now for the Final Topic:
Haskell.
Functional Programming Lecture 2 - Functions
CSE 3302 Programming Languages
Programmazione I a.a. 2017/2018.
User-Defined Functions
Main Points of Haskell Functional programming Laziness
Conditionals & Boolean Expressions
Conditionals & Boolean Expressions
Propositional Calculus: Boolean Algebra and Simplification
Solving Equations by 2-1 Adding or Subtracting Warm Up
Linear Equations Lesson 2.3.
CMSC Discrete Structures
If statement.
Conditions and Ifs BIS1523 – Lecture 8.
Chapter 6: Functions Starting Out with C++ Early Objects Ninth Edition
Solving Algebraic Equations
EQ: How do I solve an equation in one variable?
Yes, No, Maybe... BooleanAlgebra 12/10/2018.
CSCE 314: Programming Languages Dr. Dylan Shell
Material in the textbook Sections to 1.2.1
Lazy Programming Lazy evaluation:
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
CSE 3302 Programming Languages
SECTION 2-4 : SOLVING EQUATIONS WITH THE VARIABLE ON BOTH SIDES
CSCI N207 Data Analysis Using Spreadsheet
Relational Operators.
Life is Full of Alternatives
Algebra: Variables and Expressions
Solving Equations by 2-1 Adding or Subtracting Warm Up
CMSC Discrete Structures
Presentation transcript:

Functional Programming Lecture 2 - Functions Muffy Calder

Function Definitions f :: Int -> Int -- multiply argument by 100 f x = x*100 “f takes an Int and returns an Int; the value of f x is x*100” The mantra: comment, type, equation

Defining Equation of Function Fn_name arg1 arg2 … argn = expression that can use arg1…argn E.g. g x y = x + y >= 10

Evaluation or Simplification Given: f x y = x*y Evaluate: f 3 4 f 3 4 => 3 * 4 = 12 Now evaluate: f 3 (f 4 5) f 3 (f 4 5) => 3 * f 4 5 => 3 * (4*5) => 3 * 20 => 60 Note there is an alternative evaluation! Does it matter which one we pick? (Stay tuned)

Programs (Scripts) What is meant by “given”? A set of definitions is saved in a file. E.g. x = 2 y = 4 z = 100*x -30*y f :: Int -> Int -> Int f x y = x + 2*y You use a program by “asking” it to perform a calculation, e.g. calculate f 2 3. > f 2 3

Equations vs. Assignments Assignment - command to update a variable e.g. x:= x+1 x ------ ---- 3 Equation - permanent assertion that two values are equal e.g. x=3 or x = y+1 x ----- 3 You obey assignments and solve equations. Equations in a script can appear in any order!

:= and = Ada x := x+1 take the old value of x and increment it This is a common and useful operation. Haskell x = x+1 Assert that x and x+1 are the same value. This equation has no solution. It is a syntactically-valid statement, but it is not solvable. There is no solution for x. So this equation cannot be true!

Side Effects and Equational Reasoning Equations are timeless - there is no notion of changing the value of a variable Mathematics (algebra) is all about reasoning with equations, not with the contents of computer locations -- that keep changing.

Local functions: where Sometimes we want to make a function definition local (as opposed to global) For example: f x y = (g x) + y where g z = z + 32 The function g is local to the function f. Alternative: h x = (g x) + y In this case, the function g is available to other functions.

Local names: let expressions let x = 1 y = 2 z = 3 in x+y+z The variables x, y, z are local A script is just one big let expression.

A let expression is an expression! E.g. 2 + let a = x+y in (a+1)*(a-1) 2 + (let a = 5 in (a+1)*(a-1) ) + 3 - allows you to make some local definitions - it is not a block of statements to be executed.

Conditional expressions if boolean_exp then exp1 else exp2 Haskell evaluates boolean_exp and returns the appropriate expression. exp1 and exp2 must have the same type E.g. f x = if x>3 then x+2 else x-2 g x = if x>3 then x+2 else False (wrong) What are the types of f and g?

Guarded expressions f x | x>0 = True | otherwise = False In general: Name x1 x2 .. xn | guard1 = e1 | guard2 = e2 …. | otherwise = e Haskell evaluates the guards, from the top, and returns the appropriate expression. guard1, ... must have the Bool type exp1, ... must have the same type Note: | otherwise = 2 (wrong)

Examples positive :: Int -> Bool positive x | (x>=0) = True | otherwise = False or positive x = if (x>=0) then True else False max :: Int -> Int -> Int max x y | (x>=y) = x | otherwise = y max x y = if (x>=y) then x else y maxthree :: Int -> Int -> Int -> Int maxthree x y z | ((x>=y) && (x>=z) = x | (y>=z) = y | otherwise = z

Examples or maxthree x y z = max x (max y z) mystery :: Int -> Int -> Int -> Bool mystery m n p = not ((m==n) && (n==p)) Evaluate: mystery 0 1 2 mystery 0 1 1 mystery 1 1 1

Recursion Functions can be recursive (i.e. the function can occur on the rhs of the defining equation). This is fundamental to functional programming. Example fact :: Int -> Int fact 1 = 1 -- fact.0 fact n = n * (fact n-1) -- fact.1 So, fact 4 => 4 * (fact 3) => 4 * 3 * (fact 2) => 4 * 3 * 2 * (fact 1) => 4 * 3 * 2 * 1 => 24 fact.0 is called the base case.

Off-side Rule mystery :: Int -> Int -> Int -> Bool mystery m n p = not ((m==n) && (n==p)) the box Whatever is typed in the box is part of the definition. So, f :: Int -> Int f x = 42 --okay g :: Int -> Int --(start new definition) g x --okay = 36 h:: Int -> Int h y= 42 * --okay 1 hy = 42 -- will give an error (unexpected ‘;’)

Functional Data Structures Allow you to build aggregate objects from smaller values Built-in data structures Lists Tuples Arrays User defined data structures