# + addition - subtraction and unary minus * multiplication / division mod modulo

Save this PDF as:

Size: px
Start display at page:

## Transcription

1 Chapter 4 Basic types We examine in this chapter the Caml basic types. 4.1 Numbers Caml Light provides two numeric types: integers (type int) and floating-point numbers (type float). Integers are limited to the range Integer arithmetic is taken modulo 2 31 ; that is, an integer operation that overflows does not raise an error, but the result simply wraps around. Predefined operations (functions) on integers include: Some examples of expressions: #3 * 4 + 2;; - : int = 14 #3 * (4 + 2);; - : int = 18 # ;; - : int = -6 + addition - subtraction and unary minus * multiplication / division mod modulo There are precedence rules that make * bind tighter than +, and so on. In doubt, write extra parentheses. So far, we have not seen the type of these arithmetic operations. They all expect two integer arguments; so, they are functions taking one integer and returning a function from integers to integers. The (functional) value of such infix identifiers may be obtained by taking their prefix version. #prefix + ;; - : int -> int -> int = <fun> 21

2 22 CHAPTER 4. BASIC TYPES #prefix * ;; - : int -> int -> int = <fun> #prefix mod ;; - : int -> int -> int = <fun> As shown by their types, the infix operators +, *,..., do not work on floating-point values. A separate set of floating-point arithmetic operations is provided: +. addition -. subtraction and unary minus *. multiplication /. division sqrt square root exp, log exponentiation and logarithm sin, cos,... usual trigonometric operations We have two conversion functions to go back and forth between integers and floating-point numbers: int_of_float and float_of_int. # ;; Toplevel input: > ;; > ^^^ This expression has type float, but is used with type int. #float_of_int ;; - : float = 3.3 Let us now give some examples of numerical functions. We start by defining some very simple functions on numbers: #let square x = x *. x;; square : float -> float = <fun> #square 2.0;; - : float = 4.0 #square (2.0 /. 3.0);; - : float = #let sum_of_squares (x,y) = square(x) +. square(y);; sum_of_squares : float * float -> float = <fun> #let half_pi = /. 2.0 #in sum_of_squares(cos(half_pi), sin(half_pi));; - : float = 1.0

3 4.1. NUMBERS 23 We now develop a classical example: the computation of the root of a function by Newton s method. Newton s method can be stated as follows: if y is an approximation to a root of a function f, then: y f(y) f (y) is a better approximation, where f (y) is the derivative of f evaluated at y. For example, with f(x) = x 2 a, we have f (x) = 2x, and therefore: y f(y) f (y) = y y2 a = y + a y 2y 2 We can define a function deriv for approximating the derivative of a function at a given point by: #let deriv f x dx = (f(x+.dx) -. f(x)) /. dx;; deriv : (float -> float) -> float -> float -> float = <fun> Provided dx is sufficiently small, this gives a reasonable estimate of the derivative of f at x. The following function returns the absolute value of its floating point number argument. It uses the if... then... else conditional construct. #let abs x = if x >. 0.0 then x else -. x;; abs : float -> float = <fun> The main function, given below, uses three local functions. The first one, until, is an example of a recursive function: it calls itself in its body, and is defined using a let rec construct (rec shows that the definition is recursive). It takes three arguments: a predicate p on floats, a function change from floats to floats, and a float x. If p(x) is false, then until is called with last argument change(x), otherwise, x is the result of the whole call. We will study recursive functions more closely later. The two other auxiliary functions, satisfied and improve, take a float as argument and deliver respectively a boolean value and a float. The function satisfied asks wether the image of its argument by f is smaller than epsilon or not, thus testing wether y is close enough to a root of f. The function improve computes the next approximation using the formula given below. The three local functions are defined using a cascade of let constructs. The whole function is: #let newton f epsilon = # let rec until p change x = # if p(x) then x # else until p change (change(x)) in # let satisfied y = abs(f y) <. epsilon in # let improve y = y -. (f(y) /. (deriv f y epsilon)) #in until satisfied improve;; newton : (float -> float) -> float -> float -> float = <fun> Some examples of equation solving: #let square_root x epsilon = # newton (function y -> y*.y -. x) epsilon x

4 24 CHAPTER 4. BASIC TYPES #and cubic_root x epsilon = # newton (function y -> y*.y*.y -. x) epsilon x;; square_root : float -> float -> float = <fun> cubic_root : float -> float -> float = <fun> #square_root 2.0 1e-5;; - : float = #cubic_root 8.0 1e-5;; - : float = #2.0 *. (newton cos 1e-5 1.5);; - : float = Boolean values The presence of the conditional construct implies the presence of boolean values. The type bool is composed of two values true and false. #true;; #false;; The functions with results of type bool are often called predicates. Many predicates are predefined in Caml. Here are some of them: #prefix <;; - : a -> a -> bool = <fun> #1 < 2;; #prefix <.;; - : float -> float -> bool = <fun> # < ;; There exist also <=, >, >=, and similarly <=., >., >= Equality Equality has a special status in functional languages because of functional values. It is easy to test equality of values of base types (integers, floating-point numbers, booleans,... ): #1 = 2;; #false = (1>2);;

