Search code examples
rackethtdp

Why does my function sometimes return (shared ...) instead of (list ...)


I use the following function to turn a list of posns into a list of triples (posn, posn, symbol) for later use by draw-solid-line:

(define (list->triples a-list)
        (cond
          [(= (length a-list) 1) empty]
          [else (cons (list (first a-list) (second a-list) my-color) (list->triples (rest a-list)))]
          )
        )

When I apply it to (list (make-posn 10 10) (make-posn 10 20)) I get

(list (list (make-posn 10 10) (make-posn 10 20) 'black))

as expected, but when I add more posns to the list I get the following:

(shared ((-4- (make-posn 10 20))) (list (list (make-posn 10 10) -4- 'black) (list -4- (make-posn 20 20) 'black)))

which I find confusing.

It clearly behaves like the list I need, as it can be drawn in the expected way, but I just don't get what (shared ...) is and why my function returns this type of value.

My only clue is that it should be related to the presence of posns in the list, as I do not get the same behaviour with lists of symbols.

Just in case this is relevant: I'm using the HTDP-advanced teaching language.

Can anyone clarify why I get this type of output?


Solution

  • The results in your program print this way because you have the "Show sharing in values" setting enabled in your language settings. You can un-check the setting if you don't want values to print like this.

    The sharing printer is just abbreviating parts of the result that occur multiple times. You can find out more about sharing in the docs for racket/shared.

    Note: in future SO posts, it would be helpful if you provide the whole program you are trying to run so it's easy to reproduce the issue. Also it would help if you indicate which language setting you are using.