Compare commits
2 commits
bd91b8fe69
...
5ca2960b6b
Author | SHA1 | Date | |
---|---|---|---|
5ca2960b6b | |||
7c76131b8d |
8 changed files with 197 additions and 0 deletions
2
3_hello_scheme.scm
Normal file
2
3_hello_scheme.scm
Normal file
|
@ -0,0 +1,2 @@
|
|||
|
||||
(display "Hello, World!\n")
|
8
4_basic_types.scm
Normal file
8
4_basic_types.scm
Normal file
|
@ -0,0 +1,8 @@
|
|||
|
||||
(+ 1 2)
|
||||
(/ 10 2)
|
||||
(/ 2 3)
|
||||
|
||||
(+ 1 8 10)
|
||||
|
||||
(* (- 8 (/ 30 5)) 21)
|
53
5_variables_procedure.scm
Normal file
53
5_variables_procedure.scm
Normal file
|
@ -0,0 +1,53 @@
|
|||
;; -*- geiser-scheme-implementation: guile -*-
|
||||
|
||||
(define name "Jane")
|
||||
(string-append "Hello, " name "!") ; "Hello, Jane!"
|
||||
|
||||
(define (greet name)
|
||||
(string-append "Hello, " name "!"))
|
||||
; #<unspecified>
|
||||
(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
|
||||
|
48
6_conditionals_predicates.scm
Normal file
48
6_conditionals_predicates.scm
Normal file
|
@ -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."
|
||||
|
||||
|
55
7_list_and_cons.scm
Normal file
55
7_list_and_cons.scm
Normal file
|
@ -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))
|
||||
|
26
8_closures.scm
Normal file
26
8_closures.scm
Normal file
|
@ -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) ; #<procedure goldilocks (n)>
|
||||
|
||||
(define goldi (make-goldilocks 10 30)) ; #<unspecified>
|
||||
(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
|
||||
|
1
9_iteration_and_recursion.scm
Normal file
1
9_iteration_and_recursion.scm
Normal file
|
@ -0,0 +1 @@
|
|||
;; -*- geiser-scheme-implementation: guile -*-
|
|
@ -0,0 +1,4 @@
|
|||
# Guile Primer
|
||||
|
||||
This project follows the primer at https://spritely.institute/static/papers/scheme-primer.html
|
||||
|
Loading…
Reference in a new issue