guile-primer/7_list_and_cons.scm
2024-03-12 00:44:56 +01:00

55 lines
1.4 KiB
Scheme

;; -*- 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))