For Programmers: Free Programming Magazines  


Home > Archive > Scheme > April 2006 > I couldn't find the problem









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

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
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com