Re:Noulith: A new programming language by the current Advent...
by widforss on Wednesday December 14, 2022 @07:24AM
(#33982548)
Because you cannot have loops in an immutable universe? Sure, you can map functions, but sometimes loops are fun too.
[ Parent
| on HN ]
Re:Noulith: A new programming language by the current Advent...
by tialaramex on Wednesday December 14, 2022 @07:44AM
(#33982723)
It's actually better if you have immutability here, languages which try to instead have a single variable for the values, mutating that variable each time around the loop, get into some trouble.
Compare Rust's for loop:
for k in 0..10 {
// k is an immutable variable, brought into existence for one iteration
// next time around the loop that's a different immutable k
}
Versus the C for loop: for (k = 0; k < 10; ++k) {
// k is a long-lived mutable variable, we can change k inside the loop and it'll remember!
}
We can tell Rust we want a mutable variable, if we want, but it's not the same: for mut k in 0..10 {
// k is a mutable variable...
// but still brought into existence for each iteration
// changing will work in this scope... but it won't last
}
Re:Noulith: A new programming language by the current Advent...
by widforss on Thursday December 15, 2022 @09:02AM
(#33999597)
Cool, I didn't know that Rust did that (or have forgotten)!
But it sure looks like a map where we throw away the result. What I mean with that is that the loop construct must be served the iterations from an iterator, which may or may not use immutable data. I don't think you can make an immutable while loop, for example?
EDIT: Also, you better have a side-effect in that construct, otherwise it has no effect, right?
Re:Noulith: A new programming language by the current Advent...
by qwerty456127 on Wednesday December 14, 2022 @08:21AM
(#33983080)
I mean both ways should be available. It generally is reasonable to support mutable variables but immutable values should be supported as well. E.g. Scala has 2 separate keywords: var and val (the difference is you can't assign a val again once it is initialized). I want the same in C#, Python and other languages.