All Haskell bindings are recursive. This is different from most languages, but often works correctly due to laziness (Haskell's assessment is not strict, unlike most popular languages). Beginners often get confused when they try something like:
main = do let a = 3 let a = 3 + a print a
Since the second binding to a
actually ignores and obscures the first and defines a
in terms of itself, which causes an infinite loop when you try to print the result 3 + 3 + 3 + 3 + ...
A simple example of an infinite list is ones
: infinite list 1
s
ones = 1 : ones
In this case, they simply refer to themselves
_______ | | v | ________ | | ones | | | 1 : ---| --------
In Haskell, you can create an infinite tree in much the same way you can create an infinite list:
data Tree a = Stub | Branch a (Tree a) (Tree a) onesTree = Branch 1 onesTree onesTree ______ _______ | | | | | vv | | ____________ | | | onesTree | | |--- | 1 | ----| ------------
I think the real question is: why don't other languages โโsupport recursive values โโas conveniently as Haskell?
source share