Procedural versus Functional Programming

Slides:



Advertisements
Similar presentations
Copyright © Cengage Learning. All rights reserved. CHAPTER 5 SEQUENCES, MATHEMATICAL INDUCTION, AND RECURSION SEQUENCES, MATHEMATICAL INDUCTION, AND RECURSION.
Advertisements

Types and Arithmetic Operators
The Binary Numbering Systems
BOOLEAN LOGIC CSC 171 FALL 2004 LECTURE 7. ASSIGNMENT Review Quiz # 2 Start reading Chapter 5.
Chapter 2 Data Types, Declarations, and Displays
1.2 – Open Sentences and Graphs
Class 37: Computability in Theory and Practice cs1120 Fall 2011 David Evans 21 November 2011 cs1120 Fall 2011 David Evans 21 November 2011.
Lambda Calculus History and Syntax. History The lambda calculus is a formal system designed to investigate function definition, function application and.
P.1 Real Numbers. 2 What You Should Learn Represent and classify real numbers. Order real numbers and use inequalities. Find the absolute values of real.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 2: Variables & Data Types.
COMPUTER PROGRAMMING. variable What is variable? a portion of memory to store a determined value. Each variable needs an identifier that distinguishes.
Copyright © – Curt Hill Types What they do.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
What is the main focus of this course? This course is about Computer Science Geometry was once equally misunderstood. Term comes from ghia & metra or earth.
CPS120: Introduction to Computer Science Variables and Constants.
1.1 – SETS AND SYMBOLS. Goals SWBAT understand basic set notation and set symbols SWBAT solve simple sentences with a given domain SWBAT graph sets of.
Modeling Arithmetic, Computation, and Languages Mathematical Structures for Computer Science Chapter 8 Copyright © 2006 W.H. Freeman & Co.MSCS SlidesTuring.
Bill Tucker Austin Community College COSC 1315
Week 2 - Wednesday CS 121.
Functional Programming Languages
CS 3304 Comparative Languages
Homework: Maintenance sheet 1-12
Computable Functions.
Thinking Mathematically
CMSC201 Computer Science I for Majors Lecture 22 – Binary (and More)
Building Java Programs
Copyright © Cengage Learning. All rights reserved.
CS 326 Programming Languages, Concepts and Implementation
CS 611: Lecture 9 More Lambda Calculus: Recursion, Scope, and Substitution September 17, 1999 Cornell University Computer Science Department Andrew Myers.
Number Systems INTRODUCTION.
University of Gujrat Department of Computer Science
CMSC201 Computer Science I for Majors Lecture 03 – Operators
Invitation to Computer Science, Java Version, Third Edition
Discrete Structure II: Introduction
Quantum Two.
Copyright 2012, 2008, 2004, 2000 Pearson Education, Inc.
Chapter 2 Bits, Data Types & Operations Integer Representation
Precalculus Mathematics for Calculus Fifth Edition
Numbers.
Building Java Programs
Theory of Computation Turing Machines.

Functional Programming
Subsets of the Real Numbers
Building Java Programs
High Level Programming Languages
Rules of evaluation The value of a number is itself.
Copyright © Cengage Learning. All rights reserved.
Chapter 6: Programming Languages
Low Level Programming Languages
Binary Representation
Building Java Programs
Building Java Programs Chapter 2
Chapter 2: Introduction to C++.
Chapter 7 Expressions and Assignment Statements.
Building Java Programs
Building Java Programs
The Data Element.
Building Java Programs
Primitive Types and Expressions
Building Java Programs Chapter 2
The Data Element.
Building Java Programs
Rules of evaluation The value of a number is itself.
CO Games Development 2 Week 21 Turing Machines & Computability
Number Theory: Prime & Composite Numbers
Representations & Reasoning Systems (RRS) (2.2)
The Fundamentals of C++
Primitive Data Types and Operators
Building Java Programs
Presentation transcript:

Procedural versus Functional Programming Is there a “mechanical” process that can determine whether any mathematical statement is true or false? (1928) Remember this idea: a mathematical statement can be either true or false. David Hilbert I formulated a model of a machine capable of general computation. I can use it to prove that there is no such process! (1935-6) Alonzo Church I proved that already! (1935-36) Alan Turing

Procedural versus Functional Programming Turing and Church independently formulated two very different ways of describing computation: Turing’s: the Turing machine. Church’s: the Lambda Calculus. A Turing machine is an embodiment of the procedural style of programming: Do this. Then do this. Then …. Lambda calculus is more like writing mathematical formulas. There is no “doing”. When Turing learned of Church’s work, he wrote an appendix for his paper, showing that the two models were equivalent in power.

