Lesson 1 Flashcards

1
Q

‘’’
(define (square x))
(*x x ))
(square (+2 3))
‘’’

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is a predicate?

A

Predicate is a function that returns true or false. Convention is the name ends with ?

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

(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)?

A

emeschay

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is abstraction?

A

High level language (eg: Scheme)
Low level language (eg: C)
Machine language / architecture

Logic gates
Transistors
Quantum physics

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are logic gates?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

f(x) = 2x + 6
g(x) = 2(x+3)
Are they the same function?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Why is functions important?

A
  1. Functions are well understood, easy to do reasoning on software system that is built functionally. We can prove theorems on them.
  2. 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Is f(x) = 2x + b a function?

A

No, because b is a free variable, and we get different outputs given same inputs.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

(cond clause clause)
(test action)
(fn arg arg…) (fn arg arg…)
(cond ((equal? (remainder, 7) 0) ‘buzz)
((member? 7 n) ‘buzz)
(else n))

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Normal order evaluation

A

Procedure - take argument expressions and substitute them into the body. Arguments are evaluated only when we find a primitive.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Applicative order eager evaluation (hint: Scheme does this)

A

Evaluate subexpressions so we pass argument values into procedures. We substitute values for parameters in the body

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What has to happen in recursion?

A

Base case has to come first before recursion can take place.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

(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?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

First class data types can be ?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

(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))

A

(4 8 6)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

what is domain and what is range?

A

domain - what kinds of things it take as arguments
range - what kinds of things it returns as a result

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

(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?

A

(get life)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

(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?

A

(i you)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

(define (keep PRED sent)
(cond ((empty? sent) ‘())
((PRED (first sent))
((se (first sent) (keep PRED (bf sent)) )))
(else (keep PRED (bf sent))) ))

(keep (lambda wd)(member? ‘e wd))
‘(being for the benefit of mister kite))
1. what does this return?
2. what is keep an example of?

A
  1. (being the benefit mister kite)
  2. keep is a higher order procedure because it takes a procedure as an argument.
20
Q

what is a lambda expression?

A

lambda makes a procedure. lambda returns a procedure.

21
Q

what is higher order procedure?

A

it takes in a function as argument
OR
it returns a function

22
Q

Why do we want to generalize patterns?

A

to make programs shorter and to only write functions once. This is an example of procedure as data.

23
Q

What are first class data types?

A

Numbers
Character strings are first class in some languages but not all
Data aggregates are rarely first class.
Hardly any language give you procedures as first class. Python - lambda. Java - anonymized classes. Scheme.

24
Q

(define (compose f g) (lambda (x) (f (g x)))))
(define (twice f) (compose f f))
what does ((twice square) 3) return?

A

81

25
Q

(let* ((a 3)(b (+ a 5)))
(* a b))

is translated to:

(let ((a 3))
(let ((b (+ a 5)))
(* a b)))
Does this work? Can we access a inside (b (+ a 5))

A

Yes, value of a exists inside the body so we can use the value. It uses the binding of a to 3, so the order of bindings matter.
Let is an example of having procedures as a data type.

26
Q

Can you make a recursive procedure inside a let?

A

No. In order to make a recursive procedure, it needs access to its own name.

(let ((fact (lambda(n) (…) (fact(- n 1)))
(fact 5)
The body of let does have value from fact procedure. But the inner fact defined here (fact (- n 1)) gets evaluated before we provide a binding for the name fact so it will throw an error.

27
Q

((lambda (d -b 2a) (se ….))
(sqrt ….)
(- b)
(* 2a))
does this work? can we use d inside the sqrt expression?

A

No. The sqrt procedure, -b , *2a, gets evaluated before we call the lambda procedure so there is no d when we evaluate sqrt.

28
Q

what is the time complexity of this?
(define (square x) (* x x))
(define (squares set)
(if (empty? sent)
‘()
(se (square (first sent))
(squares (bf sent)) )))
(squares ‘(6 25, 3, 1, 9))

A

There are 6 class, 5 calls with non-empty sentences and 6th call with empty sentence.
empty sentence does 2 operations (if, empty). The non-empty sentences does 6 operations (first, bf, square, empty?, if, se)
Answer: 6N + 2 where N is the length of the input sentence
Constant factors in running time is not interesting, because when computers get faster, the run time will get cut in half.

29
Q

t(x) = O(x^2) is wrong
t(x) ∈ O(x^2) or
t(x) ∈ O(x ↦ x^2 ) this is even more accurate because it is a function f(x) maps to y

A

Big O notation
We look at big values of N, not small values.
Constant factors like / 2 can be removed (exception is in video games)

30
Q

Big θ

A

f∈θ(g) <-> f∈O(g) ∧ g∈O(f)

31
Q

Searching time complexity

A

θ(1)
θ(log N)
θ(N)

32
Q

Sorting time complexity

A

θ(N log N)
θ(N^2)

33
Q

Recursion - space efficiency

A

memory use is linear to the size of the problem
no work is being done until we get to the bottom of the recursive stack
remember to consider the memory space!

34
Q

θ(N^3) matrix multiply
Intractable - would never finish running for significant data sets
θ(2^N)
θ(N!)
θ(N^N)

A

Write approximate solutions that return in reasonable amount of time instead of intractable solutions

35
Q

what is a sentence?

A

list of words
it can accept words as well as list as arguments

36
Q

what is the difference between append and list?

A

append returns list of elements in the list
list returns list of all elements in the list

37
Q

What is the structure of pair?

A

cons - constructor of a pair
car - left half
cdr - right half

38
Q

What does (cons (3 4) (5 6)) return?

A

((3 4) 5 6) because cons take the first element and push it to the front of already existing list.

39
Q

What does (cons 3 (cons 4 (cons 5 ()))) return?

A

(3 4 5)

40
Q

What does (list (3 4) (5 6)) and (append (3 4) (5 6)) return?

A

((3 4) (5 6))
(3 4 5 6)

41
Q

What is the difference between data and procedures?

A

Things Actions
nouns verbs
know that know how to
DATA PROCEDURES

42
Q

is print functional?

A

no, because it changes the state of the world

43
Q

is read functional?

A

no because every time you call it with same input, you get different output

44
Q

what does flush do?

A

flush display characters on the string and waits for input. when we want to display characters, we need a newline character, but with flush we can do it with no newline character

45
Q

what is syntax vs semantics?

A

syntax - structure of language
semantics - meaning of individual words