How could I explain this unexpected behavior of my Scheme code?

前端 未结 1 501
灰色年华
灰色年华 2020-12-20 09:31

I was trying to define a data structure initialization function like \'(() ()), so that I can generate many of it later.

Right after I defined it, the init function

相关标签:
1条回答
  • 2020-12-20 10:09

    Make your init function return (list '() '()) instead of '(() ()). This will cause it to return a new list each time it's called.

    Literal data, like '(() ()), is immutable. That means that trying to mutate it using set-car! has undefined behaviour. The reason for this is that implementations are allowed to return the same instance of the literal data each time it's evaluated, so in this case, with your original code, each call to init was actually returning the same list.

    0 讨论(0)
提交回复
热议问题