1

So I've just figured out that you can use string->number to convert string 365 into int 365. But now I have another question:

How do I take, for example, (1 2 3), and convert it to string 123 so I can apply string->number to it? Any help would be appreciated. Thanks!

If it helps: I am using drracket 6.0

2
  • 1
    You should not have deleted your original question, if that were the actual problem you were trying to solve. Because now, you've turned this question into an XY problem. Commented Apr 11, 2014 at 10:09
  • 1
    (For non-10k users: the original question was about how to turn a list like '(123 45 6) into the number 123456.) Commented Apr 11, 2014 at 10:11

2 Answers 2

1

I'm going to answer your original question, not your current one, which is just one possible solution approach.

There are a number of ways to solve this. One way, like you mentioned in this question, is to convert the incoming list of numbers into strings, then concatenating them:

(require srfi/13)
(define (number-concatenate nums)
  (string->number (string-concatenate (map number->string nums))))

Here's another approach, that does not involve conversion to strings first:

(require srfi/1 srfi/26)
(define (number-concatenate nums)
  (define (expand num)
    (if (< num 10)
        (list num)
        (unfold-right zero? (cut modulo <> 10) (cut quotient <> 10) num)))

  (fold (lambda (num result)
          (fold (lambda (digit result)
                  (+ digit (* result 10)))
                result (expand num)))
        0 nums))
Sign up to request clarification or add additional context in comments.

Comments

0

If you have array of numbers you can do like this(javascript code)-

 [1,2,3].join('');
    o/p=>"123"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.