Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Yes! In lazy but immutable languages like Haskell, it is totally fine to refer to the value itself during definition. This is really the same idea that a recursive function can refer to itself during definition. It’s common to define a variable for the infinite list of prime numbers, where the definition requires the list of prime numbers itself.

    primes = 2 : sieve primes [3..]
    sieve (p:ps) xs = let (h, t) = span (< p*p) xs in h ++ sieve ps (filter (\n -> rem n p > 0) t)
Here `primes` is a variable that refers to itself in its definition (called corecursion), and `sieve` is a recursive function.


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: