Imperative Programming. Back to scheme Scheme is a functional language In some cases there is a need to capture objects state E.g. bank account.

Slides:



Advertisements
Similar presentations
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Advertisements

Lambda Calculus and Lisp PZ03J. Lambda Calculus The lambda calculus is a model for functional programming like Turing machines are models for imperative.
Imperative Programming: Mutable data (local state, letrec, set!) Example 1: counter (define counter (let ((count 0)) (lambda () (set! count (+ count 1))
Imperative Programming: Mutable data (local state, letrec, set!) Example 1: counter (define counter (let ((count 0)) (lambda () (set! count (+ count 1))
1/25 Concurrency and asynchronous computing How do we deal with evaluation when we have a bunch of processors involved?
Object-Oriented Programming In Lisp. Lisp vs. Scheme 1.Lisp has much more built-in functions and special forms, the Scheme language definition takes 45.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 10. Environment Model 3.2, pages
Imperative Programming Chapter 6 1. Local State Real world software like: Banks Course grading system Are state systems. i.e. they change along time:
1 Functional programming Languages And a brief introduction to Lisp and Scheme.
6.001 SICP SICP Sections 5 & 6 – Oct 5, 2001 Quote & symbols Equality Quiz.
SchemeCOP Introduction to Scheme. SchemeCOP Scheme Meta-language for coding interpreters –“ clean ” semantics Scheme = LISP + ALGOL –simple.
Cs784(Prasad)L123Assg1 Assignments. cs784(Prasad)L123Assg2 l-value vs. r-value Pascal/Ada: x := x + 1 C/Java: x = x + 1 l-value = location, address, reference,
1 Programming Languages (CS 550) Lecture 5 Summary Object Oriented Programming and Implementation* Jeremy R. Johnson *Lecture based on notes from SICP.
© Kenneth C. Louden, Chapter 11 - Functional Programming, Part I: Concepts and Scheme Programming Languages: Principles and Practice, 2nd Ed. Kenneth.
Chapter 3, Section 3 ELECTRONIC BANKING.
Assignment: Changing Data CMSC Introduction to Computer Programming November 1, 2002.
1 The Evaluator. 2 Compiler vs. Interpreter Command Processing Unit The Computer Program in Low Level Machine Language Program in High Level Language.
1 CSC 533: Organization of Programming Languages Spring 2005 Advanced Scheme programming  memory management: structure sharing, garbage collection  structuring.
COP4020 Programming Languages Functional Programming Prof. Xin Yuan.
1 LAB What is Collaboration diagram? 4 Collaboration diagrams illustrate the interaction between the objects, using static spatial structure. 4.
6.001 SICP 1/ SICP Object Oriented Programming Data Abstraction using Procedures with State Message-Passing Object Oriented Modeling Class diagrams.
CS220 Programming Principles 프로그래밍의 이해 2002 가을학기 Class 14: Object Oriented Programming 한 태숙.
Example 1: counter (set! vs set-box!) 1 Imperative Programming: Mutable data and local state (define counter (let ((count 0)) (lambda () (set! count (+
Example 1: counter (set! vs set-box!) 1 Imperative Programming: Mutable data and local state (define counter (let ((count 0)) (lambda () (set! count (+
Fall 2008Programming Development Techniques 1 Topic 16 Assignment and Local State Section 3.1.
Bank Account Environment Model Examples Prof. Tony White April 2010.
Imperative Programming Chapter 6 1. Local State Real world software like: Banks Course grading system Are state systems. i.e. they change along time:
Fall 2008Programming Development Techniques 1 Topic 20 Concurrency Section 3.4.
1 Programming Languages (CS 550) Lecture 4 Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
SICP Object Oriented Programming Data Abstraction using Procedures with State Message-Passing Object Oriented Modeling Class diagrams Instance.
1 Data Abstraction. Pairs and Lists. (SICP Sections – 2.2.1)
Message Passing: Alternative to Generics data is “active” — knows how to compute dispatch on operation (define (rat (n ) (d )) (method ((op )) (cond ((=
CSE 425: Functional Programming I Programs as Functions Some programs act like mathematical functions –Associate a set of input values from the function’s.
Operational Semantics of Scheme
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Spring 2016.
Chapter 11 - Functional Programming, Part I: Concepts and Scheme
CS 326 Programming Languages, Concepts and Implementation
6.001 Jeopardy.
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Spring 2017.
6.001 SICP Object Oriented Programming
Introduction to Scheme
The role of abstractions
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
Lecture 14 - Environment Model (cont.) - Mutation - Stacks and Queues
CSC 533: Programming Languages Spring 2015
Closures and Streams cs784(Prasad) L11Clos
CSC 533: Organization of Programming Languages Spring 2008
Env. Model Implementation
Introduction to Functional Programming in Racket
Dynamic Scoping Lazy Evaluation
CS 36 – Chapter 11 Functional programming Features Practice
Lecture 16: Tables and OOP
Abstraction and Repetition
6.001 SICP Further Variations on a Scheme
Streams, Delayed Evaluation and a Normal Order Interpreter
Lecture 12: Message passing The Environment Model
Lecture 13 - Assignment and the environments model Chapter 3
Lecture 14 - Environment Model (cont.) - Mutation - Stacks and Queues
6.001 SICP Variations on a Scheme
Introduction to Functional Programming in Racket
Lecture 14: The environment model (cont
Dan Grossman / Eric Mullen Autumn 2017
Abstraction and Repetition
Assignments cs7100(Prasad) L13Assg.
Lecture 13: Assignment and the Environment Model (EM)
CSC 533: Organization of Programming Languages Spring 2007
Changing Data: (Continued)
*Lecture based on notes from SICP
Abstraction and Repetition
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Spring 2019.
Presentation transcript:

Imperative Programming

Back to scheme Scheme is a functional language In some cases there is a need to capture objects state E.g. bank account

Imperative Programming Imperative programming is characterized by: 1. Understanding variables as storage places (addresses). 2. Assignment – an instruction for changing the content of a variable. 3. Operational semantics – a computation is a sequence of system-states.

Scheme has constructs for Imperative Programming!

(define withdraw (let ((balance 100)) (lambda (amount) (if (>= balance amount) (begin (set! balance (- balance amount)) balance) "Insufficient funds"))))

Substitution model? (define withdraw (let ((balance 100)) (lambda (amount) (if (>= 100 amount) (begin (set! 100 (- 100 amount)) balance) "Insufficient funds")))) Substitution model can’t explain assignment!

(define make-withdraw (lambda (balance) (lambda (amount) (if (>= balance amount) (begin (set! balance (- balance amount)) balance) "Insufficient funds"))))

(define make-withdraw (lambda (balance) (lambda (amount) (if (>= balance amount) (begin (set! balance (- balance amount)) balance) "Insufficient funds"))))

(define make-account (lambda (balance) (letrec ((withdraw (lambda (amount) …. (deposit (lambda (amount) (set! balance (+ balance amount)) balance)) (dispatch (lambda (m) (cond ((eq? m ’withdraw) withdraw) ((eq? m ’deposit) deposit) (else (error "Unknown request-make-account" m)))) dispatch )

(define transact (lambda (account transaction-type amount) ((account transaction-type) amount)))

env-model diagram

Sameness and change (define W1 (make-simplified-withdraw 100)) (define W2 (make-simplified-withdraw 100)) > (W1 30) 70 > (W1 70) 0 > (W2 70) 30

Functional (define factorial (lambda (n) (letrec ((iter (lambda(product counter) (if (> counter n) product (iter (* counter product) (+ counter 1)))) )) (iter 1 1))))

Imperative (define factorial (lambda (n) (let ((product 1) (counter 1)) (letrec ((iter (lambda() (if (> counter n) product (begin (set! product (* counter product)) (set! counter (+ counter 1)) (iter)))) (iter)))))

Function calls In functional languages, usually by value In Imperative languages can be by reference

Simulating Object Oriented Languages In fact, the account object we have created is very similar to a class We can go further and separate method selection from application (define get-method (lambda (obj m) (obj m))) (define withdraw-method (get-method acc1 ’withdraw)) > (withdraw-method 30)

Method application (define send (lambda (obj message args) (apply (get-method obj message) args))) > (send acc1 ’withdraw ’(35))

Static properties (define make-interest-account (let ((interest 1)) (lambda (balance) (letrec ((withdraw (lambda (amount) … dispatch))))

Inheritence”" (define make-limited-account (lambda (limit acct) (letrec ((withdraw (lambda (amount) (if (> amount limit) ’over-limit ((acct ’withdraw) amount)))) (dispatch (lambda (message) (if (eq? message ’withdraw) withdraw (acct message))))) dispatch)))

Delegation (define make-passwd-account (lambda (password acct) (letrec ((change-password (lambda (new-pass) (set! password new-pass))) (dispatch (lambda (pass message) (if (eq? pass password) (if (eq? message ’change-password) change-password (acct message))

letrec with set! (letrec ((f1 lambda-exp1)...(fn lambda-expn)) e1... em) => (let ((f1 ’unassigned)... (fn ’unassigned)) (set! f1 lambda-exp1)... (set! fn lambda-expn) e1... em)

Boxed type In DrRacket, a mutable composite type must wrap its mutable components with the box type. DrRacket box type: A box is a minimal mutable storage. (box v) Returns a new mutable box that contains v. (unbox box) Returns the content of box. For any v, (unbox (box v)) returns v. (set-box! box v) Sets the content of box to v.

box (define y 3) (set! y (cons (box 1) (box 2))) > y ’(#&1. #&2) ; the printed form of a box includes the prefix "#&" ; before the box content. > (unbox (car y)) 1 > (set-box! (car y) 3) > y ’(#&3. #&2) > (unbox (car y)) 3

Type checking with set! Typing rule set! : For every: type assignment TA, expression e, and type expression S: If TA |- e:S, TA |- x:S, Then TA |- (set! x e):unit