INF121: Functional Algorithmic and Programming

Similar documents
INF121: Functional Algorithmic and Programming

Lecture 5: Methods CS2301

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004

Object Oriented Methods : Deeper Look Lecture Three

CS110: PROGRAMMING LANGUAGE I

CSC324 Functional Programming Efficiency Issues, Parameter Lists

Selection statements. CSE 1310 Introduction to Computers and Programming Alexandra Stefan University of Texas at Arlington

Fall Recursion and induction. Stephen Brookes. Lecture 4

Module 3: New types of data

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

The design recipe. Readings: HtDP, sections 1-5. (ordering of topics is different in lectures, different examples will be used)

Recap: Functions as first-class values

Fundamentals of Programming

Computer Programming

CS313D: ADVANCED PROGRAMMING LANGUAGE. Lecture 3: C# language basics II

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

Dept. of CSE, IIT KGP

Module 10A Lecture - 20 What is a function? Why use functions Example: power (base, n)

Lecture 5 Tao Wang 1

CSc Introduction to Computing

2/3/2018 CS313D: ADVANCED PROGRAMMING LANGUAGE. Lecture 3: C# language basics II. Lecture Contents. C# basics. Methods Arrays. Dr. Amal Khalifa, Spr17

CSCI 131, Midterm Exam 1 Review Questions This sheet is intended to help you prepare for the first exam in this course. The following topics have

Fundamental of Programming (C)

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

Have the same meaning as variables in algebra Single alphabetic character Each variable needs an identifier that distinguishes it from the others a =

Programming in C. main. Level 2. Level 2 Level 2. Level 3 Level 3

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

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

A Simple Syntax-Directed Translator

Reviewing all Topics this term

Getting started with C++ (Part 2)

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

CS /534 Compiler Construction University of Massachusetts Lowell. NOTHING: A Language for Practice Implementation

Programming Languages

Logical Operators and if/else statement. If Statement. If/Else (4.3)

Special Directions for this Test

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

Chapter 11 :: Functional Languages

2.2 Syntax Definition

The SPL Programming Language Reference Manual

The PCAT Programming Language Reference Manual

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

Python allows variables to hold string values, just like any other type (Boolean, int, float). So, the following assignment statements are valid:

COMP 111 PROGRAMMING I MODULARITY USING FUNCTIONS

Introduction to Python (All the Basic Stuff)

Fundamentals of C Programming

Programmiersprachen (Programming Languages)

6.001 Notes: Section 6.1

Methods CSC 121 Fall 2016 Howard Rosenthal

2. You are required to enter a password of up to 100 characters. The characters must be lower ASCII, printing characters.

SAMPLE QUESTIONS FOR DIPLOMA IN INFORMATION TECHNOLOGY; YEAR 1

Project 2 Interpreter for Snail. 2 The Snail Programming Language

SCHEME AND CALCULATOR 5b

Programming in Java Prof. Debasis Samanta Department of Computer Science Engineering Indian Institute of Technology, Kharagpur

Algorithms and Programming I. Lecture#12 Spring 2015

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

CSE 130 [Winter 2014] Programming Languages

Begin at the beginning

A Brief Introduction to Scheme (I)

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

Pace University. Fundamental Concepts of CS121 1

Programming Languages & Compilers. Programming Languages and Compilers (CS 421) Programming Languages & Compilers. Major Phases of a Compiler

Curriculum Map Grade(s): Subject: AP Computer Science

Functions. Nate Foster Spring 2018

CSCI 2041: Functions, Mutation, and Arrays

Introduction to: Computers & Programming: Review prior to 1 st Midterm

1. What is the minimum number of bits needed to store a single piece of data representing: a. An integer between 0 and 100?

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

CS110D: PROGRAMMING LANGUAGE I

Control Structures in Java if-else and switch

PRG PROGRAMMING ESSENTIALS. Lecture 2 Program flow, Conditionals, Loops

Introduction to Programming in Turing. Input, Output, and Variables

Unit 3. Operators. School of Science and Technology INTRODUCTION

Math 302 Introduction to Proofs via Number Theory. Robert Jewett (with small modifications by B. Ćurgus)

Copied from: on 3/20/2017

CS1 Lecture 3 Jan. 18, 2019

CSc 372. Comparative Programming Languages. 4 : Haskell Basics. Department of Computer Science University of Arizona

An introduction introduction to functional functional programming programming using usin Haskell

Arrays. Comp Sci 1570 Introduction to C++ Array basics. arrays. Arrays as parameters to functions. Sorting arrays. Random stuff

Module 01: Introduction to Programming in Python

The return Statement

Java Methods. Lecture 8 COP 3252 Summer May 23, 2017

cis20.1 design and implementation of software applications I fall 2007 lecture # I.2 topics: introduction to java, part 1

Methods CSC 121 Spring 2017 Howard Rosenthal

CS-201 Introduction to Programming with Java

CS1 Lecture 3 Jan. 22, 2018

CPS 506 Comparative Programming Languages. Syntax Specification

PROGRAMMING IN HASKELL. Chapter 2 - First Steps

Array Basics: Outline. Creating and Accessing Arrays. Creating and Accessing Arrays. Arrays (Savitch, Chapter 7)

Chapter 5 Methods. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

Binghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program

BASIC ELEMENTS OF A COMPUTER PROGRAM

CpSc 111 Lab 3 Integer Variables, Mathematical Operations, & Redirection

CS 1200 Discrete Math Math Preliminaries. A.R. Hurson 323 CS Building, Missouri S&T

Programming Languages

CMSC 330: Organization of Programming Languages. OCaml Higher Order Functions

Methods CSC 121 Fall 2014 Howard Rosenthal

CMSC 330: Organization of Programming Languages. OCaml Higher Order Functions

CMSC 330: Organization of Programming Languages. Operational Semantics

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

Transcription:

INF121: Functional Algorithmic and Programming Lecture 2: Identifiers and functions Academic Year 2011-2012

Identifiers The notion of identifier A fundamental concept of programming languages: associating a value to a name (an identifier) Remark Close to the notion of variable but has fundamental differences! 1 / 11

Identifiers The notion of identifier A fundamental concept of programming languages: associating a value to a name (an identifier) Remark Close to the notion of variable but has fundamental differences! Some rules when defining identifiers: Maximal length: 256 characters Must begin with a non-capital letter No blanks Case-sensitive Should have a meaningful name 1 / 11

Identifiers The notion of identifier A fundamental concept of programming languages: associating a value to a name (an identifier) Remark Close to the notion of variable but has fundamental differences! Some rules when defining identifiers: Maximal length: 256 characters Must begin with a non-capital letter No blanks Case-sensitive Should have a meaningful name Example (Identifiers (valid and unvalid)) speed s Speed 3m average speed temporary3 average speed 1 / 11

Identifiers: Global definition Syntax of a global definition let identifier = expression the value of expression is bound/linked to identifier Type of the identifier is the type of the evaluated expression 2 / 11

Identifiers: Global definition Syntax of a global definition let identifier = expression the value of expression is bound/linked to identifier Type of the identifier is the type of the evaluated expression Definition is global: it can be used in other definitions in the rest of the program 2 / 11

Identifiers: Global definition Syntax of a global definition let identifier = expression the value of expression is bound/linked to identifier Type of the identifier is the type of the evaluated expression Definition is global: it can be used in other definitions in the rest of the program Simultaneous definitions: let ident1 = expr1 and ident2 = expr2... and identn = exprn 2 / 11

Identifiers: Global definition Syntax of a global definition let identifier = expression the value of expression is bound/linked to identifier Type of the identifier is the type of the evaluated expression Definition is global: it can be used in other definitions in the rest of the program Simultaneous definitions: let ident1 = expr1 and ident2 = expr2... and identn = exprn Example let x = 1 let y = 2 let i = 1 let i = i+1 DEMO: global definitions 2 / 11

Identifiers: Local definition Example (Motivating example) How to compute e=(2 3 4) (2 3 4)+(2 3 4)+2? prod= (2 3 4) e= prod prod + prod + 2 prod is local to e 3 / 11

Identifiers: Local definition Example (Motivating example) How to compute e=(2 3 4) (2 3 4)+(2 3 4)+2? prod= (2 3 4) e= prod prod + prod + 2 prod is local to e Syntax of a local definition: let identifier = expression1 in expression2 the value of expression1 is permanently bound/linked to identifier (only) when evaluating expression2 3 / 11

