Lesson 1 Flashcards
‘’’
(define (square x))
(*x x ))
(square (+2 3))
‘’’
x is formal parameter, name for argument to the function
(* x x ) is the body which is part of the definition of the function
(+2 3) is the actual argument expression
actual argument value is 5
What is a predicate?
Predicate is a function that returns true or false. Convention is the name ends with ?
(define (pigl wd)
(if (pl.done? wd)
(word wd ‘ay)
(pigl (word (bf wd) (first wd)))))
(define (pl.done? wd)
(vowel? (first wd)))
(define (vowel? letter)
(member? letter ‘(a e i o u)))
what is the result of (pigl ‘scheme)?
emeschay
What is abstraction?
High level language (eg: Scheme)
Low level language (eg: C)
Machine language / architecture
—
Logic gates
Transistors
Quantum physics
What are logic gates?
Circuits that compute boolean functions (t/f). We can represent t/f values with one wire. Logic gates are built out of transistors like remote control switch.
f(x) = 2x + 6
g(x) = 2(x+3)
Are they the same function?
They are the same functions but different procedures. Same function because output is the same given input.
Different procedures because different sequence of steps. Inside computer, we look at procedures.
Why is functions important?
- Functions are well understood, easy to do reasoning on software system that is built functionally. We can prove theorems on them.
- Because of multi-core processors. We can run the program with parallelism if we write functionally since function does not care about what the rest of the program is doing.
Is f(x) = 2x + b a function?
No, because b is a free variable, and we get different outputs given same inputs.
(cond clause clause)
(test action)
(fn arg arg…) (fn arg arg…)
(cond ((equal? (remainder, 7) 0) ‘buzz)
((member? 7 n) ‘buzz)
(else n))
cond is a special form. It evaluates the test, if the test is true, it evaluates the action part and it finish. If the test returns false, it goes to the next clause and do the same thing.
Normal order evaluation
Procedure - take argument expressions and substitute them into the body. Arguments are evaluated only when we find a primitive.
Applicative order eager evaluation (hint: Scheme does this)
Evaluate subexpressions so we pass argument values into procedures. We substitute values for parameters in the body
What has to happen in recursion?
Base case has to come first before recursion can take place.
(def (zero z) (-z z))
(applic (zero (random 10)))
(zero (random 10))
(random 10) => 8
(- 8 8) => 0
(normal (zero(random 10)))
(- (random 10) (random 10))
(random 10) => 8
(random 10) => 1
(- 8 1) => 7
Why different answers?
Because random is a procedure that is not a function. If we write purely functionally programs, then we get the same answers.
If not functional, it matters what order things happen inside the computer. Functional programming protects you from figuring out what happens when inside the computer.
First class data types can be ?
First Class Data Type can be
- the value of a variable
- an argument to a procedure
- the value returned by a procedure
- a member of an aggregate
- anonymous
(define (evens nums)
(cond ((empty? nums) ‘())
((= (remainder (first nums) 2) 0)
(se (first nums) (evens (bf nums))))
(else (evens (bf nums))) ))
what is the output of (evens ‘(3 5 1 4 9 8 6 7))
(4 8 6)
what is domain and what is range?
domain - what kinds of things it take as arguments
range - what kinds of things it returns as a result
(define (ewords sent)
(cond (empty? sent) ‘())
((member? ‘e (first sent))
(se (first sent) (ewords (bf sent))) )
(else (ewords (bf sent))) ))
what does (ewords ‘(got to get you into my life)) return?
(get life)
(define (pronouns sent)
(cond ((empty? sent) ‘())
((member? (first sent) ‘(I me you he she it him her we us they them))
((se (first sent) (pronouns (bf sent))) )
(else (pronouns (bf sent))) ))
what does (pronouns ‘(i only want to dance with you)) return?
(i you)