Last active
August 29, 2015 14:20
-
-
Save beoliver/5e89180b864c01fcbc19 to your computer and use it in GitHub Desktop.
the basics principles of set! in scheme
This file contains hidden or 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
(require r5rs) | |
(define xs '(1 2 3 4 5)) ;; xs is a list of numbers | |
xs | |
(set! xs "a list of numbers") ;; xs is now "a list of numbers" | |
xs | |
(define (update-1 arg) | |
(set! arg "this has only changed the value of 'arg' within 'update-1'") | |
arg) | |
(update-1 xs) | |
xs | |
(define (update-2) | |
;; to use set! on a gloabl scale we need to know the 'name' of the variable | |
;; we want to change... | |
(set! xs "this has actaully changed the value of xs!") | |
xs) | |
(update-2) | |
xs | |
(define (fix-life) | |
;; set! requires the name of the variable | |
;; meaning has not been defined, | |
;; if called will result in an error | |
;; if it has been defined then it will | |
;; change it to 42 | |
(set! meaning 42)) | |
(define meaning "of life") | |
meaning | |
(fix-life) | |
meaning | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment