this post was submitted on 14 Feb 2024
527 points (96.8% liked)

Programmer Humor

19623 readers
112 users here now

Welcome to Programmer Humor!

This is a place where you can post jokes, memes, humor, etc. related to programming!

For sharing awful code theres also Programming Horror.

Rules

founded 1 year ago
MODERATORS
527
??? (lemm.ee)
submitted 9 months ago* (last edited 6 months ago) by [email protected] to c/[email protected]
 
you are viewing a single comment's thread
view the rest of the comments
[–] [email protected] 7 points 9 months ago (1 children)

Neither does Haskell, and Haskell won't waste time doing something that doesn't matter.

[–] [email protected] 2 points 9 months ago (1 children)

Imagine using a linked list as your default sequential container.
Rust iterators are lazy btw.

[–] [email protected] 1 points 9 months ago (2 children)

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

[–] [email protected] 2 points 9 months ago* (last edited 9 months ago) (1 children)

it can compute how often I needed to compute the value of calling a function an infinite number of times.

println!("0");
[–] [email protected] 0 points 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.

[–] [email protected] 1 points 9 months ago (1 children)

You can't random-access an iterator and use it again later.

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.

Can Rust compute the value of calling a function an infinite number of times?

The return type of an infinitely recursive function / infinite loops is ⊥, a type that by definition has no values. (Known in rust as !)

[–] [email protected] 1 points 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, but fix (2 :) would give you a useful value that's an infinite stream of 2s. (it's also useful for other things too)