Archive for September, 2009

Announcement: Welcome to Code Immersion

Sunday, September 6th, 2009

Code Immersion is a course at Hampshire College taught by Lee Spector in the fall semester of 2009. “Code Immersion” is also the name of software developed to support this course by Ian McEwen.

Information on the course can be found at:

http://courses.hampshire.edu/S299380

The Code Immersion software can be obtained from:

http://github.com/ianmcorvidae/code-immersion

The purpose of this i3ci blog is to serve as a distribution point for links and other items of interest to the class, and to serve as a vehicle for the submission of code portfolios.

Note: Use the “Preformatted” style for code or attach larger code fragments as files using the “Add Media” button in the message composition interface.

Simple version of Eliza in Scheme

Wednesday, September 30th, 2009

I’ve converted my simple version of Eliza to Scheme and have pasted it in below — click “Read the rest of this entry” to get to it if you don’t see it here. We’re working on tweaking the site to allow source code attachments, which will make this easier for all of us to use for sharing code in the future (soon, I hope!).

-Lee

Update: I think we got the code attachments working, and here is a link.

Read the rest of this entry »

Simple, silly code

Tuesday, September 22nd, 2009

Schools of fish or students!

(define whats-in-school?
(lambda (a n)
(when (eq? a ‘fish)
(for ((i (in-range n)))
(printf ” <o))))))<“)))
(when (eq? a ‘students)
(for ((i (in-range n)))
(printf ” O_o”)))
))

(whats-in-school? ‘fish 35)

Arielle

define vs. set!, values vs. printing, printf, simple loops

Tuesday, September 22nd, 2009

The following is a message I sent previously to the class email list, providing some information on a couple of aspects of Scheme that came up in class, but I’m posting it here for future reference.

First, variables are created with “define”, but if you want to give a new value to an existing variable you should use “set!”. This is particularly important if you’re giving it a new value within a procedure or some other complicated form, because “define” can only be used at the top level, not nested within complex expressions. So for example here I first define myvar but then use set! to give it a new value (which is made in part out of pieces of its old value):

> (define myvar '(some stuff))
> myvar
(some stuff)
> (set! myvar (cons (car myvar) (cons 'more (cdr myvar))))
> myvar
(some more stuff)
>

In that case I could actually have used define a second time too, even though it’s not the best style. But if I wanted to put it in a function then I’d have to use set!, like this:

> (define myvar '(some stuff))
> (define add-more
   (lambda ()
     (set! myvar (cons (car myvar) (cons 'more (cdr myvar))))))
> (add-more)
> myvar
(some more stuff)
>

But define wouldn’t work:

> (define add-more
   (lambda ()
     (define myvar (cons (car myvar) (cons 'more (cdr myvar))))))
. begin (possibly implicit): no expression after a sequence of internal definitions in: ((define myvar (cons (car myvar) (cons (quote more) (cdr myvar)))))
>

That’s an error message, albeit a totally confusing one (as error messages sometimes unavoidably are) — the full glory with the red stopsign doesn’t appear here, but you get the idea. So use set! if you want to give an existing variable a new value.

Next, I want to show you something about printing values. When we’ve been evaluating expressions in the REPL we’ve seen the values of the expressions printed by the “P” in REPL, which stands for Read Evaluate Print Loop. Something similar happens when you put a bunch of expressions in the definitions pane and hit Run — it prints the results of all of the expressions that it evaluates (and note that “define” expressions produce no values, so nothing gets printed for those).

If you want to print something that is not the value returned from a top-level expression you can use one of the several output functions like “print”, “display” or “printf”. Because printf is one of the most generally useful I want to show you a little about it real quick.

To print a string you can pass it to printf like this:

> (printf "Hey -- I'm a string!")
Hey -- I'm a string!
>

But maybe you can’t tell that this was printf printing it instead of the REPL… So here’s a procedure that takes two arguments, prints a string, and then returns the value of adding the arguments:

> (define add-with-a-gratuitous-message
   (lambda (x y)
     (printf "Gratuitous message printed by printf!")
     (+ x y)))
> (add-with-a-gratuitous-message 23 100)
Gratuitous message printed by printf!123
>

So you can see that printf printed the string and THEN the REPL printed the value returned by the procedure call, which is 123.

Now the fact that that the string and the number 123 got smushed together on one line is probably making you wonder how to put line breaks in a string. The answer is to put “\n” in the string (which is a convention from C) or alternatively “~%” (which is a convention from Common Lisp). So:

> (define add-with-a-gratuitous-message
   (lambda (x y)
     (printf "Gratuitous message\nprinted by printf!\n") ;; NOTE: there are two \n in this line
     (+ x y)))
> (add-with-a-gratuitous-message 23 100)
Gratuitous message
printed by printf!
123
>

If you want to print something other than a canned message you can include “~A” in the string and then include whatever value or value-producing expression after the string — the value will be substituted in place of the ~A. So for example:

> (define add-with-a-gratuitous-message
   (lambda (x y)
     (printf "I got ~A for x, ~A for y.\n" x y)
     (printf "And if you're curious their product is ~A.\n" (* x y))
     (+ x y)))
> (add-with-a-gratuitous-message 23 100)
I got 23 for x, 100 for y.
And if you're curious their product is 2300.
123
>

One more quick item: loops. There are actually many ways to make loops in Scheme, but for those of you who want to make simple numerical counting loops here’s a quick way to do it:

> (for ((i (in-range 5 10)))
   (printf "~A\n" i))
5
6
7
8
9
>

I’ll give more details as requested in class (or in email).

-Lee

Data plotting

Sunday, September 20th, 2009

There was a question in class about drawing in DrScheme, and another over email about doing things with numerical data in the form of (x y) pairs. Both of these are big subjects to which we can devote more attention in the future, but I thought some of you might be interested in one fairly simple intersection of these subjects: drawing simple plots of (x y) pair data in DrScheme.

The following is code that you can use to make such plots. It uses a couple of slightly fancy things that we haven’t yet covered — keyword arguments, “map”, etc. — and I won’t explain those here, but we can go over them in class:

(require plot)
(define plot-points
  (lambda (list-of-xy-pairs)
    (plot (points (map (lambda (p) (apply vector p)) list-of-xy-pairs)
                  #:sym 'oplus)
          #:x-min (apply min (map first list-of-xy-pairs))
          #:x-max (apply max (map first list-of-xy-pairs))
          #:y-min (apply min (map second list-of-xy-pairs))
          #:y-max (apply max (map second list-of-xy-pairs)))))

The first line, (require plot), gives you access to all of the things in the “plot” module — you’ll see things like this whenever we use libraries that aren’t part of the core Scheme language.

The plot-points function expects to be called with a single argument, which should be a list of (x y) pairs, where each (x y) pair is a list of two numbers.

So for example after you evaluate the definition above you could do something like:

(plot-points '((0 0) (1 1) (2 4) (3 9) (4 16) (5 25) (6 36)))

to produce:

A plot of some data points... y=x^2

Here’s something a little fancier, in which I generate the data using a for/list loop (which we haven’t yet covered in class):

(plot-points (for/list ((x (in-range -10 10)))
               (list x (sin (/ x 3.14)))))

This produces:

A plot of some sine wave data.

The plot package can be used for many other kinds of plots, and there are additional libraries for different kinds of graphics, but this might be useful for some simple data plotting applications.

-Lee

A simple Scheme shuffle

Sunday, September 20th, 2009

Here’s a pretty simple definition for a function that shuffles a list, using almost only the functions and ideas that we’ve already covered in class and in the first couple of chapters of The Little Schemer. It’s a recursive definition in which we say that a shuffled list is just the list itself if the length of the list is less than 2, and is otherwise a randomly selected item from the list stuck onto the front of the result of shuffling the rest of the list. Notice that we can’t get the “rest of the list” by using cdr, as most of the examples in the book do, because we don’t want the list without its FIRST item — instead we want the list without whatever item it is that we’ve randomly selected. To do this I use two things that we haven’t yet seen in class, but they’re pretty straightforward: “let”, which is used to create local variables, and “remove”, which returns a list just like the list that you give it but without the first item that matches the item that you give it.

(define shuffle ; Returns a randomly re-ordered copy of list.
  (lambda (list)
    (if (< (length list) 2) 
        list
        (let ((item (list-ref list (random (length list)))))
          (cons item (shuffle (remove item list)))))))
(shuffle '(dark star crashes pouring its light into ashes))

=> (into dark its light pouring ashes star crashes)

-Lee

Links

Sunday, September 6th, 2009

PLT Scheme: http://www.plt-scheme.org/

PLT Scheme Documentation: http://docs.plt-scheme.org/

R6RS Scheme Standard: http://www.r6rs.org/

Scheme Language Steering Committee: http://www.scheme-reports.org/

The Scheme Programming Language, Third Edition, by R. Kent Dybvig: http://www.scheme.com/tspl3/

Scheme Frequently Asked Questions: http://community.schemewiki.org/?scheme-faq-language

PLaneT repository: http://planet.plt-scheme.org/

SICP: Structure and Interpretation of Computer Programs by Harold Abelson and Gerald Jay Sussman with Julie Sussman: http://mitpress.mit.edu/sicp/full-text/book/book.html

SICP video lectures: http://groups.csail.mit.edu/mac/classes/6.001/abelson-sussman-lectures/

The SICP Picture Language: http://planet.plt-scheme.org/package-source/soegaard/sicp.plt/1/1/doc.txt

Schemers: http://www.schemers.org/

Schematics: http://schematics.sourceforge.net/projects.html

Scheme Cookbook: http://schemecookbook.org/

Fluxus: http://www.pawfal.org/fluxus/

Fluxus presentation: http://www.pawfal.org/dave/files/scheme-uk/scheme-uk-fluxus.pdf

Livecoding of graphics: http://www.toplap.org/index.php/Live_coding_of_graphics

Plumbing graphics: http://www.cs.unm.edu/~williams/cs257/graphics.htmlhttp://www.cs.unm.edu/~williams/cs257/plumber.scm

Scheme at Google: http://googleresearch.blogspot.com/2009/08/under-hood-of-app-inventor-for-android.html

Alligator Eggs: http://worrydream.com/AlligatorEggs/

Functional graphics: http://gustavus.edu/+max/concabs/schemes/drscheme/fungraph-notes.htmlhttp://eprints.ecs.soton.ac.uk/7577/1/funcgeo2.pdf