Vectors, Lines, and Fun Shapes

October 8, 2009
by David Warshow (diw08)

Built off Lee’s basic graphics code, a couple of procedures, to create vectors, lines, and some fun shapes/patterns.

Run with (sunBurst)

if you want check out (line) and (sunRay) I will warn you it’s completely uncommented

—> vectors-lines-and-fun.ss <—


code-immersion software overview (part 1) – package files

October 6, 2009
by Ian McEwen (ihm08)

Also posted to my personal blog at http://ianmcorvidae.net/

This post will go through each of the files of the code-immersion package and describe their function within the package. Files are in no particular order, but hopefully hopefully minimal things will rely on stuff described in files later in the list. Read the rest of this entry »


Some simple drawing code

October 6, 2009
by Lee Spector (lspector)

Here’s some simple code for drawing shapes in a new window.

-Lee


Dice roller for combat sim

October 6, 2009
by Stefan Terry (smt09)

so apparently I’m not the only one doing one, so to make some of your lives easier, here’s the dice roller:

#lang scheme

(define gigadice
(lambda (sides quantity)
(cond
((zero? quantity) ‘())
(else
(cons (+ 1 (random sides)) (gigadice sides (sub1 quantity)))))))

It’s actually really simple–it’s just a random number generator and a recursive call.


Dice Roller, Sentance Generator, and Morse Code Translator

October 3, 2009
by David Warshow (diw08)

Multiple Functions (programs) in one script integrated with the project script from code emersion.

type (s) to generate a sentance

type (d [sides on dice] [rolls to make])  to roll dice of any type

type (morse ‘[one letter]) converts to morse code  :: Example :: (morse ‘a)

I am working on improving the morse translator so it can do strings and not single chars. My eventual goal is to be able to call (morse (s)) to generate a sentence and immediately translate it into morse code.

Code here –>  project <–


Simple version of Eliza in Scheme

September 30, 2009
by Lee Spector (lspector)

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

September 22, 2009
by Arielle Soutar (as07)

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

September 22, 2009
by Lee Spector (lspector)

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

September 20, 2009
by Lee Spector (lspector)

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

September 20, 2009
by Lee Spector (lspector)

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