Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5847483
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:43:30+00:00 2026-05-22T12:43:30+00:00

Recursion is something I have always struggled with understanding. I hope I can get

  • 0

Recursion is something I have always struggled with understanding. I hope I can get some assistance here on how this function works. It works but I want to know how:

fibStep :: (Int,Int) -> (Int,Int)
fibStep    (u,  v)    = (v,  u+v)

fibPair :: Int -> (Int,Int)
fibPair n
    | n==0      = (0,1)
    | otherwise = fibStep (fibPair (n-1))
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T12:43:30+00:00Added an answer on May 22, 2026 at 12:43 pm

    Recursion works much like a loop. With a loop you have a stop condition, and the same is true for recursion, except it is called the base case. In this Fibonacci example, the base case is n==0, which returns the tuple (0,1) — and of course this represents the first Fibonacci number.

    After you’ve established your base case, now you need to figure out the recursive step — in this example it is fibStep (fibPair (n-1)). This is equivalent to the code block for a loop, or what step is repeated on every iteration until the base case has been reached. Naturally, it is critical that you make sure you always converge to your base case, else the recursion will never end — and in the example, the recursive step does converge to the base case, because for each consecutive step, the value of n decreases by one, meaning we must eventually reach the case where n==0 (assuming n is initially positive).

    Let us look at the evaluation of fibPair 3. Each consecutive line is an expansion of the previous, using the definitions provided in the example.

    fibPair 3 **note: n is not 0 so we use the recursive step
    fibStep (fibPair (3-1))
    fibStep (fibPair 2) **note: n is not 0 use recursive step again
    fibStep (fibStep (fibPair (2-1)))
    fibStep (fibStep (fibPair 1)) **note: n is not 0 use recursive step again
    fibStep (fibStep (fibStep (fibPair (1-1))))
    fibStep (fibStep (fibStep (fibPair 0))) **note: n equals 0 so base case is used
                                            ** recursion has now ended
    fibStep (fibStep (fibStep (0,1))) **note: now fibStep begins evaluation
    fibStep (fibStep (1, 1))
    fibStep (1, 2)
    (2, 3)
    

    Importantly, you should become comfortable with an English explanation of what is happening. fibStep takes a tuple of (fib number x, fib number x+1) and returns the next tuple in sequence, namely (fib number x+1, fib number x+2). fibPair acts as loop over fibStep, causing fibStep to be called n times on the same tuple, meaning the tuple (fib number x, fib number x+1) will result as (fib number x+n, fib number x+n+1).

    Hopefully that helps clarify things a little bit. Recursion is truly just another way to write a loop; it has all of the same elements yet is written a bit differently. For some problems using recursion results in much simpler logic, or code, or both. Once you become more comfortable with recursion it will be a very useful tool to have for your future programming.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a fairly simple script but something is going wrong and I can't
Ok I'm really confused about something about recursion in Java. Say I have the
I have just started looking at javascript so hopefully this will be something simple.
Creating Traversals for Binary Search Tree with Recursion. void inOrder(void (*inOrderPtr)(T&)) { if(this->left !=
Recently I have been studying recursion; how to write it, analyze it, etc. I
If I have a choice to use recursion or memoization to solve a problem
I wrote some code with a lot of recursion, that takes quite a bit
I'm pretty new to the idea of recursion and this is actually my first
As a new .net/C# web begginner, I always get tripped up when I try
I must be doing some stupid mistake. I have a server that returns the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.