invented exactly sixty years ago by John McCarthy in 1958 Practical influences The Turing machine is arguably close in spirit to real computing devices. After all, it was conceived as machine. The Lambda calculus gave rise to a programming language, one of the earliest programming languages invented…. Lisp invented exactly sixty years ago by John McCarthy in 1958 https://xkcd.com/224/

In this class, we use Scheme Lisp Many dialects evolved: MacLisp(1965), InterLisp (1970), ZetaLisp (1970), Lisp Machine Lisp (1975), Scheme (1975), …, Emacs Lisp (1985), …, Racket (1990), …. In this class, we use Scheme a subset of Racket that lines up with a subset of Scheme.

There is a tradition of using Scheme as an educational language. When we designed CS17, we thought to contribute to that tradition. https://www.youtube.com/watch?v=a0YrCABCOEY

Number of pages in description of language standard Java standard reference has 436 pages. If you count the pages on the standard library, it’s 1363

“CS17 Scheme” Racket Scheme

Key ideas for today Data objects and types Textual representations and denotations of them Expressions and evaluations

Data 17 (1 3 5) atom + (1 3 5 7 9) “hello, world” Intuitively, a datum is a mathematical object that the computer (or the language) can think about or remember. Datum or data object refers to the conceptual thing, independent of how it is represented in the computer or on the screen or on paper.

Types of data in our first language Numbers Booleans Symbols Lists Functions (procedures) Real languages (including Scheme and Racket) allow you to create new data types—-but we won’t do that until Ocaml.

How they are represented in Scheme’s memory Numbers Distinguish between How you can write them How Scheme writes them How they are represented in Scheme’s memory How you can write them: 17 The integer seventeen -17 The integer negative seventeen 3.14159 The real number that approximates pi 2/3 The fraction two-thirds 6.022e+23 Scientific notation 6.022✖️10²³ How does Scheme write them? It uses same formats—but not necessarily the same as you used. How are they represented? Won’t discuss now.

Boolean values represent the truth values of statements. Booleans I have invented the rules of thought! Boolean values represent the truth values of statements. There are two Boolean values: true false George Boole, c. 1860 Other representations Scheme might use: #true, #false #t, #f

Symbol Sequence of letters and digits and punctuation marks Must not be interpretable as a number. Examples: cs17 MyName if.you#dare + * - / Mostly used as names to refer to other things. We will use in Eliza project: The words in a sentence are symbols. How do we represent the whole sentence?

LISP CYCLES (https://xkcd.com/297/) Lists A list is a sequence of data objects. Any number (including zero). Scheme’s representation is very interesting. We’ll get to it later. Notation for lists? LISP CYCLES (https://xkcd.com/297/)

Lists Notation for lists? A list is a sequence of data objects. Any number (including zero). Scheme’s representation is very interesting. We’ll get to it later. Notation for lists? Examples: (1 3 5 7) (hello world) (+ 10 7) (+ (* 2 5) 7) (when the moon hits your eye ) Left parenthesis First item Space(s) Second item …. Last item Right parenthesis Item can be itself a list. (Extra spaces and even a newline doesn’t affect it.)

Sometimes helpful to visualize with a diagram. Lists Because you can make a list whose items are lists, you can make an arbitrarily complicated data object. ((1 2) (a (b c d) e) (f)) Sometimes helpful to visualize with a diagram. Such a diagram is called a tree. In CS, trees are drawn upside down compared to biological trees.

Quiz Write a text that represents the list with first item the symbol a second item the list consisting of 1 and 2 third item the list with zero items (a (1 2) () )

Denotation versus expressions Text: A sequence of characters that you or the computer writes. Denotation of a text: The data object that the text represents Examples: 17 denotes the integer seventeen Hello denotes a symbol + denotes a symbol (a b c) denotes a list (+ 10 7) denotes a list We will often elide the difference between text and its denotation, e.g. we will say “(a b c) is a list.” Some data objects can be evaluated. Evaluation is a complicated process but it follows certain rules, the Rules of Evaluation. Sometimes evaluation leads to a result, called the value. For example, the list denoted by (+ 10 7) evaluates to 17.

Rules of evaluation The value of a number is itself. The value of a Boolean is itself. The value of a symbol is the data object it is bound to. The value of a list is obtained as follows: First evaluate each item in the list. The first item should evaluate to a procedure. Apply the procedure to the values of the rest of the items. The result of the procedure application is the value of the list. Questions: What does bound to mean? What is a procedure? What does it mean to apply a procedure?

Examples (in arithmetic) The symbol + is bound to the addition procedure. The symbol * is bound to the multiplication procedure. The symbol / is bound to the division procedure. The symbol - is bound to the subtraction procedure. (+ 7 10) evaluates to 17 (+ 7 (* 2 5)) evaluates to 17

Unwritten Quiz Evaluate (+ 2 (- 5 1))