From 7c76131b8dd42bf46799fb1234ae6bfdaf8f8fdc Mon Sep 17 00:00:00 2001 From: Peter Tillemans Date: Tue, 12 Mar 2024 00:44:56 +0100 Subject: [PATCH] first session --- 3_hello_scheme.scm | 2 ++ 4_basic_types.scm | 8 +++++ 5_variables_procedure.scm | 53 +++++++++++++++++++++++++++++++++ 6_conditionals_predicates.scm | 48 ++++++++++++++++++++++++++++++ 7_list_and_cons.scm | 55 +++++++++++++++++++++++++++++++++++ 8_closures.scm | 26 +++++++++++++++++ 9_iteration_and_recursion.scm | 1 + 7 files changed, 193 insertions(+) create mode 100644 3_hello_scheme.scm create mode 100644 4_basic_types.scm create mode 100644 5_variables_procedure.scm create mode 100644 6_conditionals_predicates.scm create mode 100644 7_list_and_cons.scm create mode 100644 8_closures.scm create mode 100644 9_iteration_and_recursion.scm diff --git a/3_hello_scheme.scm b/3_hello_scheme.scm new file mode 100644 index 0000000..bc801ac --- /dev/null +++ b/3_hello_scheme.scm @@ -0,0 +1,2 @@ + +(display "Hello, World!\n") diff --git a/4_basic_types.scm b/4_basic_types.scm new file mode 100644 index 0000000..28699d5 --- /dev/null +++ b/4_basic_types.scm @@ -0,0 +1,8 @@ + +(+ 1 2) +(/ 10 2) +(/ 2 3) + +(+ 1 8 10) + +(* (- 8 (/ 30 5)) 21) diff --git a/5_variables_procedure.scm b/5_variables_procedure.scm new file mode 100644 index 0000000..e925a5d --- /dev/null +++ b/5_variables_procedure.scm @@ -0,0 +1,53 @@ +;; -*- geiser-scheme-implementation: guile -*- + +(define name "Jane") +(string-append "Hello, " name "!") ; "Hello, Jane!" + +(define (greet name) + (string-append "Hello, " name "!")) + ; # +(greet "Jane") ; "Hello, Jane!" +(greet "Samantha") ; "Hello, Samantha" + +((lambda (name) (string-append "Hello, " name "!")) "Jane") + ; "Hello, Jane!" +((lambda (name) (string-append "Hello, " name "!")) "Samantha") + ; "Hello, Samantha!" + +(let ((name "Horace")) + (string-append "Hello, " name "!")) ; "Hello, Horace!" + +(let* ((name "Horace") + (greeting (string-append "Hello, " name "!"))) + (string-append greeting " How are you?")) + ; "Hello, Horace! How are you?" + +(apply + '(1 2 5)) + ; 8 +(define (chatty-add chatty-name . nums) + (format #t "<~a> If you add those together you get ~a." + chatty-name + (apply + nums))) +(chatty-add "Jane" 1 2 5) ; #t + +(define* (shopkeeper thing-to-buy + #:optional (how-many 1) + (cost 20) + #:key (shopkeeper "Sammy") + (store "Plentiful Great Produce")) + (format #t "You walk into ~a, grab something from the shelves,\n" store) + (display "and walk up to the counter.\n\n") + (format #t "~a looks at you and says, " shopkeeper) + (format #t "'~a ~a, eh? That'll be ~a coins!'\n" how-many thing-to-buy (* how-many cost))) +(shopkeeper "apples") +(shopkeeper "bananas" 10 28) +(shopkeeper "screws" 3 2 #:shopkeeper "Horace" #:store "Horace's Hardware") + +(define (add-and-multiply x y) + (values (+ x y) (* x y))) +(add-and-multiply 2 8) ; 10 16 + +(define-values (added multiplied) (add-and-multiply 2 8)) +added ; 10 +multiplied ; 16 + diff --git a/6_conditionals_predicates.scm b/6_conditionals_predicates.scm new file mode 100644 index 0000000..f6e8047 --- /dev/null +++ b/6_conditionals_predicates.scm @@ -0,0 +1,48 @@ +;; -*- geiser-scheme-implementation: guile -*- + +(string? "apple") ; #t +(string? 5) ; #f +(string? 'apple) ; #f + +(define (string-enthusiast obj) + (if (string? obj) + "Oh my gosh you gave me A STRING!!!" + "That WASN'T A STRING AT ALL!!! MORE STRINGS PLEASE!")) +(string-enthusiast "apple") ; "Oh my gosh you gave me A STRING!!!" +(string-enthusiast 5) ; "That WASN'T A STRING AT ALL!!! MORE STRINGS PLEASE!" + +(> 8 9) ; #f +(< 8 9) ; #t +(> 8 8) ; #f +(>= 8 8) ; #t + +(define (goldilocks n smallest-ok biggest-ok) + (if (< n smallest-ok) + "That's too small!" + (if (> n biggest-ok) + "That's too big!" + "That's just right!"))) +(goldilocks 7 6 8) ; "That's just right!" +(goldilocks 33 10 20) ; "That's too big!" +(goldilocks 3 10 20) ; "That's too small!" + +(define (goldilocks n smallest-ok biggest-ok) + (cond ((< n smallest-ok) "That's too small!") + ((> n biggest-ok) "That's too big!") + (else "That's just right!"))) +(goldilocks 7 6 8) ; "That's just right!" +(goldilocks 33 10 20) ; "That's too big!" +(goldilocks 3 10 20) ; "That's too small!" + +(member 'b '(a b c)) ; (b c) +(member 'z '(a b c)) ; #f + +(define (fruit-sleuth fruit basket) + (if (member fruit basket) + "I found it!" + "Nope, not in here.")) + +(fruit-sleuth 'apple '(orange banana apple)) ; "I found it!" +(fruit-sleuth 'grape '(orange banana apple)) ; "Nope, not in here." + + diff --git a/7_list_and_cons.scm b/7_list_and_cons.scm new file mode 100644 index 0000000..43e8466 --- /dev/null +++ b/7_list_and_cons.scm @@ -0,0 +1,55 @@ +;; -*- geiser-scheme-implementation: guile -*- + +(list 1 2 "cat" 33.8 'foo) ; (1 2 "cat" 33.8 foo) +'(1 2 "cat" 33.8 foo) ; (1 2 "cat" 33.8 foo) + +(cons 'a '()) ; (a) +(cons 'a (cons 'b (cons 'c '()))) ; (a b c) + +(list 'a 'b 'c) ; (a b c) +'(a b c) ; (a b c) + +(car '(a b c)) ; a +(cdr '(a b c)) ; (b c) +(car (cdr '(a b c))) ; b + +(cons 'a 'b) ; (a . b) + +(+ 1 2 (- 8 4)) ; 7 +'(+ 1 2 (- 8 4)) ; (+ 1 2 (- 8 4)) +(let ((name "Horace")) + (string-append "Hello, " name)) ; "Hello, Horace" +'(let ((name "Horace")) + (string-append "Hello, " name)) ; (let ((name "Horace")) (string-append "Hello, " name)) + + +'foo ; foo +(quote foo) ; foo + +'(lambda (x) (* x 2)) ; (lambda (x) (* x 2)) +(quote (lambda (x) (* x 2))) ; (lambda (x) (* x 2)) + +;;; assoc lists + +(define animal-noises + '((cat . meow) + (dog . woof) + (pig . oink))) +(assoc 'cat animal-noises) ; (cat . meow) +(assoc 'alien animal-noises) ; #f + +(define (cat-years years) + (cond + ((<= years 1) ; first year equivalent to 15 + (* years 15)) + ((<= years 2) + (+ 15 (* 9 (- years 1)))) ; second year 9 + (else + (+ 24 (* 4 (- years 2)))))) ; years after that 4 + +(define (cat-entry name age) + `(cat (name ,name) (age ,age) (cat-years-age ,(cat-years age)))) + +(cat-entry "Missy Rose" 16) ; (cat (name "Missy Rose") (age 16) (cat-years-age 80)) +(cat-entry "Kelsey" 22) ; (cat (name "Kelsey") (age 22) (cat-years-age 104)) + diff --git a/8_closures.scm b/8_closures.scm new file mode 100644 index 0000000..649c1a2 --- /dev/null +++ b/8_closures.scm @@ -0,0 +1,26 @@ +;; -*- geiser-scheme-implementation: guile -*- + +(define (make-goldilocks smallest-ok biggest-ok) + (define (goldilocks n) + (cond ((< n smallest-ok) 'too-small) + ((> n biggest-ok) 'too-big) + (else 'just-right))) + goldilocks) + +(make-goldilocks 10 30) ; # + +(define goldi (make-goldilocks 10 30)) ; # +(goldi 7) ; too-small +(goldi 256) ; too-big +(goldi 22) ; just-right + + +(define (abstract-cons car-data cdr-data) + (lambda (method) + (cond ((eq? method 'car) car-data) + ((eq? method 'cdr) cdr-data) + (else (error "Unknown method -- ABSTRACT-CONS" method))))) +(define our-cons (abstract-cons 'foo 'bar)) +(our-cons 'car) ; foo +(our-cons 'cdr) ; bar + diff --git a/9_iteration_and_recursion.scm b/9_iteration_and_recursion.scm new file mode 100644 index 0000000..779600f --- /dev/null +++ b/9_iteration_and_recursion.scm @@ -0,0 +1 @@ +;; -*- geiser-scheme-implementation: guile -*-