Fibonacci in Lisp. Computer Programming: Skills & Concepts (CP1) Programming Languages. Varieties of Programing Language

Similar documents
Operating Systems CMPSCI 377, Lec 2 Intro to C/C++ Prashant Shenoy University of Massachusetts Amherst

Declaration Syntax. Declarations. Declarators. Declaration Specifiers. Declaration Examples. Declaration Examples. Declarators include:

Chapter 5. Names, Bindings, and Scopes

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

Dynamic Data Structures. CSCI 112: Programming in C

[0569] p 0318 garbage

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

Types and Type Inference

Chapter 5: Procedural abstraction. Function procedures. Function procedures. Proper procedures and function procedures

Weeks 6&7: Procedures and Parameter Passing

Introduction to C++ with content from

Week 7. Statically-typed OO languages: C++ Closer look at subtyping

QUIZ. What are 3 differences between C and C++ const variables?

CSE 431S Type Checking. Washington University Spring 2013

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

Informal Semantics of Data. semantic specification names (identifiers) attributes binding declarations scope rules visibility

Chapter 1 INTRODUCTION SYS-ED/ COMPUTER EDUCATION TECHNIQUES, INC.

Administrivia. Introduction to Computer Systems. Pointers, cont. Pointer example, again POINTERS. Project 2 posted, due October 6

CS61C Machine Structures. Lecture 4 C Pointers and Arrays. 1/25/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

Agenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011

Lecture 16: Static Semantics Overview 1

Short Notes of CS201

Programming Languages. Streams Wrapup, Memoization, Type Systems, and Some Monty Python

Types II. Hwansoo Han

G Programming Languages - Fall 2012

COMSW Introduction to Computer Programming in C

CS201 - Introduction to Programming Glossary By

G Programming Languages - Fall 2012

Introduction to C++ Introduction. Structure of a C++ Program. Structure of a C++ Program. C++ widely-used general-purpose programming language

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

Outline. When we last saw our heros. Language Issues. Announcements: Selecting a Language FORTRAN C MATLAB Java

