Compare commits

..

2 commits

Author SHA1 Message Date
3570252c1b add macros 2024-03-13 23:52:40 +01:00
41a04a9d94 add mutation assignment lesson 2024-03-13 23:52:26 +01:00
2 changed files with 74 additions and 0 deletions

View file

@ -0,0 +1,34 @@
;; -*- geiser-scheme-implementation: guile -*-
(define chest 'sword)
chest ; sword
(set! chest 'gold) ; #<unspecified>
chest ; gold
(define (make-countdown n)
(lambda ()
(define last-n n)
(if (zero? n)
0
(begin
(set! n (- n 1))
last-n))))
(define cdown (make-countdown 3))
(cdown) ; 3
(cdown) ; 2
(cdown) ; 1
(cdown) ; 0
(cdown) ; 0
(define vec (vector 'a 'b 'c))
vec ; #(a b c)
(vector-ref vec 1) ; b
(vector-set! vec 1 'boop)
(vector-ref vec 1) ; boop
vec ; #(a boop c)

40
11_extensibility.scm Normal file
View file

@ -0,0 +1,40 @@
;; -*- geiser-scheme-implementation: guile -*-
(if (our-test)
(begin
(do-thing-1)
(do-thing-2)))
(define (when test . body)
`(if ,test
,(cons 'begin body)))
(when '(our-test)
'(do-thing-1)
'(do-thing-2))
; (if (our-test) (begin (do-thing-1) (do-thing-2)))
(define-macro (when test . body)
`(if ,test
,(cons 'begin body)))
(when (our-test)
(do-thing-1)
(do-thing-2))
(define-syntax-rule (when test body ...)
(if test
(begin body ...)))
(define-syntax-rule (for (item lst) body ...)
(for-each (lambda (item)
body ...)
lst))
(for (str '("strawberries" "bananas" "grapes"))
(display
(string-append "I just love "
(string-upcase str)
"!!!\n")))
; #<unspecified>