Last active
December 9, 2015 20:39
-
-
Save syntacticsugar/4325203 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(define (abs x) | |
(cond ((= 0 x) 0) | |
((< 0 x) x) | |
((> 0 x)(- 0 x)))) | |
(define abs | |
(lambda (x) | |
(cond ((= 0 x) 0) | |
((< 0 x) x) | |
((> 0 x)(- 0 x))))) | |
The genie appropriates the keyboard. “Let’s see, now...” | |
He types: (abs 14) | |
The machine responds: 14 | |
He types: (abs -6) | |
The machine responds: 6 | |
He types: (abs 0) | |
The machine responds: 0 | |
“Very good.” |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Welcome to the interpreter. Enter ":q" to exit, or ":m" for documentation, or ":?" for a list of other ":" commands.] | |
>>(define cat '(100)) | |
(100) | |
>>(define dog '(100)) | |
(100) | |
>>:q | |
[Suspending interpreter. Press green button to reactivate.] | |
You lean back. The genie glances over, and asks, "Got it working yet?" | |
>yes | |
(to the genie) | |
The genie appropriates the keyboard. "Let's see, now..." | |
He types: cat | |
The machine responds: (100) | |
He types: dog | |
The machine responds: (100) | |
He types: (equal? cat dog) | |
The machine responds: t | |
He types: (eqv? cat dog) | |
The machine responds: nil | |
He types: (eqv? (cdr cat) (cdr dog)) | |
The machine responds: t | |
"Perfect! There are actually two ways to solve this problem. You used the simpler one, using one-term lists. The trickier solution would be something like this: | |
(define tail '(end)) | |
(define cat (cons 'head tail)) | |
(define dog (cons 'head tail)) | |
"The cdrs are eqv? because they are both the thing defined on the first line. See?" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
“Define sum to be a function that adds up a list of integers. So (sum ‘(8 2 3)) should return 13. Make sure it works correctly for the | |
empty list; (sum nil) should return 0.” | |
> turn on computer | |
The computer returns to life... | |
[Welcome to the interpreter. Enter “:q” to exit, or “:m” for documentation, or “:?” for a list of other “:” commands.] | |
>> (define sum (lambda (ints) (if (equal? ‘() ints) 0 (+ (car ints) (sum (cdr ints)))))) | |
[function] | |
>> :q | |
[Suspending interpreter. Press green button to reactivate.] | |
You lean back. The genie glances over, and asks, “Got it working yet?” | |
> yes | |
(to the genie) | |
The genie appropriates the keyboard. “Let’s see, now...” | |
He types: (sum (quote (8 -5 16 25))) | |
[Error: undefined atom: if] | |
The machine responds: [ERROR] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(define (print x) | |
(write x) | |
(newline)) | |
(define sum | |
(lambda (ints) | |
(if (equal? '() ints) | |
0 | |
(car ints) | |
))) | |
(print (sum '())) | |
(print (sum '(4))) | |
(print (sum '(2 7))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment