[racket] cube-all

From: Ashley Fowler (afowler2 at broncos.uncfsu.edu)
Date: Thu Oct 4 16:03:32 EDT 2012

From: Stephen Bloch [bloch at adelphi.edu]
Sent: Thursday, October 04, 2012 3:38 PM
To: Ashley Fowler
Cc: users at racket-lang.org
Subject: Re: [racket] cube-all

On Oct 4, 2012, at 12:17 PM, Ashley Fowler <afowler2 at broncos.uncfsu.edu<mailto:afowler2 at broncos.uncfsu.edu>> wrote:

I need to write a function that takes a list and cubes the whole list, for instance, (cube-all '(3 4 2 5)) returns (27 64 8 125).

OK, that's one good test case.  My students would be dinged for writing only one, but maybe your instructor isn't so demanding.

So far I have the code below, but it is not working out like I want it to. Any advice or suggestions?
(define cube-all
(lambda (ls)
(if (null? ls)
(cons(car ls)(cube-all (cdr ls))

What I call the "inventory with values" technique.  Write down all the expressions you're likely to need inside the function, one on a line: in your case,
(null? ls)
(car ls)
(cdr ls)
(cube-all (cdr ls))
Also write down the words "right answer" on another line.
Next, write the data type of each expression next to it.
Then pick a not-too-simple example (the one you gave, '(3 4 2 5), will work nicely).  Write down next to each expression its value for that example.
Then look at the value of "right answer" and ask how you could get it from the values of the other expressions.  Which of the other expressions resembles it most closely?  (Usually the result of a recursive call.)  What would you need to do to that similar value to get _exactly_ the right answer?
Once you've got an expression that works for this example, try it on other examples.

Stephen Bloch
sbloch at adelphi.edu<mailto:sbloch at adelphi.edu>

I tried using your example. One question. What do you mean by write the data type of each expression next to it?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20121004/15cd614a/attachment-0001.html>

Posted on the users mailing list.