[racket] cube-all

From: Stephen Bloch (bloch at adelphi.edu)
Date: Thu Oct 4 15:38:29 EDT 2012

On Oct 4, 2012, at 12:17 PM, Ashley Fowler <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)
>      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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20121004/022c09b8/attachment.html>

Posted on the users mailing list.