Skip to content

Instantly share code, notes, and snippets.

@iangreenleaf
Created September 2, 2010 18:47
Show Gist options
  • Save iangreenleaf/562724 to your computer and use it in GitHub Desktop.
Save iangreenleaf/562724 to your computer and use it in GitHub Desktop.
Impromptu Scheme session
;; Semicolons are comments
> (+ 2 4)
6
> (+ 2 4 8)
14
> (define double (lambda (a) (+ a a)))
> (double 3)
6
> '(1 2 3) ; this is a list
(1 2 3)
> (car '(1 2 3)) ; returns the first element of a list
1
> (car '(2 3))
2
> (cdr '(1 2 3)) ; returns the list minus the first element
(2 3)
> (cons 'a '(b c)) ; pushes an element onto the front of the list
(a b c)
> (define incList
(lambda (list)
(if (null? list)
'()
(cons
(+ 1 (car list))
(incList (cdr list))))))
> (incList '(1 2 3))
(2 3 4)
> (define inc (lambda (a) (+ a 1)))
> (inc 2)
3
> (map inc '(1 2 3))
(2 3 4)
> (apply + '(1 2 3 4))
10
> (map (lambda (a) (+ a 1)) '(1 2 3))
(2 3 4)
> (define simple (lambda (func) (func 2 3)))
> (simple +)
5
> (define add5 (lambda (a) (+ a 5)))
> (add5 6)
11
> ; ruby has many equivalents: [1 2 3].map { |a| a + 1 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment