3 ms·
> I guess primes is a thing (function?) that takes 2 or more things? Primes is an infinite list of integers containing all prime numbers. It's not a function,
by exDM69 12y ago
> I guess primes is a thing (function?) that takes 2 or more things?
Primes is an infinite list of integers containing all prime numbers. It's not a function, it's a value just like any other list of integers.
You obviously can't do something like that in C++ or most other languages. I think that's a pretty good example to demonstrate the difference between a pure and lazy language like Haskell and the more mainstream kind of languages.
If you're primarily a C++ programmer, Haskell should feel alien to you. It's a good thing. The differences in syntax are only skin deep, the differences in the semantics is what is interesting.
- dragonwriter 12y ago> Primes is an infinite list of integers containing all prime numbers. It's not a function, it's a value just like any other list of integers. > You obviously can't do something like that in C++ or most other languages. While the syntax is different, you can create a value just like any other iterable sequence that happens to be a generator for the infinite series of integers resulting from filtering another infinite series of integers by a particular function (which depends on the previous values of the series) in many, and possibly most, modern languages. (And the example here -- primes -- is pretty much the canonical example for every language.) Definitely in most of the popular dynamic languages (Ruby, Python, etc.). And plenty of popular static OO languages, too. (Pretty sure both Java and C#, and definitely sure that Scala and other JVM and .NET static languages can.)
- exDM69 12y ago> you can create a value just like any other iterable sequence that happens to be a generator for the infinite series of integers... I know that. Almost every language out there has some kind of generator/iterator protocol for "lazy" sequences. But they're not a list like any other list out there. You can (usually) iterate through them only once. And you can't apply a generator/iterator type of deal to, say, a tree structure or any other non-linear data structure. In Haskell, every value works like that (unless you explicitly tell otherwise). This is what the "primes" example is trying to show.