Skip to content

Instantly share code, notes, and snippets.

@shaunhess
Created July 18, 2015 22:30
Show Gist options
  • Save shaunhess/a71cede25a0aa9dbc867 to your computer and use it in GitHub Desktop.
Save shaunhess/a71cede25a0aa9dbc867 to your computer and use it in GitHub Desktop.
Notes from SICP using Scheme (LISP)
;; Installation on Mac OS X
;; After downloading and installing into applications folder run the following commands from the terminal
;; sudo ln -s /Applications/MIT\:GNU\ Scheme.app/Contents/Resources /usr/local/lib/mit-scheme-x86-64
;; sudo ln -s /usr/local/lib/mit-scheme-x86-64/mit-scheme /usr/bin/scheme
;; To Launch simply type "scheme" in the terminal
;; Using operators on operands (Scheme uses prefix notation)
(+ 4 4) ; Add 4 + 4 = 8
;; Add two numbers that are multiplied
(+ (* 2 4) (* 2 4)) ; 2 * 4 + 2 * 4 = 16
;; Assigning a variable a value
(define size 2)
(* 5 size)
(define pi 3.14159)
(define radius 10)
(define circumference (* 2 pi radius))
;; Compund procedure
(define (square x) (* x x))
(square 2) ; 2 * 2 = 4
;; Compound procedure as a building block
(define (sum-of-squares x y)
(+ (square x) (square y)))
(sum-of-squares 2 2) ; 2 * 2 + 2 * 2 = 8
;; Case analysis or Conditional
(define (abs x)
(cond ((> x 0) x)
((= x 0) 0)
((< x 0) (-x))))
;; or another way
(define (abs x)
(cond ((< x 0) ( - x))
(else x)))
;; If Expression
(define (abs x)
(if (< x 0)
(- x)
x))
;; Primitive predicates <, >, =
;; Logical composition operations and, or, not
(and (> x 5) (< x 10))
;; Define a new predicate
(define (>= x y)
(or (> x y) (= x y)))
;; or alternative
(define (>= x y)
(not (< x y)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment