Question

I've written an extremely simple function to give me a list for all the integers between two bounds.

However, rather than outputing a list, it seems to be giving me data strucuture made of nested mcons cells.

What on earth did I do wrong?

#lang racket

(require rnrs/base-6)

(define (enumerate low high)
        (if  (> low high)
                '()
                (cons low
                    (enumerate (+ low 1) high))))

(enumerate 1 10)
;(mcons 1 (mcons 2 (mcons 3 (mcons 4 (mcons 5 (mcons 6 (mcons 7 (mcons 8 (mcons 9 (mcons 10))))))))))
Was it helpful?

Solution

#!r6rs and #!racket (short for #lang racket) are different languages. Racket allows you to blend them but pairs in R6RS is base on mutable pairs while #!racket uses immutable pairs. In #!racket the default output syntax for mcons (which all pairs in procedures from a R6RS library will be) are constructor so that you can clearly see difference between them and #!racket lists.

I advice you to not blend languages but if that is exactly what you want you can change the behaviour of how the interaction window displays results in the language options (In the bottom left select box, choose "Choose language", then click Show details. Under "Output syntax" you can choose everything to be shown as constructor, quasiquote, write (what you expected) or print (the default printing according to the selected language).

So if choose not to blend languages you can just remove (require rnrs/base-6) since rnrs base library pretty much has the same stuff as #!racket/base and your program has the larger #!racket defined as language. Howver, if you need mutation of pairs it's pretty awful to do it in #!racket so then you could change to R6RS by changing #!r6rs and(import (rnrs))mconswill display in #!r6rs ascons` do in #!racket.

OTHER TIPS

Because the cons defined in rnrs/base-6 is equivalent to racket's mcons and creates a value of that type (because Scheme lists are mutable and Racket lists aren't).

You're mixing Racket and Scheme here; if you leave out the require form then it'll be pure Racket and return

'(1 2 3 4 5 6 7 8 9 10)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top