-1

If i have a scheme code that generates the following result: (i'm using cons)

'((1 . 0) . 0)

How can i take this, and just simply display 100 as if it were just one integer number and not a list presented with those dots and parenthesis?

Thanks!

EDIT:

my full code:

(define (func X) 
    (if ( <= X 3 )
       X
            (cons (modulo X 4) (func(floor(/ X 4)) ))  
))
Gambit2007
  • 3,260
  • 13
  • 46
  • 86

2 Answers2

2

If I understand correctly, you're trying to convert a number from base 10 to base 4, and then display it as a number, but there are several problems with your implementation.

You're building a list as output - but that's not what you want, you want a number. Also, you're traversing the input in the wrong order, and that's not the correct way to find the quotient between two numbers. Perhaps this will help:

(define (func X)
  (let loop ((n X) (acc 0) (mult 1))
    (if (< n 4)
        (+ (* mult n) acc)
        (loop (quotient n 4)
              (+ (* mult (modulo n 4)) acc)
              (* mult 10)))))

Alternatively, you could output a string to stress the fact that the output is not in base 10:

(define (func X)
  (let loop ((n X) (acc ""))
    (if (< n 4)
        (string-append (number->string n) acc)
        (loop (quotient n 4)
              (string-append (number->string (modulo n 4)) acc)))))

It'll work as expected:

(func 16)
=> 100
Óscar López
  • 232,561
  • 37
  • 312
  • 386
  • 1
    Urg... I don't think that's a good idea. If you want to write it as a string in base 4, that makes sense, but converting it to a base 10 number like that gives me the willies. How about just going straight to string-append? – John Clements Mar 05 '16 at 21:09
  • Initially I wrote a `string-append`-based solution, and it's uglier. But the output is not a base 10 number, it's in base 4, the part where I multiply by 10 is just for shifting each successive digit to the left – Óscar López Mar 05 '16 at 21:12
  • @JohnClements anyway, I added my alternate solution. – Óscar López Mar 05 '16 at 21:18
  • since i'm fairly new to scheme, could you please tell me what the (let loop) part is doing? i've only seen the 'let' keyword used in cases like (let (a 4)). – Gambit2007 Mar 05 '16 at 21:38
  • @Gambit2007 it's called a _named `let`_, it's just a shortcut for writing a helper procedure (it's not a special looping construct.) Here's a [link](https://docs.racket-lang.org/guide/let.html#%28part._.Named_let%29) to the relevant documentation. – Óscar López Mar 05 '16 at 21:52
  • i see.. that's a very interesting way of solving this problem.. thanks! – Gambit2007 Mar 06 '16 at 18:17
2

Oscar Lopez's answer is excellent. I can't help adding that this problem doesn't need the "loop" construct:

;; translate a string to a base-4 string.
(define (func n)
  (cond [(< n 4) (number->string n)]
        [else (string-append (func (quotient n 4))
                                   (number->string (modulo n 4)))]))
John Clements
  • 16,895
  • 3
  • 37
  • 52