Comment on Give me Options or give me death
PoolloverNathan@programming.dev 9 months agoNeither does Haskell, and Haskell won’t waste time doing something that doesn’t matter.
Comment on Give me Options or give me death
PoolloverNathan@programming.dev 9 months agoNeither does Haskell, and Haskell won’t waste time doing something that doesn’t matter.
anton@lemmy.blahaj.zone 9 months ago
Imagine using a linked list as your default sequential container.
Rust iterators are lazy btw.
PoolloverNathan@programming.dev 9 months ago
You can’t random-access an iterator and use it again later. Can Rust compute the value of calling a function an infinite number of times?
— former rustacean
Lauchmelder@feddit.de 9 months ago
it can compute the number of times I needed to compute the value of calling a function an infinite number of times.
println!("0");
PoolloverNathan@programming.dev 9 months ago
If you’ve used a parser library’s recursive parser, you have infinite calls right there. If it supplies a recursive-parser function, that function is a type-limited equivalent to
fix
, which performs the infinite call operation. Your Rust library most likely implements recursion using hidden mutability, but in Haskell, your parsers can remain infinitely-recursive while still referencing themselves and immutable.Also, we get to ask people if they know what a monad is.
anton@lemmy.blahaj.zone 9 months ago
If your specific use case really needs random access to a list while lazy computing the elements just wrap them in
Lazy
and put them in a vector.The return type of an infinitely recursive function / infinite loops is ⊥, a type that by definition has no values. (Known in rust as
!
)PoolloverNathan@programming.dev 9 months ago
Haskell lets you infinitely recurse while still completing in finite time, and there’s even a function (
fix
) for that. Doing e.g.fix (+ 2)
would be an infinite loop if evaluated, yes, butfix (2 :)
would give you a useful value that’s an infinite stream of 2s. (it’s also useful for other things too)