@luka.hadzi has joined the channel
this racket2 thing made me have an existential-scheme crisis.. and now the more I think about it, the more it seems the only real way scheme is suitable for serious / prod level programming is in a role of embedded scripting atop of something else
I’m sorry it’s been a bad time for you. I hope you are feeling better soon. I think a scheme was used to embed a scripting language for dialog in the game ‘last of us’.
Hi! Doing SICP in Racket, I found out that mutable list are highly discouraged in Racket. “Use of mutable lists for modern Racket code is strongly discouraged.” from: https://docs.racket-lang.org/compatibility/mlists.html.
How to implement a similar behaviour then ?
For example, in section “3.3.4 A Simulator for Digital Circuits” of SICP, things are implemented using mutable lists, for example: (define (add-to-segments! segments)
(if (= (segment-time (car segments)) time)
(insert-queue! (segment-queue (car segments))
action)
(let ((rest (cdr segments)))
(if (belongs-before? rest)
(set-cdr!
segments
(cons (make-new-time-segment time action)
(cdr segments)))
(add-to-segments! rest)))))
How to do that in Racket?
“The right way” ?
Oh, you mean if that SICP exercise were translated to Racket, what would it be turned into?
Yes
How to implement (add-to-segments! segments)
the way and experienced Racketer would ?
Which essentially boils down to insert an element in the middle of a list without having to copy anything
It cannot be done without mutation, can it ?
@contact When SICP was written Scheme had to data structures: lists and vectors.
SICP shows how to built other, compound data structures using the building blocks.
They use so-called tagged lists, where the first element of a list is a tag (symbol) that shows what the remaining elements in the list represent.
Today one uses structs or records (same concept - Racket uses structs).
and structs/records are much better, because the fields are named
You can use the keyword #:mutable
when you define a struct to make a struct mutable.
Ok! Thank you! I will try with these.
Yes - I think of the SICP approach as the “Poor man’s structs”.
Note that the sicp language does support mutable lists - but learning about structs is important.
Thank you very much!
@contact Note: the SICP language that @soegaard2 was talking about is #lang sicp
. You can install it with raco pkg install sicp
. In that hash lang, lists are mutable.
:+1:
I consider SICP outdated, but it shouldn’t be hard to write a “translation” into idiomatic Racket. Given its continuing popularity, I’m surprised someone hasn’t done so.
Or this: