[plt-scheme] Novice needs help writing function

From: Matthias Felleisen (matthias at ccs.neu.edu)
Date: Sat Dec 29 10:12:11 EST 2007

Dave, over the course of the extended exercise, you have
also developed a function for erasing pictures from the
canvas. So you definitely want to use a function (move)
that draws the picture, and if successful, erases it a
little while later from the same spot:

  (cond
    [(draw-losh FACE ....)
     (and (wait-a-while ...) (erase-losh FACE ...))]
    ...)

Holler if you need more help.

;; ---

At the same time, I recommend that you abandon the draw.ss
teachpack and use the world.ss teachpack instead. A first
introduction to the new way to dealing with images (as real
first-class values) is available at

  http://www.ccs.neu.edu/home/matthias/HtDP/Prologue/book.html

Images-as-values will be the basis of the next edition of
HtDP and they do make life much easier.

-- Matthias





On Dec 27, 2007, at 12:55 PM, dave yrueta wrote:

> Hi All --
>
> I'm a novice programmer embarking on a self-study course with "How to
> Design Programs" and I'm stuck on a problem that's driving me nuts!
> Some help would be greatly appreciated.
>
> The problems asks you to write a function which recursively mimics the
> following sequence of function calls --
>
> (draw-list-of-shapes
>        (move-picture 1
>              (move-picture 1
>                   (move-picture 1
>                         move-picture 1 FACE)
>
> -- where "draw-list-of-shapes" is a function which consumes a single
> "shape" structure, draws the shape by referring to other drawing
> functions, and returns "true;"
>
> --"move-pictures" consumes a number and a "shape" structure, draws and
> clears the shape and then returns a shape structure incremented by the
> value of the "number" parameter on the x axis of the drawing canvas,
> and;
>
> --"FACE" is a list of shapes resembling a human face.
>
> The challenge for me in writing this function is dealing with the list
> output of the "move-picture" function, which, since it's not boolean,
> cannot be evaluated within a conditional expression.  So the closest
> I've gotten to a solution is this function --
>
> (define(apply-n n)
>   (cond
>     [(zero? n) true]
>     [else (and(apply-n (sub1 n))(draw-losh(move-picture (+ 1 n)
> FACE)))]))
>
> The problem with this solution is that it draws 5 faces incremented by
> a value of 1 pixel across the x axis, instead of drawing a shape,
> clearing it, and then drawing the new "translated" shape, as the
> problem requests.
>
> Any thoughts?  Here's the full text of the problem in case I've
> explained it poorly:
>
> Exercise 11.2.3.   Develop apply-n. The function consumes a natural
> number, n. It applies the function move from exercise 10.3.6 n times
> to FACE, the list of shapes from exercise 10.3.1. Each application
> should translate the shape by one pixel. The purpose of the function
> is to simulate a continuously moving shape on a canvas, the last
> missing piece of the extended exercise 10.3.
>
> Thanks for your help!  This is my first time posting, so your support
> is greatly appreciated!
>
> Cheers,
> Dave
>
>
>
> _________________________________________________
>   For list-related administrative tasks:
>   http://list.cs.brown.edu/mailman/listinfo/plt-scheme



Posted on the users mailing list.