Compilers. Type checking. Yannis Smaragdakis, U. Athens (original slides by Sam

Data Types. Every program uses data, either explicitly or implicitly to arrive at a result.

B16 Object Oriented Programming

CSCE 314 Programming Languages. Type System

COMP26120: Linked List in C (2018/19) Lucas Cordeiro

Topic 6: A Quick Intro To C

Data Structures and Algorithms

CS11 Advanced C++ Fall Lecture 7

Informatica 3 Syntax and Semantics

Chapter 17 vector and Free Store

B16 Object Oriented Programming

THE GOOD, BAD AND UGLY ABOUT POINTERS. Problem Solving with Computers-I

Static Semantics. Lecture 15. (Notes by P. N. Hilfinger and R. Bodik) 2/29/08 Prof. Hilfinger, CS164 Lecture 15 1

Design Issues. Subroutines and Control Abstraction. Subroutines and Control Abstraction. CSC 4101: Programming Languages 1. Textbook, Chapter 8

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

14. Pointers, Algorithms, Iterators and Containers II

Agenda CS121/IS223. Reminder. Object Declaration, Creation, Assignment. What is Going On? Variables in Java

CSE 303: Concepts and Tools for Software Development

Programming in C and C++

377 Student Guide to C++

Introduction to Programming Using Java (98-388)

First of all, it is a variable, just like other variables you studied

CSCI 171 Chapter Outlines

Fast Introduction to Object Oriented Programming and C++

The Imperative Paradigm

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

FORM 1 (Please put your name and section number (001/10am or 002/2pm) on the scantron!!!!) CS 161 Exam II: True (A)/False(B) (2 pts each):

Chapter 17 vector and Free Store

Types and Type Inference

COMP 181. Agenda. Midterm topics. Today: type checking. Purpose of types. Type errors. Type checking

CS121/IS223. Object Reference Variables. Dr Olly Gotel

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++

C++ assign array values. C++ assign array values.zip

Pointers. A pointer is simply a reference to a variable/object. Compilers automatically generate code to store/retrieve variables from memory

Introduce C# as Object Oriented programming language. Explain, tokens,

Static Semantics. Winter /3/ Hal Perkins & UW CSE I-1

C++ C and C++ C++ fundamental types. C++ enumeration. To quote Bjarne Stroustrup: 5. Overloading Namespaces Classes

Functions in C C Programming and Software Tools. N.C. State Department of Computer Science

15. Pointers, Algorithms, Iterators and Containers II

CS61C : Machine Structures

Recursion. Data and File Structures Laboratory. DFS Lab (ISI) Recursion 1 / 20

CSE 307: Principles of Programming Languages

Memory, Arrays & Pointers

Types. Type checking. Why Do We Need Type Systems? Types and Operations. What is a type? Consensus

Chapter 1 GETTING STARTED. SYS-ED/ Computer Education Techniques, Inc.

C++ for Java Programmers

C++ Programming. Classes, Constructors, Operator overloading (Continued) M1 Math Michail Lampis

Standard Library. Lecture 27. Containers. STL Containers. Standard Library

The issues. Programming in C++ Common storage modes. Static storage in C++ Session 8 Memory Management

G52CPP C++ Programming Lecture 13

C Pointers. 6th April 2017 Giulio Picierro

4 Bindings and Scope. Bindings and environments. Scope, block structure, and visibility. Declarations. Blocks. 2004, D.A. Watt, University of Glasgow

CSCI-1200 Data Structures Spring 2018 Lecture 7 Order Notation & Basic Recursion


Topic 9: Type Checking

Topic 9: Type Checking

The role of semantic analysis in a compiler

G Programming Languages Spring 2010 Lecture 6. Robert Grimm, New York University

Arrays. Returning arrays Pointers Dynamic arrays Smart pointers Vectors

Names, Scope, and Bindings

Patterns: Working with Arrays

Subject: PROBLEM SOLVING THROUGH C Time: 3 Hours Max. Marks: 100

Functions in C C Programming and Software Tools

Maltepe University Computer Engineering Department. BİL 133 Algoritma ve Programlama. Chapter 8: Arrays and pointers

News and information! Review: Java Programs! Feedback after Lecture 2! Dead-lines for the first two lab assignment have been posted.!

FOR Loop. FOR Loop has three parts:initialization,condition,increment. Syntax. for(initialization;condition;increment){ body;

Contents of Lecture 3

Polymorphism and Type Inference

Transcription:

Fibonacci in Lisp Computer Programming: Skills & Concepts (CP1) Programming Languages (defun fibonacci (n) (if (or (= n 0) (= n 1)) 1 (+ (fibonacci (- n 1)) (fibonacci (- n 2))))) 22nd November 2010 defun- - define a function. In functional programming almost everything is a function, even at basic level Notice in fibonacci (n) that we have no variables to store n-1 or n-2. Instead we apply the function - to the arguments n and 1 (and 2 respectively) CP1 27 slide 1 22nd November 2010 CP1 27 slide 3 22nd November 2010 Varieties of Programing Language Procedural/imperative (like C) Language consists of statements which act on the state space of variables. Functions, procedures common. Functional Languages (eg Haskell, Lisp) Specify what is computed, but abstract away from how. The concept of an evolving state space (of program variables) is not explicit. Object-Oriented Languages Focus is on the organisation and representation of the state space. Compilation versus Interpretation C is usually a compiled language: - Programming cycle is write/compile/run. - Compiler generates code to run on the hardware of the machine. - Fast, compact and efficient (once compiled). Sometimes languages (especially functional) may be interpreted: - The encoding into machine code is done on a step-by-step basis. - Allows for dynamic creation of variables and data structures. - Can be good for debugging. - Slower execution, requires interpreter. CP1 27 slide 2 22nd November 2010 CP1 27 slide 4 22nd November 2010

Imperative/procedural languages C - Need to be careful with array bounds (as we know!). - Allows direct access to memory. - Good for direct interfacing to hardware and writing device drivers. - Pointers get you into trouble. Fortran - Bit old fashioned, but still used (good for numerical work). - UK Met Office Unified Model - millions of lines of Fortran. - Limited feature set - less to go wrong. - Easy to make a fast compiler. What are they? Functional languages Emphasis is the evaluation of expressions, rather than the execution of commands - comp.lang.functional Important in theoretical computer science, not used so often in practice. Haskell is perhaps the most popular functional language. CP1 27 slide 5 22nd November 2010 CP1 27 slide 7 22nd November 2010 Features of Fortran - No explicit pointers (special case in F90) Easier to automatically optimise code. - Very stable numerical libraries available. - In F77, no dynamic storage allocation. - Cannot do recursion (but can in F90). - All variables passed by reference. Faster than by value. - Variable dimension array arguments to functions. Required by many numerical algorithms. - Built-in complex numbers. C total = 0; for (i=1; i<=10; ++i) total += i; Functional language. sum [1..10] Sum integers from 1-10 sum is a function to compute the sum of a list of values. [1..10] is an expression representing the list containing the numbers from 1 to 10 CP1 27 slide 6 22nd November 2010 CP1 27 slide 8 22nd November 2010

Object-Oriented Languages Used in practice int main(void) { Complex_t z, z1, z2, z3, z4; z1 = MakeComplex(1.0, -5.0); z2 = MakeComplex(3.0, 2.0); z3 = MakeComplex(2.0, -7.0); z4 = ComplexMultiply(z1, z2); z = ComplexSum(z4,z3); printf("the modulus of z is %f\n", Modulus(z)); return EXIT_SUCCESS; Evaluating the expression z = (z1*z2) + z3. CP1 27 slide 9 22nd November 2010 CP1 27 slide 11 22nd November 2010 typedef struct { float re, im; Complex_t; reminder: struct in C Complex_t ComplexSum(Complex_t z1, Complex_t z2) /* Returns the sum of z1 and z2 */ { Complex_t z; z.re = z1.re + z2.re; z.im = z1.im + z2.im; return z; C++ and objects C groups similar data into a struct: - Functions which operate on those data are separate from the data itself. C++ groups the functions operating on some struct type: - C++ calls these classes. - An instance of a class is called an object. - The functions don t exist until the object is created. Complex c1,c2,c3 ; c3 = c1.multiply(c2); CP1 27 slide 10 22nd November 2010 CP1 27 slide 12 22nd November 2010

Operator overloading C++ allows re-definition of standard operators - eg Complex number multiplication with *. - Also could define * for matrices etc Complex c1,c2,c3 ; c4 = c1 * c2 + c3; Inheritance Can define generic classes with general properties. Then subclasses can be derived from this base class. For example generic class (in C++) for a vehicle: char colour[50] ; int numwheels ; int start() ; int stop() ; Derived class for a car: char typeoffuel ; CP1 27 slide 13 22nd November 2010 CP1 27 slide 15 22nd November 2010 Common OO Languages C++ - Extension to the C language. - Has objects, but also still C pointers and memory access. - Compiles directly on the machine, like C. Java - Cleaner than C++ - no pointers. - Compiles onto a virtual machine. - Portable across platforms - and web applets. - Slower than C++ - and less efficient. Object-Oriented design What are classes? - sometimes obvious - complex numbers. Some tasks fit the model very well. Graphics, pipelined processes. Sometimes difficult to see where the objects are in a design. Some tasks are just a sequence of functions. CP1 27 slide 14 22nd November 2010 CP1 27 slide 16 22nd November 2010

Queue - a dynamic list of items first-in is first-out. Stack - first-in, last-out. Common Data Structures Both these structures have implementations with faster access than arrays (because no need for random access). C++ templates Way of writing objects (eg data structures) that is independent of the type that it works with. Write a generic queue template with a type parameter T. T can be replaced with any data type. (eg) Our queue can be used with any data type. Change details of template all queues automatically change. Very useful for common operations. lists, sorting, searching, queues etc. Useful set of templates provided in the Standard template library. CP1 27 slide 17 22nd November 2010 CP1 27 slide 19 22nd November 2010 Implementing a Queue You are implementing a queue for an accounting system. You implement a queue for customer records. Now you need a queue for messages too? You have to re-write the queue to work with the new message type? Examples: vectors in C++ (using arrays) void f(int a[], int s) { /* do something with a; the size of a is s */ for (int i = 0; i<s; ++i) a[i] = i; int arr1[20]; int arr2[10]; void g() { f(arr1,20); f(arr2,20); /* CRASH!! */ CP1 27 slide 18 22nd November 2010 CP1 27 slide 20 22nd November 2010

#define S 10; Using arrays void f(int s) { int a1[s]; /* error */ int a2[s]; /* ok */ /* Arrays have to be declared at compile time. *... Vector template void f(vector<int>& v) { /* do something with v */ for (int i = 0; i<v.size(); ++i) v[i] = i; vector<int> v1(20); vector<int> v2(10); void g() { f(v1); f(v2); Equivalent code with C++ vectors CP1 27 slide 21 22nd November 2010 CP1 27 slide 23 22nd November 2010 C++ vectors const int S = 10; void g(int s) { vector<int> v1(s); /* ok */ vector<int> v2(s); /* ok */ v2.resize(v2.size()*2); /* Can resize arrays during runtime. */ Summary C Good general purpose language. Good for interfacing with hardware. Not good for big projects(organisationally). Fortran Good for numerical computation. Stable, well-supported. C++ Use with the standard template library. Java Widely used, good for web applets, and neater than C++ Not as fast or efficient as C++. CP1 27 slide 22 22nd November 2010 CP1 27 slide 24 22nd November 2010