Identifiers: Local definition Example (Motivating example) How to compute e=(2 3 4) (2 3 4)+(2 3 4)+2? prod= (2 3 4) e= prod prod + prod + 2 prod is local to e Syntax of a local definition: let identifier = expression1 in expression2 the value of expression1 is permanently bound/linked to identifier (only) when evaluating expression2 Can be nested: Works with simultaneous definitions: let id1=expr1 in let id2=expr2 in... let idn = exprn... in expr let id1=expr1 and id2=expr2... and idn = exprn... in expr DEMO: local definitions 3 / 11

Functions Introduction So far, we have considered: expressions pre-defined operators Defining our own functions: a piece of code with a specific job 4 / 11

Functions Introduction So far, we have considered: expressions pre-defined operators Defining our own functions: a piece of code with a specific job Motivations: code readability its job can be more elaborated than the job of pre-defined functions being able to execute this code from several locations 4 / 11

Functions Introduction So far, we have considered: expressions pre-defined operators Defining our own functions: a piece of code with a specific job Motivations: code readability its job can be more elaborated than the job of pre-defined functions being able to execute this code from several locations Inputs Function Output args f f (args) 4 / 11

Functions Introduction So far, we have considered: expressions pre-defined operators Defining our own functions: a piece of code with a specific job Motivations: code readability its job can be more elaborated than the job of pre-defined functions being able to execute this code from several locations Inputs Function Output args f f (args) Functions in functional languages No side-effect (contrarily to C) Close to mathematical functions First-class objects: they are values they have a type 4 / 11

Functions: functions with one argument On an example Example (Absolute value from a mathematical/abstract point of view) Z N a if a < 0 then a else a 5 / 11

Functions: functions with one argument On an example Example (Absolute value from a mathematical/abstract point of view) Z N a if a < 0 then a else a Example (Absolute value in OCaml) or or fun a if a < 0 then a else a function a if a < 0 then a else a fun/function (a:int) if a < 0 then a else a 5 / 11

Functions: functions with one argument On an example Example (Absolute value from a mathematical/abstract point of view) Z N a if a < 0 then a else a Example (Absolute value in OCaml) or or fun a if a < 0 then a else a function a if a < 0 then a else a fun/function (a:int) if a < 0 then a else a keyword formal param. keyword function s body Analysis: fun a if a < 0 then a else a type: int -> int Remark This function is anonymous, i.e., it does not have a name DEMO: anonymous functions 5 / 11

Functions How to define them Naming a function allows to reuse it Example (Defining the function absolute value) or or or let abs = fun (a:int) if a < 0 then a else a let abs a = if a < 0 then a else a let abs (a:int) = if a < 0 then a else a let abs (a:int):int = if a < 0 then a else a DEMO: defining functions 6 / 11

Functions How to define them Naming a function allows to reuse it Example (Defining the function absolute value) or or or let abs = fun (a:int) if a < 0 then a else a let abs a = if a < 0 then a else a let abs (a:int) = if a < 0 then a else a let abs (a:int):int = if a < 0 then a else a DEMO: defining functions Exercise Define the function square: int int 6 / 11

Functions How to use them As in mathematics, the result of applying f to x is f (x) Example abs(2) abs(2 3) abs 2 (parenthesis can be omitted) 7 / 11

Functions How to use them As in mathematics, the result of applying f to x is f (x) Example abs(2) abs(2 3) abs 2 (parenthesis can be omitted) Application of a function expr1 expr2 Typing: if and expr1 has type t1->t2 expr2 has type t1 } then expr1 expr2 has type t2 7 / 11

Functions: Generalization to functions with several arguments Example (Surface area of a rectangle) Needs 2 parameters: length and width (floats) definition: let surface (x:float) (y:float):float = x. y let surface (length:float) (width:float):float = length. width usage: surface 2.3 1.2 8 / 11

Functions: Generalization to functions with several arguments Example (Surface area of a rectangle) Needs 2 parameters: length and width (floats) definition: let surface (x:float) (y:float):float = x. y let surface (length:float) (width:float):float = length. width usage: surface 2.3 1.2 Definition of a Function with n parameters let fct_name (p1:t1) (p2:t2)... (pn:tn) : t = expr p1,..., pn are formal parameters Type of fct_name is t1 -> t2 ->... -> tn -> t 8 / 11

