3 ms·
Have you noticed how easy it is to implement circular data structures or random number generators in C? The same is not true for Haskell. I might be
by Tehnix 13y ago
Have you noticed how easy it is to implement circular
data structures or random number generators in C?
The same is not true for Haskell.
I might be wrong, but, isn't this a circular data structure (or, what they are referring to) http://www.haskell.org/haskellwiki/Tying_the_Knot http://www.haskell.org/haskellwiki/Tying_the_Knot ?
In a language like Haskell, where Lists are defined
as Nil | Cons a (List a), creating data structures
like cyclic or doubly linked lists seems impossible.
However, this is not the case: laziness allows for
such definitions, and the procedure of doing so is
called tying the knot.
- quchen 13y agoYes. oneTwo = 1 : 2 : oneTwo is a circular singly-linked list containing 1 and 2. If you wanted to change the 1 to a 4 though, you would not be able to do so without creating an entire new list. You can use Vector or Array (i.e. fixed-size containers) to make yourself a circular buffer, but the implementation will be very similar to what you would do in C.
- cbsmith 13y agoWow. That looks hard. I bet you stayed up all night working on that. ;-) Seriously: sometimes things are hard because you don't know the language all that well. Consider that possibility.
- cbsmith 13y agoJust to be clear: the advice of "consider that possibility" is intended for the author of the article. quchen clearly knows the "easy" way to do this kind of thing with Haskell.