For Programmers: Free Programming Magazines  


Home > Archive > Scheme > September 2005 > HtDP query









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 HtDP query
ttb

2005-09-03, 11:23 pm

Ni hao,

Found this board in a search for HtDP stuff - sorry to pollute with more HtDP questions if that irritates the more experienced.

I saw a previous question on the exercize that asks to convert a list of digits into an integer, with the first item being the least significant.

This was simple enough, but I started thinking about how I would do the other way - the first digit being the _most_ significant digit and so on down.

HtDP is my first intro to Scheme, so my knowledge of the language extends only to my point in the book.

Could anyone give me a hint as to how I could make a "convert-backwards" function which converts list 1 2 3 into one-hundred twenty three (123)? Perhaps it is more straightforward with language constructs I do not know yet because I am not so far in the book.

Thanks in advance

ttb
ttb

2005-09-08, 10:29 am

Does nobody know how to do this question?

Thank you for any help

ttb
ttb

2005-09-13, 5:42 pm

Here is the solution I came up with for any others that come upon this and are interested.

I am sorry nobody else could offer a better one

----------------------

(define (convert-backwards a-list)
  (convert-iter a-list 0))
(define (convert-iter a-list sum)
  (cond
    [(empty? a-list) sum]
    [else (convert-iter (rest a-list) (+ (* 10 sum) (first a-list)))]))
Sponsored Links







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

Copyright 2008 codecomments.com