Functions: Generalization to functions with several arguments Example (Surface area of a rectangle) Needs 2 parameters: length and width (floats) definition: let surface (x:float) (y:float):float = x. y let surface (length:float) (width:float):float = length. width usage: surface 2.3 1.2 Definition of a Function with n parameters let fct_name (p1:t1) (p2:t2)... (pn:tn) : t = expr p1,..., pn are formal parameters Type of fct_name is t1 -> t2 ->... -> tn -> t Using a Function with n parameters fct_name e1 e2... en e1,...,en are effective parameters Type of fct_name e1 e2... en is t if ti is the type of ei and fct_name is of type t1 -> t2 ->... -> tn -> t 8 / 11

Functions: SPECIFICATION and IMPLEMENTATION In this module (and in your future), it is very important to distinguish two concepts/stages about defining functions (and programs in general) 9 / 11

Functions: SPECIFICATION and IMPLEMENTATION In this module (and in your future), it is very important to distinguish two concepts/stages about defining functions (and programs in general) Specification: A description of what it is expected to do/ the job at an abstract level should be precise close to maths description in fun programming illustrate the function with some interesting examples A contract: Inputs Function Consists of: description signature examples Output 9 / 11

Functions: SPECIFICATION and IMPLEMENTATION In this module (and in your future), it is very important to distinguish two concepts/stages about defining functions (and programs in general) Specification: A description of what it is expected to do/ the job at an abstract level should be precise close to maths description in fun programming illustrate the function with some interesting examples A contract: Inputs Function Consists of: description signature examples Output Implementation: The description of how it is done Inputs Function Output the OCaml code 9 / 11

Functions: SPECIFICATION and IMPLEMENTATION In this module (and in your future), it is very important to distinguish two concepts/stages about defining functions (and programs in general) Specification: A description of what it is expected to do/ the job at an abstract level should be precise close to maths description in fun programming illustrate the function with some interesting examples A contract: Inputs Function Consists of: description signature examples Output Implementation: The description of how it is done the OCaml code Inputs Function Defining a function: Specification AND THEN Implementation Output 9 / 11

Functions: SPECIFICATION and IMPLEMENTATION In this module (and in your future), it is very important to distinguish two concepts/stages about defining functions (and programs in general) Specification: A description of what it is expected to do/ the job at an abstract level should be precise close to maths description in fun programming illustrate the function with some interesting examples A contract: Inputs Function Consists of: description signature examples Output Implementation: The description of how it is done the OCaml code Inputs Function Defining a function: Specification AND THEN Implementation Has many advantages (how big software is developed): re-usability you will save a lot of time thinking before acting you will have a better grade Output 9 / 11

Defining functions: some examples Example (Defining the function absolute value) Specification: The function absolute value abs takes an integer n as a parameter and returns n if this integer is positive or n if this integer is negative. The function absolute value always returns a positive integer. Signature: Z N Example: abs(1) = 1, abs(0) = 0, abs( 2) = 2 Implementation: let abs (a:int) = if a < 0 then a else a 10 / 11

Defining functions: some examples Example (Defining the function absolute value) Specification: The function absolute value abs takes an integer n as a parameter and returns n if this integer is positive or n if this integer is negative. The function absolute value always returns a positive integer. Signature: Z N Example: abs(1) = 1, abs(0) = 0, abs( 2) = 2 Implementation: let abs (a:int) = if a < 0 then a else a Example (Defining the function square) Specification: The function square sq takes an integer n as a parameter and returns n n. Signature: Z N Example: sq(1) = 1, sq(0) = 0, sq(3) = 9, sq( 4) = 16 Implementation: let sq (n:int) = n n 10 / 11

Some exercises A piece of algorithmic Exercise Define the function my_max returning the maximum of two integers 11 / 11

Some exercises A piece of algorithmic Exercise Define the function my_max returning the maximum of two integers Exercise Define functions: square: int int sum_square: int int int s.t. sum_square computes the sum of the squares of two numbers 11 / 11

Some exercises A piece of algorithmic Exercise Define the function my_max returning the maximum of two integers Exercise Define functions: square: int int sum_square: int int int s.t. sum_square computes the sum of the squares of two numbers Problem: Olympic mean Computing the mean of 4 grades (or values), by suppressing the highest and lowest one 1. Propose a type for the function mean 2. Propose an algorithm, by supposing that you have two functions min4 and max4, which compute respectively the minimum and the maximum of 4 integers 3. Define functions min4 et max4, using min and max 11 / 11