Understanding direct self-reference in Haskell

CYC

I just started learning Haskell a few hours a go, trying to comprehend what this The Fibonacci sequence does:

fibs = 0 : 1 : next fibs
  where
    next (a : t@(b:_)) = (a+b) : next t

next function is strange to me, it will eventually get some "invalid" input, like at first it goes like this:

next (0:1) = (0+1) : next [1]

but then next ([1]) is not operable, since t@(b:_) has no input in it. So how does next work?

And my next confusion is fib itself, since it's suppose to be a Fibonacci sequence, I assume it will get fibs = 0 : 1 : 1 : next fibs after the first step, but then we will need to compute next([0, 1, 1]) witch gives (0+1): next([1, 1]) == 1 : next([1, 1]), we get the initial element 1, so in next([0, 1, 1]), the first value of the list (in next fibs) will be 1, but attached this 1 to the original fib, we get 0 : 1 : 1 : 1 which is not Fibonacci sequence.

I think I misunderstood something, so how it actually works?

chi

The standard way to define the result of a recursive definition is to approximate such value starting from undefined and unfolding the recursion from there as follows:

-- A function describing the recursion
f x = 0 : 1 : next x

fibs0 = undefined
fibs1 = f fibs0 = 0 : 1 : next undefined 
      -- next requires at least 2 elements
      = 0 : 1 : undefined
fibs2 = f fibs1 = 0 : 1 : next fibs1
      = 0 : 1 : next (0 : 1 : undefined)
      = 0 : 1 : 1 : next (1 : undefined)
      -- next requires at least 2 elements
      = 0 : 1 : 1 : undefined
fibs3 = f fibs2 = 0 : 1 : next fibs2
      = 0 : 1 : next (0 : 1 : 1 : undefined)
      = 0 : 1 : 1 : next (1 : 1 : undefined)
      = 0 : 1 : 1 : 2 : next (1 : undefined)
      -- next requires at least 2 elements
      = 0 : 1 : 1 : 2 : undefined
fibs4 = f fibs3 = 0 : 1 : next fibs3
      = 0 : 1 : next (0 : 1 : 1 : 2 : undefined)
      ...

If we keep going on we will approach the full sequence "at the limit", approximating it step by step. This informal argument can be formally justified through the Kleene's fixed point theorem.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Understanding Haskell callCC examples

From Dev

Understanding Self Join

From Dev

Understanding Haskell Type Signatures

From Dev

Understanding Direct Mapped Cache

From Dev

Understanding parameters in Haskell

From Dev

Understanding Haskell's `toUpper`

From Dev

Understanding Kind of Haskell Class

From Dev

com.fasterxml.jackson.databind.JsonMappingException: Direct self-reference leading to cycle (through reference chain)

From Dev

Understanding the Haskell as-pattern

From Dev

pass direct reference or closure

From Dev

Understanding Structure Sharing in Haskell

From Dev

understanding instance object in reference to self convention in __init__(self) function when defining class

From Dev

Understanding Haskell seq

From Dev

haskell understanding fmap

From Dev

Understanding Haskell's RankNTypes

From Dev

Understanding scoping with haskell monads

From Dev

Understanding basic recursion in Haskell

From Dev

Direct self-reference leading to cycle Superclass issue JSON

From Dev

Functions in Haskell - Understanding

From Dev

Haskell understanding some functions

From Dev

com.fasterxml.jackson.databind.JsonMappingException: Direct self-reference leading to cycle (through reference chain)

From Dev

Getter / Setter or direct reference?

From Dev

pass direct reference or closure

From Dev

Understanding self with method chaining

From Dev

haskell understanding fmap

From Dev

How to solve Direct self-reference leading to cycle error saving ActorRef on redis in playframework 2.4.6

From Dev

Understanding basic recursion in Haskell

From Dev

Understanding classes 'self'

From Dev

Understanding forever in Haskell

Related Related

HotTag

Archive