> (car foo) ; car gets the first element in a pair. 1 > (cdr foo) ; while cdr gets the second 2
Lists
Pair is basically a little list.
The definition of a list is a set of pairs and the last pair in that has null on the right hand side. If you don’t believe that, check out the code below.
1 2 3 4 5 6 7 8 9 10 11 12
> (cons1 null) ; it's indeed a pair of 1 and null but the way the interpreter writes it is a list containing only 1. (1)
> (cons12) (1 . 2) ; it's a pair. > (cons1 (cons2 null)) (12) ; it's a list > (cons1 (cons2 (cons3 null))) (123)
> (equal? (cons1 (cons2 null)) (list12)) #t
List is a bunch of pairs cascading down and ending in null.
> (car mylist) 1; the first element 1 in the pair mylist > (cdr mylist) (23) ; the second element (cons 2 (cons 3 null)) in the pair mylist, which is precisely the rest of the list mylist.
> (cadr mylist) ; d, then a. 2 > (caddr mylist) ; double d, then a. 3
Map applies the function you give it to every element in the list you give it. Difference between it and Python’s map is the latter one returns a map object which is a kind of iterator, but Scheme’s map directly give you a list.
> (define (assert-equal a b) (define (print-error) (display a) (display" is not equal to ") (display b) (newline)) (if (not (equal? a b)) (print-error) null)) > (assert-equal33) > (assert-equal34) 3 is not equal to 4
1 2 3 4 5 6 7
> (define (circle-details r) (define pi 3.1415926) (define (area) (round (* pi r r))) (define (circum) (round (*2 pi r))) (list (area) (circum))) > (circle-details3) '(28.019.0)
The inner function can access the symbols of the outer function unless you override them within the inner function.
> (define add-one make-add-one) ; no bracket, no function call > add-one(2) #<procedure:make-add-one> . . application: not a procedure; expected a procedure that can be applied to arguments given: 2 arguments...: [none]
If we want to make a list of three symbols, we get an error.
1 2 3
> (list a b c) . . a: undefined; cannot reference an identifier before its definition
We don’t have there definition, but if we define them, we get a list of what they refer to.
1 2 3
> (define a 1)(define b 2)(define c 3) > (list a b c) '(123)
How to make a list of the symbols themselves instead of what they refer to? The answer is by quoting.
1 2
> (list'a'b'c) '(a b c)
Quoting can apply to anything you like. Because of that quote symbol there, it’s not threated as a piece of code we’re going to run.
1 2 3 4 5
> '(a (b1 b2) c) '(a (b1 b2) c)
> (equal? '(a (b1 b2) c) (list'a (list'b1'b2) 'c)) #t
When you execute a bracketed list you always have to have at least one thing in that list which is the name of the procedure you want to execute. But how to create a list with no element?
1 2 3 4 5 6 7 8 9 10 11 12 13
> () . #%app: missing procedure expression; probably originally (), which is an illegal empty application in: (#%app) > (list) ; it's a way. '() > '() ; with the power of quotation, this is another way. '() > null '()
> 'null 'null
Asking about data
Before a procedure get executed, all the arguments get evaluated.
1 2 3 4 5 6 7 8
> (symbol?'z) #t > (symbol? z) . . z: undefined; cannot reference an identifier before its definition > (define z 1) > (symbol? z) #f
Generic type safety
Quote this stuff looks like code.
1 2 3 4 5
(define generic-safe-sum '(define (safe-sum x y) (if (and (TEST x) (TEST y)) ; we don't know what TEST is so far but we will. (+ x y) "INCORRECT TYPES")))