Lecture 19 Scheme I
- Questions
- How to keep track of parentheses?
- Resources
- Page for built in functions
- Scheme Expressions
- Operators always at the front
- Primitive expression: 2, 3.3, true, +, quotient
- Are self evaluating
- Plugging in 1 in the interpreter will just give us 1
- Combinations
- Combines things together somehow
- Things in parentheses means a combination
- (quotient 10 2)
- Call expressions include an operator and 0 or more operands in parentheses
- (quotient 10 2)
- Indentation
- Adding to the two things on the same indentations
- Built in functions
- (integer? 1.5)
-
t
- Built in boolean procedures
- (< 4 5)
-
t
- Usually something with a question mark will return a boolean
- The only falsy value is #f, everything else is truthy
- 0 is truthy in scheme
- Special Forms
- A combination that is not a call expression is a special form
- You just kinda have to learn them
- if expression: (if
- if
- Binding symboles: (define )
- New procedures: (define ( ) )
- Evaluation order
- Evaluate the predicate expression
- Evaluate either the consequent of alternative
- Scheme Interpreters
- Similar to how python short circuits
- (and 1 2 3 4 5)
- 5
- Don’t need to wrap strings in quotes, only one at the beginning
- print ‘big
- use () to call a function
- Calling a Function x
- x vs (x)
- Lambda Expressions
- Evaluates to anonymous procedures
- (lambda () )
- ((define (f) 3))
- errors because we can’t call the string of f
- ((lambda () 3))
- 3
- this is ok
- More special forms
- We either have to evaluate the function that we created or use a lambda for helper functions
- Cond & Begin
- Cond special form that behaves like if-elif-else statements in Python
- The begin special form combines multiple expressions into one expression
- Let Expressions
- binds symbols to values temporarily, just for one expression
- Lists
- Scheme Lists
- Everything in scheme list is a linked list
- cons: two argument procedure that creates a linked list
- car: procudure that returns the first element of a list
- cds: procedure that return sth rest of a list
- nil: the empty list
- Written in parentheses with elements separated by spaces
- If we create a list in Scheme, it makes a linked list
- doesn’t evaluate nested, will include the operators
- will talk more about in interpreter lecture
- Symbolic Programming
- List Processing
- Example: Even Subsets
- We use a lot of recursion ← No iteration
- We want the subsets that have an even sum
- Recursive approach: even subsets of s include:
- all the even subsets of the rest of s
- the first element of s followed by an (even/odd) subset of the rest
- just the first element of s if it is even
12.