| Author |
I couldn't find the problem
|
|
| earwen 2006-04-16, 7:06 pm |
| Hello everyone
I am a student and it my first year of university. I wrote a code but
it gives bug and I couldn't find the problem. Can anyone help me?
Thanks
;;to10 list of digits -> number
;;example
;;(to10 '(1 2 3 4))
;;gives= 1234
(define to10
(lambda (list)
(local
((define (rev listt accumulator)
(cond
((null? listt) accumulator)
(else
(rev (cdr listt) (+ (+ (* (car listt) 10) (first (rest
listt))) accumulator))))))
(rev list 0))))
| |
| Jens Axel Søgaard 2006-04-16, 7:06 pm |
| earwen wrote:
> Hello everyone
> I am a student and it my first year of university. I wrote a code but
> it gives bug and I couldn't find the problem. Can anyone help me?
> Thanks
>
> ;;to10 list of digits -> number
> ;;example
> ;;(to10 '(1 2 3 4))
> ;;gives= 1234
> (define to10
> (lambda (list)
> (local
> ((define (rev listt accumulator)
> (cond
> ((null? listt) accumulator)
> (else
> (rev (cdr listt) (+ (+ (* (car listt) 10) (first (rest
> listt))) accumulator))))))
> (rev list 0))))
You are on the right way. Try writing the helper function first.
The helper function is easier to write, if the digits are
in reverse order.
(help '(1 2 3 4) 0)
= (help '(2 3 4) 1)
= (help '(3 4) 21)
= (help '(4) 321)
= (help '() 4321)
= 4321
After you have written and tested help, write to10 such that
it calls "help" with the digits in reverse order (remember "reverse"
is a primitive).
--
Jens Axel Søgaard
| |
| earwen 2006-04-16, 7:06 pm |
| I want to write a function that takes a list of digits and makes a
number
And I tried to find the number like this;
given list -> '(1 2 3)
to find the number -> ((1 . 10 + 2) 10) + 3
function gives -> 123 as a integer number
But my 'to10' function can't take the list that I give
it say -> first: expects argument of type <non-empty list>; given ()
| |
| Jens Axel Søgaard 2006-04-16, 7:06 pm |
| earwen wrote:
> I want to write a function that takes a list of digits and makes a
> number
> And I tried to find the number like this;
> given list -> '(1 2 3)
> to find the number -> ((1 . 10 + 2) 10) + 3
> function gives -> 123 as a integer number
>
> But my 'to10' function can't take the list that I give
> it say -> first: expects argument of type <non-empty list>; given ()
If you write "help" first, you can write to10 as
(define to10
(lambda (digits)
(help (reverse digits) 0)))
Hint:
(help '() 4321)
= 4321
and
(help '(2 3 4) 1)
= (help '(3 4) 2+10*1)
so in
(define (help digits acc)
...)
the body is ... what?
--
Jens Axel Søgaard
|
|
|
|