Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Recitation 6: Imperative Programming
; (begin
; <FunctionCall#1>
; <FunctionCall#2>
; ...
; <ReturnFunctionCall>)
; ; executes a sequence of function calls, one at a time
; ; returns the value of the last one
; vend/outside-stock : number -> string or number
; vends the item if one is in stock, else return payment
; AN EXAMPLE OF SIDE EFFECTS AND INCONSISTENT RETURN VALUES
(define (vend/outside-stock payment)
(cond
[(and (= payment 1)
(> outside-stock/twix 0)) (begin
(decrement-stock/twix)
"twix")]
[(and (= payment 2)
(> outside-stock/chips 0)) (begin
(decrement-stock/chips)
"chips")]
[(and (= payment 3)
(> outside-stock/jerky 0)) (begin
(decrement-stock/jerky)
"jerky")]
; Refund payment if no matching item, or chosen item out of stock
[else payment]))
; sum-list : (listof number) -> number
; sums a list imperatively
(define (sum-list lon)
(local [(define sum 0)
(define remaining lon)
(define (loop)
(if
; If remaining is an empty list, return sum
(empty? remaining) sum
; Otherwise...
(begin
; First, update the value of sum
(set! sum (+ sum (first remaining)))
; Next, update the remaining list
(set! remaining (rest remaining))
; Finally, repeat
(loop))))]
; Kick off the loop
(loop)))
(check-expect (sum-list (list 1 2 3))
6)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; (for-each proc lst)
; ; like map because it applies proc to each lst item, but
; ; doesn't return the resulting lst
; sum-list/for-each : (listof numbers) -> number
; sums a list imperatively
(define (sum-list/for-each lon)
(local [; Initialize the sum to zero
(define sum 0)]
(begin
; Loop through the list, adding each item to the sum
(for-each (lambda (item)
(set! sum (+ sum item)))
lon)
; After we're done looping, return the sum
sum)))
; Using for-each works the same way as before
(check-expect (sum-list/for-each (list 1 2 3))
(sum-list (list 1 2 3)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Imperative Programming vs. Functional Programming
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Vending machines (functions) can do two things:
; 1. RETURN items, after taking payment
; 2. Cause CHANGES ("side effects") - decreasing the stock,
; increasing the stored amount of profits
; Functional-style programs only do (1) and never (2)
; Imperative-style programs can do (1), (2), or both
; ; Suppose we have a vending machine with the following options:
; ; $1 -------- twix bars
; ; $2 ------------ chips
; ; $3 ------- beef jerky
; FUNCTIONAL EXAMPLE
; vend: number -> string or number
; returns the item whose cost matches the payment, or returns payment if no match found
(define (vend payment)
(cond
[(= payment 1) "twix"]
[(= payment 2) "chips"]
[(= payment 3) "jerky"]
[else payment]))
(check-expect (vend 1) "twix")
(check-expect (vend 5) 5)
; (set! <VariableName> <NewValue>)
; ; updates the value of <VariableName>
; Outside vending machine stock
(define outside-stock/twix 2)
(define outside-stock/chips 2)
(define outside-stock/jerky 2)
; stock-twix : -> void
; decreases the number of twix in stock by one
(define (decrement/stock-twix)
(set! outside-stock/twix
(- outside-stock/twix 1)))
(define (decrement/stock-chips)
(set! outside-stock/chips
(- outside-stock/chips 1)))
(define (decrement/stock-jerky)
(set! outside-stock/jerky
(- outside-stock/jerky 1)))
; (void)
; ; does nothing
; If a function doesn't return any values, we say it returns void.
; vend/broken : number -> void
; takes your money and EATS IT. NOTHING HAPPENS.
(define (vend/broken payment)
(void))
; vend/broken-twix : number -> string or number or void
; works normally, except for the twix
(define (vend/broken-twix payment)
(cond
[(= payment 1) void]
[(= payment 2) "chips"]
[(= payment 3) "jerky"]
[else payment]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment