guile-primer/5_variables_procedure.scm

54 lines
1.9 KiB
Scheme
Raw Permalink Normal View History

2024-03-12 00:44:56 +01:00
;; -*- 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