1

I have two definitions of ones/0, both resulting in an infinite list of ones:

ones = 1 : ones

ones = repeat 1
repeat::a->[a]
repeat x = x : repeat x

I'm being told that the first one is a cyclic structure, while the second one is not. Is that true? And what does that actually mean/how can I tell whether a structure is cyclic or not?

Marcin
  • 368
  • 3
  • 18
  • 1
    The latter can be converted to the former by the compiler. – Dan D. Jun 23 '15 at 15:45
  • 1
    Related: http://stackoverflow.com/questions/16632143/why-recursive-let-make-space-effcient. `ones/0`? Are you coming from Erlang, by any chance? `:)` – jub0bs Jun 23 '15 at 16:01
  • @DanD. at least if you use [the definition in GHC's prelude](http://hackage.haskell.org/package/base-4.8.0.0/docs/src/GHC-List.html#repeat). In op's definition `repeat` won't be inlined. I don't know how the two differ operationally in that case. – jberryman Jun 23 '15 at 16:01
  • @Jubobs Nope, that's just a notation my teacher uses, so I bet he might ;) – Marcin Jun 23 '15 at 16:05
  • 2
    Also note that a "cyclic" structure as the one you posted is semantically indistinguishable to its non-cyclyc cousin. They will have different performance, especially in term of memory occupation, but they lead to the same result. Indeed, as @DanD. notes, the compiler could indeed replace one with the other. – chi Jun 23 '15 at 16:41

0 Answers0