Intro to Scheme SI 413: Programming Languages and Implementation

advertisement
Intro to Scheme
SI 413: Programming Languages
and Implementation
Carl Albing, Ph.D.
Distinguished Visiting Professor
Dep't. of Computer Science
United States Naval Academy
Fall 2015
More Information
●
Class Web Site
http://www.usna.edu/~albing/courses/f15si413
●
Class group
https://groups.google.com/a/usna.edu/d/forum/f15si413­group
Fall 2014
SI 413 (USNA)
2/13
Scheme and Lists
●
Lists – everything (almost) is lists
●
Of what are lists made? “atoms”
a aword stuff
●
Here are some lists:
(aword stuff more stuff)
((a b c) (d e) f (g h ij))
Fall 2014
SI 413 (USNA)
3/13
Scheme and Lists
●
Lists – everything (almost) is lists
●
Of what are lists made? “atoms”
a aword stuff
●
Here are some lists:
(aword stuff more stuff)
((a b c) (d e) f (g h ij))
Exercises:
1) how many lists do you see?
2) how many lists have 4 elements? 3? 2? 1?
3) how many distinct atoms do you see in those two lists?
Fall 2014
SI 413 (USNA)
4/13
bash
●
Tries to run stuff
ls ­ld /home/me/mystuff # runs ls
x=ls ; opts=”­ld” mypath=/home/me/mystuff $x $opts $mypath # runs...
●
basic operation:
to run commands
Fall 2014
SI 413 (USNA)
5/13
Scheme, a.k.a. LISP
●
Fundamental operation:
to evaluate lists
(76 5 (23 11 8) 9 12 (14))
means...??
●
But what if the first atom of a list “meant”
something?
(cdr (212 (813 2311)))
Fall 2014
SI 413 (USNA)
6/13
Scheme
(car (a list)) → a
(cdr (a list)) → (list)
(cons two things) → (two things)
(quote (a list)) → (a list)
'( )
' ← short hand for (quote
Fall 2014
SI 413 (USNA)
7/13
Quote
What do you get from:
(quote (a b c))
(quote (a (b c d) e f g))
'( (a) (b) (c d e))
(car (cdr '( (a) (b) (c d e))
Fall 2014
SI 413 (USNA)
8/13
Scheme
(+ 5 4)
(* 3 6)
(/ 8 2)
●
Note these are not “infix” operators!
–
Which frees us up to do something interesting w/ numbers...
Fall 2014
SI 413 (USNA)
9/13
Logical Scheme
(if C T E )
where C is a list which evaluates to false or true
and where T is a list whose value is returned if C is true
and where E is a list whose value is returned if C is false
example:
(if (null? L) L (cdr L) )
(if
C
T
E
)
Fall 2014
SI 413 (USNA)
10/13
Defining Functions
(define
N
R
)
(define (func arg ...) (result) )
where func is an atom that is the name of the function
and where arg … are the arguments to the function
and where (result) is a list whose value is returned by the function
example:
(define (summer nums) (+ (car nums) (cadr nums)))
(summer '(2 3))
Fall 2014
SI 413 (USNA)
11/13
Remember...
●
Homework #2 due next Wed. (start of class)
●
Friday's lab due next Thurs. midnight
●
Finish reading Ch. 1 of PLP
●
Check the calendar on course web site
Fall 2014
SI 413 (USNA)
12/13
“Field Trip”
●
Let's go up to MI-316
●
Login
●
Command line: drracket
●
Choose R5S5 as the language
●
Try some Scheme
●
Top screen: function definitions (press “run” button)
●
Bottom screen: immediate interpreter (c.f. python)
Fall 2014
SI 413 (USNA)
13/13
Download