[plt-scheme] HTDP: 9.5.5

From: Grant Rettke (grettke at acm.org)
Date: Sat Sep 13 18:46:45 EDT 2008


I'm working on the 'convert' function in 9.5.5. Reading the problem
description, and then jotting out how I would solve this problem by
hand, I ended up with something like this (assuming the list of
numbers were 3 digits long)

Add up
n1 * 10^(3-3)
n2 * 10^(3-2)
n3 * 10^(3-1)

To get the answer. The only problem with that to implement it would
require an accumulator (the original length of the list, and the
current step, which are used to determine the exponent of 10). The
only problem with this is that this concept hasn't been introduced

Am I reading the problem right? Based on the fact that the "The first
digit is the least significant, and so on." then I assume this to be

(check-expect (convert empty) 0)
(check-expect (convert (cons 1 empty)) 1)
(check-expect (convert (cons 0 (cons 1 empty))) 10)
(check-expect (convert (cons 0 (cons 0 (cons 1 empty)))) 100)

If the first digit were the *most* significant digit, though, I could
implement the solution to this function using this approach:

Add up
n1 * (10^(length of the rest of the numbers in the list))
n2 * (10^(length of the rest of the numbers in the list))
n3 * (10^(length of the rest of the numbers in the list))

I'm confused by this one.

Best wishes,


Posted on the users mailing list.