Last active
August 22, 2018 11:35
-
-
Save theodesp/02661821e2533f63b29b5c19f21d0eb8 to your computer and use it in GitHub Desktop.
Scheme Reference #scheme
This file contains 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
(import (rnrs (6))) | |
(use-modules ((rnrs) :version (6))) | |
;; Make a variable `call/cc' an alias of `call-with-current-continuation`. | |
(define call/cc call-with-current-continuation) |
This file contains 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
(import (rnrs (6))) | |
(use-modules ((rnrs) :version (6))) | |
;; Local Variables and Environments | |
(define a 5.3) | |
(define b 4.7) | |
(define c 2.8) | |
(define area | |
(let ((s (/ (+ a b c) 2))) | |
(sqrt (* s (- s a) (- s b) (- s c))))) | |
;; Lexixal Scoping | |
(define currency-abbreviation "USD") | |
(define (currency-string units hundredths) | |
(string-append currency-abbreviation | |
(number->string units) | |
"." | |
(number->string hundredths))) | |
(define (french-currency-string units hundredths) | |
(let ((currency-abbreviation "FRF")) | |
(currency-string units hundredths))) | |
(french-currency-string 33 44) ; "USD33.44" as `currency-abbreviation` inside `currency-string` is taken from the global environment | |
(define (make-serial-number-generator) | |
(let ((current-serial-number 0)) | |
(lambda () | |
(set! current-serial-number (+ current-serial-number 1)) | |
current-serial-number))) | |
(define entry-sn-generator (make-serial-number-generator)) | |
(entry-sn-generator) ; 1 | |
(entry-sn-generator) ; 2 | |
;; Example with A Shared Persistent Variable | |
(define get-balance #f) | |
(define deposit #f) | |
(let ((balance 0)) | |
(set! get-balance | |
(lambda () | |
balance)) | |
(set! deposit | |
(lambda (amount) | |
(set! balance (+ balance amount)) | |
balance))) | |
(define (withdraw amount) | |
(deposit (- amount))) | |
(get-balance) ; 0 | |
(deposit 50) ; 50 | |
(withdraw 75) ; -25 | |
;; Example: Object Orientation | |
(define (make-account) | |
(let ((balance 0)) | |
(define (get-balance) | |
balance) | |
(define (deposit amount) | |
(set! balance (+ balance amount)) | |
balance) | |
(define (withdraw amount) | |
(deposit (- amount))) | |
(lambda args | |
(apply | |
(case (car args) | |
((get-balance) get-balance) | |
((deposit) deposit) | |
((withdraw) withdraw) | |
(else (error "Invalid method!"))) | |
(cdr args))))) | |
(define my-account (make-account)) | |
(my-account 'get-balance) ; 0 | |
(my-account 'withdraw 5) ; -5 | |
(my-account 'deposit 396) ;391 |
This file contains 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
(import (rnrs (6))) | |
(use-modules ((rnrs) :version (6))) | |
;; A Scheme program consists of a sequence of expressions. | |
;; A Scheme interpreter executes the program by evaluating these expressions in order, one by one. | |
;; Evaluating Literal Data | |
"abc" ; string | |
3+4i ; complex number | |
#(1 2 3) ; vector | |
'() ; list | |
;; Evaluating a Variable Reference | |
(define key "Paul Smith") | |
(set! key 3.74) | |
(set! another 3.75) ;; error! | |
;; Evaluating a Procedure Invocation Expression | |
(string-length (string-append "/home" "/" "andrew")) | |
;; (string-length (string-append "/home" "/" "andrew") => | |
;; (string-length "/home/andrew") => 12 | |
;; Evaluating Special Syntactic Expressions | |
;; `If` needs to be evaluated before we delete the file. `If` is a special syndax. | |
(if (string=? (read-answer "Should I delete this file?") | |
"yes") | |
(delete-file file)) |
This file contains 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
(import (rnrs (6))) | |
(use-modules ((rnrs) :version (6))) | |
; Define module with list of exports | |
(define-module (foo bar) | |
#:export (times-2)) | |
(define (times-2 x) (* 2 x)) | |
(begin | |
(display (times-2 10)) | |
(newline)) |
This file contains 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
(import (rnrs (6))) | |
(use-modules ((rnrs) :version (6))) | |
;; A procedure invocation in Scheme is written like this: | |
;; (procedure [arg1 [arg2 …]]) | |
(string-append "/home" "/" "andrew") ;; "/home/andrew" | |
;; Returns the length of a single string argument, | |
(string-length "abc") ;; 3 | |
;; Creating | |
;; (lambda (name address) expression …) | |
;; Invocation | |
((lambda (name address) | |
(string-append "Name=" name ":Address=" address)) | |
"FSF" | |
"Cambridge") | |
;; Return a function | |
(define make-combined-string | |
(lambda (name address) | |
(string-append "Name=" name ":Address=" address))) | |
(make-combined-string "FSF" "Cambridge") ;; "Name=FSF:Address=Cambridge" | |
;; A define expression of the form | |
;; (define (name [arg1 [arg2 …]]) | |
;; expression …) | |
;; is exactly equivalent to the longer form | |
;; (define name | |
;; (lambda ([arg1 [arg2 …]]) | |
;; expression …)) | |
;; Variadic arguments | |
(define (square-var x . rest) | |
(* x (car rest))) | |
(square-var 5 6 7 8) ;; 30 |
This file contains 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
(import (rnrs (6))) | |
(use-modules ((rnrs) :version (6))) | |
;; Make a variable `x' with initial numeric value 1. | |
(define x 1) | |
;; Make a variable `organization' with an initial string value. | |
(define organization "Free Software Foundation") | |
;; Change the value of `x' to 5. | |
(set! x 5) | |
;; Change the value of `organization' to the FSF's street number. | |
(set! organization 545) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment