CS 2510 - Lab 1 : Scheme Review: Data, Loops and Accumulator


For this lab we will be using the DrRacket/Scheme, HtDP Advanced Student language.


Scheme Loop Function Review

Remember the Scheme loop functions? The functions you (hopefully) learned in Fundamentals I. Take a moment to review the functions here. This may be a bit out of date... so you can read the current documentation.

Exercise 1:

Remember the different syntax for creating lists? We went over it in class... Here's a quick reminder:

              ;; Hand Constructed
              (define lst1 (cons 1 (cons 2 (cons 3 empty))))

              ;; List notation
              (define lst2 (list 1 2 3))

              ;; Quote notation
              (define lst3 '(1 2 3))
            

Make a few examples of your own. At least one of each for [listof Number], [listof String], and a [listof Posn]. We'll use them in the next few exercizes, so think up some good ones.

Exercise 2:

A) For the [listof Number], design a function called times-14 that multiplies each number in the list by 14, using the plain-old design recipe.

B) Now create a new version of your function using map (call it times-14-map).

C) Abstract out the 14 so it is passed as an argument to your function. Test your function using different multipliers.

Exercise 3:

A) For the [listof String], design a function called length-sum that returns the sum of the lengths of all the strings in the list... using the design recipe. Remember string-length?

B) Now create a new version of your function using foldr or foldl.

Exercise 4:

A) For the [listof Posn], design a function called far-away that returns a list of all the Posns that are at least 10 units away from the origin (e.g., (0,0)). Remember posn and our distance function from class?

B) Now create a new version of your function using filter.

C) Abstract out the 10 (the minimum distance) so it is passed as an argument to your function, and modify it so that the caller also provides a Posn from which the distance is measured (instead of just the origin).


Images and World Teachpack Review

Add the world teachpack (Language > Add Teachpack: world.ss) to Dr.Scheme/Racket.

Exercise 5:

Try out these images in the interactions window (the bottom one).

  • (circle 20 'solid 'red)
  • (rectangle 50 40 'outline 'blue)
  • (star 5 20 40 'solid 'green)
What does ech one do? Can you figure out what all the arguments to the functions mean?

Exercise 6:

Take a look at the documentation for the functions empty-scene and place-image. Try out a few examples with the images above.

Create a few [listof Posn] examples, and design a function, call it draw-posns that takes a [listof Posn] and draws a star at each point into an empty-scene that is 400 x 400 pixels (i.e., (empty-scene 400 400).

Now rewrite your function using foldr or foldl. What do you need to pass to the loop function? Hint: You can use lambda or create a helper function.

You can use this code to test your function... Click in the window to create Stars!

                ;; Place in the bottom of your definitions
                (big-bang 400 400 .1 '())
                (on-redraw draw-posns)
                (on-mouse-event (lambda (w x y what)
                                  (if (symbol=? what 'button-down)
                                      (cons (make-posn x y) w)
                                      w)))
              


Homework Partner Selection

Please pair yourselves up with a partner. I think there are 25 people currently enroled in the course... so if one person would like to work alone for the first part of the semester, please speak up ASAP.

You and your partner will work on the assignments together (starting with the first one), and will produce a joint submission.

Take a bit of time to write down eachother's email and other contact information, and email me (chadwick@ccs.neu.edu) your names and your NEU account names (e.g., chadwick.b).



Homework Submission Setup

We will be using an assignment submission system similar to what you (probably) used in 2500... For the explanation of setup, we move to a separate webpage. You should go through this with your partner, since you will eventually submit your file together.