Introduction to Scheme prepared by Jason Gould
Installation:
http://www.drscheme.org/
Manual for DrScheme:
http://download.plt-scheme.org/doc/drscheme/
Useful link:
http://download.plt-scheme.org/doc/206p1/html/t-y-scheme/t-y-scheme-Z-H-1.html
Comment Statement:
; this
is a comment in Scheme
Data Types:
Simple Data Types:
Booleans:
(boolean? #t)
(boolean? “Hello World”)
(not? #f)
(not? #t)
Numbers:
(number? 42)
(complex? 2+3i)
(real? 3.1416)
(rational? 22/7)
(integer? 42)
(eqv? 42 42)
(= 42 42)
(+ 1 2 3)
(expt 2 3)
(max 1 2 3 4)
(min 1 2 3 4)
Characters
(char? #\c)
Symbol
‘xyz
Compound Data Types:
Strings
(string #\h #\e #\l #\l #\o)
(define greeting “Hello , Hello !”)
(string-
ref greeting 0) => #\H
Vectors
Dotted Pairs and lists
Conversion between data type:
(char->integer
#\d) => 100 look at http://www.asciitable.com/ to
see why you get 100 as the answer.
Procedures:
We
can create our own procedures by using the lambda keyword
(define add2
(lambda(x) (+ x 2)))
then you can call (add2 4) and you will get 6
(define area
(lambda(length)
(*
length)))
then you can call (area 3 4) and you will get 12
Simple If statement:
(define test
(lambda(x) (if(> x 2)'safe 'unsafe)))
Calling other procedures inside a procedure:
(define iseven
(lambda (n)
(if (= n 0) #t
(isodd (- n 1)))))
(define isodd
(lambda (n)
(if (= n 0) #f
(iseven (- n 1)))))
Recursion with Factorials:
(define factorial
(lambda (n)
(if (= n 0) 1
(*
n (factorial (- n 1))))))