5 4.2. BOOLEAN VALUES 25 But it is impossible, in the general case, to decide the equality of functional values. Hence, equality stops on a run-time error when it encounters functional values. #(fun x -> x) = (fun x -> x);; Uncaught exception: Invalid_argument "equal: functional value" Since equality may be used on values of any type, what is its type? Equality takes two arguments of the same type (whatever type it is) and returns a boolean value. The type of equality is a polymorphic type, i.e. may take several possible forms. Indeed, when testing equality of two numbers, then its type is int -> int -> bool, and when testing equality of strings, its type is string -> string -> bool. #prefix =;; - : a -> a -> bool = <fun> The type of equality uses type variables, written a, b, etc. Any type can be substituted to type variables in order to produces specific instances of types. For example, substituting int for a above produces int -> int -> bool, which is the type of the equality function used on integer values. #1=2;; #(1,2) = (2,1);; #1 = (1,2);; Toplevel input: >1 = (1,2);; > ^^^ This expression has type int * int, but is used with type int Conditional Conditional expressions are of the form if e test then e 1 else e 2, where e test is an expression of type bool and e 1, e 2 are expressions possessing the same type. As an example, the logical negation could be written: #let negate a = if a then false else true;; negate : bool -> bool = <fun> #negate (1=2);;

6 26 CHAPTER 4. BASIC TYPES Logical operators The classical logical operators are available in Caml. Disjunction and conjunction are respectively written or and &: #true or false;; #(1<2) & (2>1);; The operators & and or are not functions. They should not be seen as regular functions, since they evaluate their second argument only if it is needed. For example, the or operator evaluates its second operand only if the first one evaluates to false. The behavior of these operators may be described as follows: Negation is predefined: #not true;; e 1 or e 2 is equivalent to if e 1 then true else e 2 e 1 & e 2 is equivalent to if e 1 then e 2 else false The not identifier receives a special treatment from the parser: the application not f x is recognized as not (f x) while f g x is identical to (f g) x. This special treatment explains why the functional value associated to not can be obtained only using the prefix keyword: #prefix not;; - : bool -> bool = <fun> 4.3 Strings and characters String constants (type string) are written between " characters (double-quotes). Single-character constants (type char) are written between characters (backquotes). The most used string operation is string concatenation, denoted by the ^ character. #"Hello" ^ " World!";; - : string = "Hello World!" #prefix ^;; - : string -> string -> string = <fun> Characters are ASCII characters: # a ;; - : char = a # \065 ;; - : char = A

7 4.4. TUPLES 27 and can be built from their ASCII code as in: #char_of_int 65;; - : char = A Other operations are available (sub_string, int_of_char, etc). manual [20] for complete information. See the Caml Light reference 4.4 Tuples Constructing tuples It is possible to combine values into tuples (pairs, triples,... ). The value constructor for tuples is the, character (the comma). We will often use parentheses around tuples in order to improve readability, but they are not strictly necessary. #1,2;; - : int * int = 1, 2 #1,2,3,4;; - : int * int * int * int = 1, 2, 3, 4 #let p = (1+2, 1<2);; p : int * bool = 3, true The infix * identifier is the type constructor for tuples. For instance, t 1 t 2 corresponds to the mathematical cartesian product of types t 1 and t 2. We can build tuples from any values: the tuple value constructor is generic Extracting pair components Projection functions are used to extract components of tuples. For pairs, we have: #fst;; - : a * b -> a = <fun> #snd;; - : a * b -> b = <fun> They have polymorphic types, of course, since they may be applied to any kind of pair. They are predefined in the Caml system, but could be defined by the user (in fact, the cartesian product itself could be defined see section 6.1, dedicated to user-defined product types): #let first (x,y) = x #and second (x,y) = y;; first : a * b -> a = <fun> second : a * b -> b = <fun> #first p;; - : int = 3 #second p;;

8 28 CHAPTER 4. BASIC TYPES We say that first is a generic value because it works uniformly on several kinds of arguments (provided they are pairs). We often confuse between generic and polymorphic, saying that such value is polymorphic instead of generic. 4.5 Patterns and pattern-matching Patterns and pattern-matching play an important role in ML languages. They appear in all real programs and are strongly related to types (predefined or user-defined). A pattern indicates the shape of a value. Patterns are values with holes. A single variable (formal parameter) is a pattern (with no shape specified: it matches any value). When a value is matched against a pattern (this is called pattern-matching), the pattern acts as a filter. We already used patterns and pattern-matching in all the functions we wrote: the function body (function x ->...) does (trivial) pattern-matching. When applied to a value, the formal parameter x gets bound to that value. The function body (function (x,y) -> x+y) does also pattern-matching: when applied to a value (a pair, because of well-typing hypotheses), the x and y get bound respectively to the first and the second component of that pair. All these pattern-matching examples were trivial ones, they did not involve any test: formal parameters such as x do not impose any particular shape to the value they are supposed to match; pair patterns such as (x,y) always match for typing reasons (cartesian product is a product type). However, some types do not guarantee such a uniform shape to their values. Consider the bool type: an element of type bool is either true or false. If we impose to a value of type bool to have the shape of true, then pattern-matching may fail. Consider the following function: #let f = function true -> false;; Toplevel input: >let f = function true -> false;; > ^^^^^^^^^^^^^^^^^^^^^^ Warning: this matching is not exhaustive. f : bool -> bool = <fun> The compiler warns us that the pattern-matching may fail (we did not consider the false case). Thus, if we apply f to a value that evaluates to true, pattern-matching will succeed (an equality test is performed during execution). #f (1<2);; But, if f s argument evaluates to false, a run-time error is reported: #f (1>2);; Uncaught exception: Match_failure ("", 1346, 1368) Here is a correct definition using pattern-matching:

9 4.5. PATTERNS AND PATTERN-MATCHING 29 #let negate = function true -> false # false -> true;; negate : bool -> bool = <fun> The pattern-matching has now two cases, separated by the character. Cases are examined in turn, from top to bottom. An equivalent definition of negate would be: #let negate = function true -> false # x -> true;; negate : bool -> bool = <fun> The second case now matches any boolean value (in fact, only false since true has been caught by the first match case). When the second case is chosen, the identifier x gets bound to the argument of negate, and could be used in the right-hand part of the match case. Since in our example we do not use the value of the argument in the right-hand part of the second match case, another equivalent definition of negate would be: #let negate = function true -> false # _ -> true;; negate : bool -> bool = <fun> Where _ acts as a formal paramenter (matches any value), but does not introduce any binding: it should be read as anything else. As an example of pattern-matching, consider the following function definition (truth value table of implication): #let imply = function (true,true) -> true # (true,false) -> false # (false,true) -> true # (false,false) -> true;; imply : bool * bool -> bool = <fun> Here is another way of defining imply, by using variables: #let imply = function (true,x) -> x # (false,x) -> true;; imply : bool * bool -> bool = <fun> Simpler, and simpler again: #let imply = function (true,x) -> x # (false,_) -> true;; imply : bool * bool -> bool = <fun> #let imply = function (true,false) -> false # _ -> true;; imply : bool * bool -> bool = <fun> Pattern-matching is allowed on any type of value (non-trivial pattern-matching is only possible on types with data constructors). For example:

10 30 CHAPTER 4. BASIC TYPES #let is_zero = function 0 -> true _ -> false;; is_zero : int -> bool = <fun> #let is_yes = function "oui" -> true # "si" -> true # "ya" -> true # "yes" -> true # _ -> false;; is_yes : string -> bool = <fun> 4.6 Functions The type constructor -> is predefined and cannot be defined in ML s type system. We shall explore in this section some further aspects of functions and functional types Functional composition Functional composition is easily definable in Caml. It is of course a polymorphic function: #let compose f g = function x -> f (g (x));; compose : ( a -> b) -> ( c -> a) -> c -> b = <fun> The type of compose contains no more constraints than the ones appearing in the definition: in a sense, it is the most general type compatible with these constraints. These constraints are: the codomain of g and the domain of f must be the same; x must belong to the domain of g; compose f g x will belong to the codomain of f. Let s see compose at work: #let succ x = x+1;; succ : int -> int = <fun> #compose succ list_length;; - : _a list -> int = <fun> #(compose succ list_length) [1;2;3];; - : int = Currying We can define two versions of the addition function:

11 4.6. FUNCTIONS 31 #let plus = function (x,y) -> x+y;; plus : int * int -> int = <fun> #let add = function x -> (function y -> x+y);; add : int -> int -> int = <fun> These two functions differ only in their way of taking their arguments. The first one will take an argument belonging to a cartesian product, the second one will take a number and return a function. The add function is said to be the curried version of plus (in honor of the logician Haskell Curry). The currying transformation may be written in Caml under the form of a higher-order function: #let curry f = function x -> (function y -> f(x,y));; curry : ( a * b -> c) -> a -> b -> c = <fun> Its inverse function may be defined by: #let uncurry f = function (x,y) -> f x y;; uncurry : ( a -> b -> c) -> a * b -> c = <fun> We may check the types of curry and uncurry on particular examples: #uncurry (prefix +);; - : int * int -> int = <fun> #uncurry (prefix ^);; - : string * string -> string = <fun> #curry plus;; - : int -> int -> int = <fun> Exercises Exercise 4.1 Define functions that compute the surface area and the volume of well-known geometric objects (rectangles, circles, spheres,... ). Exercise 4.2 What would happen in a language submitted to call-by-value with recursion if there was no conditional construct (if... then... else...)? Exercise 4.3 Define the factorial function such that: factorial n = n (n 1) Give two versions of factorial: recursive and tail recursive. Exercise 4.4 Define the fibonacci function that, when given a number n, returns the nth Fibonacci number, i.e. the nth term u n of the sequence defined by: u 1 = 1 u 2 = 1 u n+2 = u n+1 + u n

12 32 CHAPTER 4. BASIC TYPES Exercise 4.5 What are the types of the following expressions? uncurry compose compose curry uncurry compose uncurry curry

### Product types are finite labeled products of types. They are a generalization of cartesian product. Elements of product types are called records.

Chapter 6 User-defined types The user is allowed to define his/her own data types. With this facility, there is no need to encode the data structures that must be manipulated by a program into lists (as

### Regular Expressions and Automata using Haskell

Regular Expressions and Automata using Haskell Simon Thompson Computing Laboratory University of Kent at Canterbury January 2000 Contents 1 Introduction 2 2 Regular Expressions 2 3 Matching regular expressions

### School of Informatics, University of Edinburgh

CS1Ah Lecture Note 5 Java Expressions Many Java statements can contain expressions, which are program phrases that tell how to compute a data value. Expressions can involve arithmetic calculation and method

### Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

Name: Class: Date: Exam #1 - Prep True/False Indicate whether the statement is true or false. 1. Programming is the process of writing a computer program in a language that the computer can respond to

VistaPro User Guide Custom Variable Expressions IES Virtual Environment Copyright 2015 Integrated Environmental Solutions Limited. All rights reserved. No part of the manual is to be copied or reproduced

### Joy: Forth s Functional Cousin

Joy: Forth s Functional Cousin Manfred von Thun 9th October 2001 1 Synopsis of the language Joy The language Joy is a purely functional programming language. Whereas all other functional programming languages

### Dept. of CSE, IIT KGP

Programming in C: Basics CS10001: Programming & Data Structures Pallab Dasgupta Professor, Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur Types of variable We must declare the

### Primitive Data Types Summer 2010 Margaret Reid-Miller

Primitive Data Types 15-110 Summer 2010 Margaret Reid-Miller Data Types Data stored in memory is a string of bits (0 or 1). What does 1000010 mean? 66? 'B'? 9.2E-44? How the computer interprets the string

### CHAPTER 2. Inequalities

CHAPTER 2 Inequalities In this section we add the axioms describe the behavior of inequalities (the order axioms) to the list of axioms begun in Chapter 1. A thorough mastery of this section is essential

### Compilation 2013 SML Basics

Compilation 2013 SML Basics Erik Ernst Aarhus University Revised from slides by Jan Midtgaard SML, top-down SML is a functional language Functions are first class (like in Scheme) The core syntactic category

### Introduction to SML Basic Types, Tuples and Records

Introduction to SML Basic Types, Tuples and Records Michael R. Hansen mrh@imm.dtu.dk Informatics and Mathematical Modelling Technical University of Denmark c Michael R. Hansen, Fall 2004 p.1/21 Overview

### A First Book of C++ Chapter 2 Data Types, Declarations, and Displays

A First Book of C++ Chapter 2 Data Types, Declarations, and Displays Objectives In this chapter, you will learn about: Data Types Arithmetic Operators Variables and Declarations Common Programming Errors

### Introduction to Python

Caltech/LEAD Summer 2012 Computer Science Lecture 2: July 10, 2012 Introduction to Python The Python shell Outline Python as a calculator Arithmetic expressions Operator precedence Variables and assignment

### The PCAT Programming Language Reference Manual

The PCAT Programming Language Reference Manual Andrew Tolmach and Jingke Li Dept. of Computer Science Portland State University (revised October 8, 2004) 1 Introduction The PCAT language (Pascal Clone

### CHAPTER 3. Mapping Concepts and Mapping Problems for. Scalar Valued Functions of a Scalar Variable

A SERIES OF CLASS NOTES TO INTRODUCE LINEAR AND NONLINEAR PROBLEMS TO ENGINEERS, SCIENTISTS, AND APPLIED MATHEMATICIANS REMEDIAL CLASS NOTES A COLLECTION OF HANDOUTS FOR REMEDIATION IN FUNDAMENTAL CONCEPTS

### We will learn the Python programming language. Why? Because it is easy to learn and many people write programs in Python so we can share.

LING115 Lecture Note Session #4 Python (1) 1. Introduction As we have seen in previous sessions, we can use Linux shell commands to do simple text processing. We now know, for example, how to count words.

### 2. Methods of Proof Types of Proofs. Suppose we wish to prove an implication p q. Here are some strategies we have available to try.

2. METHODS OF PROOF 69 2. Methods of Proof 2.1. Types of Proofs. Suppose we wish to prove an implication p q. Here are some strategies we have available to try. Trivial Proof: If we know q is true then

### Introduction to C++ Programming

Introduction to C++ Programming 1 Outline Introduction to C++ Programming A Simple Program: Printing a Line of Text Another Simple Program: Adding Two Integers Memory Concepts Arithmetic Decision Making:

### SML Functions. SML functions are values. 1. defining functions. 2. multiple argument functions. 3. higher-order functions. 4.

SML Functions SML functions are values. 1. defining functions 2. multiple argument functions 3. higher-order functions 4. currying 5. polymorphic functions 1 Defining Functions Defining integer values,

### Anatomy of Programming Languages. William R. Cook

Anatomy of Programming Languages William R. Cook Copyright (C) 2013 2 Chapter 1 Preliminaries Preface What? This document is a series of notes about programming languages, originally written for students

### Chapter 2: Problem Solving Using C++

Chapter 2: Problem Solving Using C++ 1 Objectives In this chapter, you will learn about: Modular programs Programming style Data types Arithmetic operations Variables and declaration statements Common

### Journal of Functional Programming, volume 3 number 4, 1993. Dynamics in ML

Journal of Functional Programming, volume 3 number 4, 1993. Dynamics in ML Xavier Leroy École Normale Supérieure Michel Mauny INRIA Rocquencourt Abstract Objects with dynamic types allow the integration

### ML for the Working Programmer

ML for the Working Programmer 2nd edition Lawrence C. Paulson University of Cambridge CAMBRIDGE UNIVERSITY PRESS CONTENTS Preface to the Second Edition Preface xiii xv 1 Standard ML 1 Functional Programming

### Introduction to Programming (in C++) Data types and visibility. Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. Computer Science, UPC

Introduction to Programming (in C++) Data types and visibility Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. Computer Science, UPC Outline Data types Type conversion Visibility Introduction to

### Chapter 7: Functional Programming Languages

Chapter 7: Functional Programming Languages Aarne Ranta Slides for the book Implementing Programming Languages. An Introduction to Compilers and Interpreters, College Publications, 2012. Fun: a language

### Cartesian Products and Relations

Cartesian Products and Relations Definition (Cartesian product) If A and B are sets, the Cartesian product of A and B is the set A B = {(a, b) :(a A) and (b B)}. The following points are worth special

### Elementary Number Theory and Methods of Proof. CSE 215, Foundations of Computer Science Stony Brook University http://www.cs.stonybrook.

Elementary Number Theory and Methods of Proof CSE 215, Foundations of Computer Science Stony Brook University http://www.cs.stonybrook.edu/~cse215 1 Number theory Properties: 2 Properties of integers (whole

### Induction and Recursion

Induction and Recursion Jan van Eijck June 3, 2003 Abstract A very important proof method that is not covered by the recipes from Chapter 3 is the method of proof by Mathematical Induction. Roughly speaking,

### Borland C++ Compiler: Operators

Introduction Borland C++ Compiler: Operators An operator is a symbol that specifies which operation to perform in a statement or expression. An operand is one of the inputs of an operator. For example,

### 1 Some mathematical preliminaries: sets, relations and functions

Bengt Nordström, November 4, 2013 Department of Computing Science, Chalmers and University of Göteborg, Göteborg, Sweden 1 Some mathematical preliminaries: sets, relations and functions 1.1 Propositions,

### Variables and constants linked with operators. Uses relational and logical operators Evaluates to 1 or 0 (true or false) only

Expressions 1 Expressions Variables and constants linked with operators Arithmetic expressions Uses arithmetic operators Can evaluate to any value Logical expressions Uses relational and logical operators

### 6.096 Lab 1. Due: 7 January 12:30:00. September 20, 2011

6.096 Lab 1 Due: 7 January 1:30:00 September 0, 011 1 Additional Material 1.1 Constants A constant is an expressions with a fixed value. Kinds of constants: Literals: used to express particular values

### Today s topics. Java programs. Java Virtual Machine (JVM) Bytecodes. HelloWorld.java javac HelloWorld.class

Today s topics Java programs Parsing Java Programming Notes from Tammy Bailey Reading Great Ideas, Chapter 3 & 4 Java programs are created as text files using a text editor (like emacs) Save to disk with.java

### The Decaf Language. The following are keywords. They are all reserved, which means they cannot be used as identifiers or redefined.

The Decaf Language In this course, we will write a compiler for a simple object-oriented programming language called Decaf. Decaf is a strongly-typed, object-oriented language with support for inheritance

### Table 3.1: Arithmetic operators

26 Chapter 3 Arithmetic Expressions Java programs can solve mathematical problems. Often programs that appear nonmathematical on the surface perform a large number of mathematical calculations behind the

### Exam objectives. Java Certification - Week 2. Operators and Assignments. Chris Harris. Overview. Exam objectives (continued)

Exam objectives Java Certification - Week 2 Operators and Assignments Chris Harris Determine the result of applying any operator,including assignment operators,instance of,and casts to operands of any

### SCHOOL OF ENGINEERING & BUILT ENVIRONMENT. Mathematics. Numbers

SCHOOL OF ENGINEERING & BUILT ENVIRONMENT Mathematics Numbers. Factors 2. Multiples 3. Prime and Composite Numbers 4. Modular Arithmetic 5. Boolean Algebra 6. Modulo 2 Matrix Arithmetic 7. Number Systems

### Functional Programming. Functional Programming Languages. Chapter 14. Introduction

Functional Programming Languages Chapter 14 Introduction Functional programming paradigm History Features and concepts Examples: Lisp ML 1 2 Functional Programming Functional Programming Languages The

### MATH 55: HOMEWORK #2 SOLUTIONS

MATH 55: HOMEWORK # SOLUTIONS ERIC PETERSON * 1. SECTION 1.5: NESTED QUANTIFIERS 1.1. Problem 1.5.8. Determine the truth value of each of these statements if the domain of each variable consists of all

### Chapter 3. Cartesian Products and Relations. 3.1 Cartesian Products

Chapter 3 Cartesian Products and Relations The material in this chapter is the first real encounter with abstraction. Relations are very general thing they are a special type of subset. After introducing

### The design recipe. Programs as communication. Some goals for software design. Readings: HtDP, sections 1-5

The design recipe Readings: HtDP, sections 1-5 (ordering of topics is different in lectures, different examples will be used) Survival and Style Guides CS 135 Winter 2016 02: The design recipe 1 Programs

### Sets. A set is a collection of (mathematical) objects, with the collection treated as a single mathematical object.

Sets 1 Sets Informally: A set is a collection of (mathematical) objects, with the collection treated as a single mathematical object. Examples: real numbers, complex numbers, C integers, All students in

### C++ Language Tutorial

cplusplus.com C++ Language Tutorial Written by: Juan Soulié Last revision: June, 2007 Available online at: http://www.cplusplus.com/doc/tutorial/ The online version is constantly revised and may contain

### Section 1. Statements and Truth Tables. Definition 1.1: A mathematical statement is a declarative sentence that is true or false, but not both.

M3210 Supplemental Notes: Basic Logic Concepts In this course we will examine statements about mathematical concepts and relationships between these concepts (definitions, theorems). We will also consider

### C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 2: Basic Elements of C++

C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Basic Elements of C++ Objectives In this chapter, you will: Become familiar with the basic components of a C++ program,

### Outline TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA. What is DrJava? Dr Java

1 Outline TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B. Ericson, and instructor materials prepared

### Mathematics Review for MS Finance Students

Mathematics Review for MS Finance Students Anthony M. Marino Department of Finance and Business Economics Marshall School of Business Lecture 1: Introductory Material Sets The Real Number System Functions,

### 8 Primes and Modular Arithmetic

8 Primes and Modular Arithmetic 8.1 Primes and Factors Over two millennia ago already, people all over the world were considering the properties of numbers. One of the simplest concepts is prime numbers.

### INTRODUCTION TO PROOFS: HOMEWORK SOLUTIONS

INTRODUCTION TO PROOFS: HOMEWORK SOLUTIONS STEVEN HEILMAN Contents 1. Homework 1 1 2. Homework 2 6 3. Homework 3 10 4. Homework 4 16 5. Homework 5 19 6. Homework 6 21 7. Homework 7 25 8. Homework 8 28

### Higher-Order Logic. Specification and Verification with Higher-Order Logic

Higher-Order Logic Specification and Verification with Higher-Order Logic Arnd Poetzsch-Heffter Software Technology Group Fachbereich Informatik Technische Universität Kaiserslautern Sommersemester 2010

### Introduction to Objective Caml

Introduction to Objective Caml Jason Hickey DRAFT. DO NOT REDISTRIBUTE. Copyright Jason Hickey, 2008. This book has been submitted for publication by Cambridge University Press. This draft may be used

### MA2C03 Mathematics School of Mathematics, Trinity College Hilary Term 2016 Lecture 59 (April 1, 2016) David R. Wilkins

MA2C03 Mathematics School of Mathematics, Trinity College Hilary Term 2016 Lecture 59 (April 1, 2016) David R. Wilkins The RSA encryption scheme works as follows. In order to establish the necessary public

### Chapter 1. Logic and Proof

Chapter 1. Logic and Proof 1.1 Remark: A little over 100 years ago, it was found that some mathematical proofs contained paradoxes, and these paradoxes could be used to prove statements that were known

### Inf1A: Deterministic Finite State Machines

Lecture 2 InfA: Deterministic Finite State Machines 2. Introduction In this lecture we will focus a little more on deterministic Finite State Machines. Also, we will be mostly concerned with Finite State

### Operators and Expressions

Chapter 5 Operators and Expressions The purspose of computing is insight, not numbers. Richard Hamming Chapter Objectives ˆ Learn Python s arithmetic, string, relational, logical, bit wise operators ˆ

### Adjusted/Modified by Nicole Tobias. Chapter 2: Basic Elements of C++

Adjusted/Modified by Nicole Tobias Chapter 2: Basic Elements of C++ Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types

### Functional Programming

FP 2005 1.1 3 Functional Programming WOLFRAM KAHL kahl@mcmaster.ca Department of Computing and Software McMaster University FP 2005 1.2 4 What Kinds of Programming Languages are There? Imperative telling

### TI-83 Plus Graphing Calculator Keystroke Guide

TI-83 Plus Graphing Calculator Keystroke Guide In your textbook you will notice that on some pages a key-shaped icon appears next to a brief description of a feature on your graphing calculator. In this

### MATHEMATICS (CLASSES XI XII)

MATHEMATICS (CLASSES XI XII) General Guidelines (i) All concepts/identities must be illustrated by situational examples. (ii) The language of word problems must be clear, simple and unambiguous. (iii)

### Domain Theory: An Introduction

Domain Theory: An Introduction Robert Cartwright Rebecca Parsons Rice University This monograph is an unauthorized revision of Lectures On A Mathematical Theory of Computation by Dana Scott [3]. Scott

### INTRODUCTORY SET THEORY

M.Sc. program in mathematics INTRODUCTORY SET THEORY Katalin Károlyi Department of Applied Analysis, Eötvös Loránd University H-1088 Budapest, Múzeum krt. 6-8. CONTENTS 1. SETS Set, equal sets, subset,

### LINEAR RECURSIVE SEQUENCES. The numbers in the sequence are called its terms. The general form of a sequence is. a 1, a 2, a 3,...

LINEAR RECURSIVE SEQUENCES BJORN POONEN 1. Sequences A sequence is an infinite list of numbers, like 1) 1, 2, 4, 8, 16, 32,.... The numbers in the sequence are called its terms. The general form of a sequence

### Cognitive Abilities Test Practice Activities. Teacher Guide. Form 7. Quantitative Tests. Level. Cog

Cognitive Abilities Test Practice Activities Teacher Guide Form 7 Quantitative Tests Level 9 Cog Test 4: Number Analogies, Level 9 Part 1: Overview of Number Analogies An analogy draws parallels between

### Problem Set 1: Program Verification

Problem Set 1: Program Verification CS 173 Honors Section Assigned: September 7, 2011 Due on: September 14, 2011 One of the applications of logic in computer science is its use in proving programs to be

### Chapter 5 Type Declarations

Plan Chapter 5 Type Declarations (Version of 27 September 2004) 1. Renaming existing types....................... 5.2 2. Enumeration types............................. 5.3 3. Constructed types..............................

### COMPUTER SKILLS LESSON 3

COMPUTER SKILLS LESSON 3 Prof. Valeria Cardellini cardellini@ing.uniroma2.it Prof. Emiliano Casalicchio casalicchio@ing.uniroma2.it Computer Skills - Lesson 3 - V. Cardellini 2 The professors Valeria Cardellini,

### Mathematical induction & Recursion

CS 441 Discrete Mathematics for CS Lecture 15 Mathematical induction & Recursion Milos Hauskrecht milos@cs.pitt.edu 5329 Sennott Square Proofs Basic proof methods: Direct, Indirect, Contradiction, By Cases,

Mathematics 0N1 Solutions 1 1. Write the following sets in list form. 1(i) The set of letters in the word banana. {a, b, n}. 1(ii) {x : x 2 + 3x 10 = 0}. 3(iv) C A. True 3(v) B = {e, e, f, c}. True 3(vi)

### 2.4 Multiplication of Integers. Recall that multiplication is defined as repeated addition from elementary school. For example, 5 6 = 6 5 = 30, since:

2.4 Multiplication of Integers Recall that multiplication is defined as repeated addition from elementary school. For example, 5 6 = 6 5 = 30, since: 5 6=6+6+6+6+6=30 6 5=5+5+5+5+5+5=30 To develop a rule

### MAT2400 Analysis I. A brief introduction to proofs, sets, and functions

MAT2400 Analysis I A brief introduction to proofs, sets, and functions In Analysis I there is a lot of manipulations with sets and functions. It is probably also the first course where you have to take

### WUCT121. Discrete Mathematics. Logic

WUCT121 Discrete Mathematics Logic 1. Logic 2. Predicate Logic 3. Proofs 4. Set Theory 5. Relations and Functions WUCT121 Logic 1 Section 1. Logic 1.1. Introduction. In developing a mathematical theory,

### Sets and functions. {x R : x > 0}.

Sets and functions 1 Sets The language of sets and functions pervades mathematics, and most of the important operations in mathematics turn out to be functions or to be expressible in terms of functions.

### CS 2336 Discrete Mathematics

CS 2336 Discrete Mathematics Lecture 2 Logic: Predicate Calculus 1 Outline Predicates Quantifiers Binding Applications Logical Equivalences 2 Predicates In mathematics arguments, we will often see sentences

### C library functions Trig functions and the like used to require expr, but in Max 4 some are available as objects.

Advanced Math in Max Eventually we will need to do something more complicated than the ordinary math objects allow. This is not a tutorial on how to do that math, but a guide to where the math can be done.

### SAT Subject Math Level 2 Facts & Formulas

Numbers, Sequences, Factors Integers:..., -3, -2, -1, 0, 1, 2, 3,... Reals: integers plus fractions, decimals, and irrationals ( 2, 3, π, etc.) Order Of Operations: Arithmetic Sequences: PEMDAS (Parentheses

### Chapter 15 Functional Programming Languages

Chapter 15 Functional Programming Languages Introduction - The design of the imperative languages is based directly on the von Neumann architecture Efficiency (at least at first) is the primary concern,

### Java: Primitive Data Types, Variables and Constants

Java: Primitive Data Types, Variables and Constants Introduction A primitive data type is a data type provided as a basic building block by a programming language. It is predefined by the programming language

### Python 3 and IDLE. We will use version 3.x of Python (where x is the most recent version

Just Enough Python Python 3 and IDLE We will use version 3.x of Python (where x is the most recent version Differences between Python 2 and Python 3 are mostly minor, but can be confusing Python comes

### 1. R In this and the next section we are going to study the properties of sequences of real numbers.

+a 1. R In this and the next section we are going to study the properties of sequences of real numbers. Definition 1.1. (Sequence) A sequence is a function with domain N. Example 1.2. A sequence of real

### CMPSCI 119 Python Review Sheet Professor William T. Verts. line break (can contain " or '). N = [1, 2.3, "Hello"]

Variables, Data Types, Computations, and Assignment variable = expression Variable is assigned the value on right, data type of variable becomes data type of value (variables may change type). N = 5 Variable

### Mathematical Induction

Mathematical Induction Victor Adamchik Fall of 2005 Lecture 2 (out of three) Plan 1. Strong Induction 2. Faulty Inductions 3. Induction and the Least Element Principal Strong Induction Fibonacci Numbers

### F U N C T I O N A L P E A R L S Monadic Parsing in Haskell

Under consideration for publication in J. Functional Programming 1 F U N C T I O N A L P E A R L S Monadic Parsing in Haskell Graham Hutton University of Nottingham Erik Meijer University of Utrecht 1

### 1.3 Induction and Other Proof Techniques

4CHAPTER 1. INTRODUCTORY MATERIAL: SETS, FUNCTIONS AND MATHEMATICAL INDU 1.3 Induction and Other Proof Techniques The purpose of this section is to study the proof technique known as mathematical induction.

### Math/CSE 1019: Discrete Mathematics for Computer Science Fall Suprakash Datta

Math/CSE 1019: Discrete Mathematics for Computer Science Fall 2011 Suprakash Datta datta@cse.yorku.ca Office: CSEB 3043 Phone: 416-736-2100 ext 77875 Course page: http://www.cse.yorku.ca/course/1019 1

### Introduction to C Programming

Introduction to C Programming C HOW TO PROGRAM, 6/E 1992-2010 by Pearson Education, Inc. All Rights Reserved. 2.1 Introduction The C language facilitates a structured and disciplined approach to computer

### SOME EXCEL FORMULAS AND FUNCTIONS

SOME EXCEL FORMULAS AND FUNCTIONS About calculation operators Operators specify the type of calculation that you want to perform on the elements of a formula. Microsoft Excel includes four different types

### Introducing Functions

Functions 1 Introducing Functions A function f from a set A to a set B, written f : A B, is a relation f A B such that every element of A is related to one element of B; in logical notation 1. (a, b 1

### CHAPTER 2. Logic. 1. Logic Definitions. Notation: Variables are used to represent propositions. The most common variables used are p, q, and r.

CHAPTER 2 Logic 1. Logic Definitions 1.1. Propositions. Definition 1.1.1. A proposition is a declarative sentence that is either true (denoted either T or 1) or false (denoted either F or 0). Notation:

### Chapter 5 Names, Bindings, Type Checking, and Scopes

Chapter 5 Names, Bindings, Type Checking, and Scopes Chapter 5 Topics Introduction Names Variables The Concept of Binding Type Checking Strong Typing Scope Scope and Lifetime Referencing Environments Named

### Rational Exponents. Squaring both sides of the equation yields. and to be consistent, we must have

8.6 Rational Exponents 8.6 OBJECTIVES 1. Define rational exponents 2. Simplify expressions containing rational exponents 3. Use a calculator to estimate the value of an expression containing rational exponents

### Mathematics for Computer Science/Software Engineering. Notes for the course MSM1F3 Dr. R. A. Wilson

Mathematics for Computer Science/Software Engineering Notes for the course MSM1F3 Dr. R. A. Wilson October 1996 Chapter 1 Logic Lecture no. 1. We introduce the concept of a proposition, which is a statement

### Simple C++ Programs. Engineering Problem Solving with C++, Etter/Ingber. Dev-C++ Dev-C++ Windows Friendly Exit. The C++ Programming Language

Simple C++ Programs Engineering Problem Solving with C++, Etter/Ingber Chapter 2 Simple C++ Programs Program Structure Constants and Variables C++ Operators Standard Input and Output Basic Functions from

### User-defined Functions (UDFs)

User-defined Functions (UDFs) In this lecture I will be introducing the programmation language Visual Basic for Applications (VBA) and explaining one particular use of it: creating UDFs. VBA is a powerful

### 21st Annual Iowa Collegiate Mathematics Competition ISU, Saturday, February 21, 2015 Problems by Razvan Gelca

21st Annual Iowa Collegiate Mathematics Competition ISU, Saturday, February 21, 215 Problems by Razvan Gelca SOLUTIONS 1. Asked about his age, a boy replied: I have a sister, and four years ago when she

### TI-86 Graphing Calculator Keystroke Guide

TI-86 Graphing Calculator Keystroke Guide In your textbook you will notice that on some pages a key-shaped icon appears next to a brief description of a feature on your graphing calculator. In this guide

### Mathematical Induction. Lecture 10-11

Mathematical Induction Lecture 10-11 Menu Mathematical Induction Strong Induction Recursive Definitions Structural Induction Climbing an Infinite Ladder Suppose we have an infinite ladder: 1. We can reach

### Math 139 Problem Set #6 Solution 15 April 2003

Math 139 Problem Set #6 Solution 15 April 2003 A1 (Based on Barr, 41, exercise 3) (a) Let n be an integer greater than 2 Consider the list of numbers n! + 2, n! + 3, n! + 4,, n! + n Explain why all the