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 5838063
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T11:25:41+00:00 2026-05-22T11:25:41+00:00

I have used many recursive functions now but still have trouble getting my head

  • 0

I have used many recursive functions now but still have trouble getting my head around how such a function exactly works (i’m familiar with the second line (i.e. | n==0 = 1) but am not so familiar with the final line (i.e. | n>0 = fac (n-1) * n)).

fac :: Int -> Int
fac n
    | n==0 = 1
    | n>0  = fac (n-1) * n
  • 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-22T11:25:41+00:00Added an answer on May 22, 2026 at 11:25 am

    Recursive algorithms are very closely linked to mathematical induction. Perhaps studying one will help you better understand the other.

    You need to keep two key principles in mind when using recursion:

    • Base Case
    • Inductive Step

    The Inductive Step is often the most difficult piece, because it assumes that everything it relies upon has already been computed correctly. Making this leap of faith can be difficult (at least it took me a while to get the hang of it), but it is only because we’ve got preconditions on our functions; those preconditions (in this case, that n is a non-negative integer) must be specified so that the inductive step and base case are always true.

    The Base Case is also sometimes difficult: say, you know that the factorial N! is N * (N-1)!, but how exactly do you handle the first step on the ladder? (In this case, it is easy, define 0! := 1. This explicit definition provides you with a way to terminate the recursive application of your Inductive Step.)

    You can see your type specification and guard patterns in this function are providing the preconditions that guarantee the Inductive Step can be used over and over again until it reaches the Base Case, n == 0. If the preconditions can’t be met, recursive application of the Inductive Step would fail to reach the Base Case, and your computation would never terminate. (Well, it would when it runs out of memory. 🙂

    One complicating factor, especially with functional programming languages, is the very strong desire to re-write all ‘simple’ recursive functions, as you have here, with variants that use Tail Calls or Tail Recursion.

    Because this function calls itself, and then performs another operation on the result, you can build a call-chain like this:

    fac 3        3 * fac 2
      fac 2      2 * fac 1
        fac 1    1 * fac 0
          fac 0  1
        fac 1    1
      fac 2      2
    fac 3        6
    

    That deep call stack takes up memory; but a compiler that notices that a function doesn’t change any state after making a recursive call can optimize away the recursive calls. These kinds of functions typically pass along an accumulator argument. A fellow stacker has a very nice example: Tail Recursion in Haskell

    factorial 1 c = c
    factorial k c = factorial (k-1) (c*k)
    

    This very complicated change 🙂 means that the previous call chain is turned into this:

    fac 3 1       fac 2 3
      fac 2 3     fac 1 6
        fac 1 6   6
    

    (The nesting is there just for show; the runtime system wouldn’t actually store details of the execution on the stack.)

    This runs in constant memory, regardless of the value of n, and thus this optimization can convert ‘impossible’ algorithms into ‘possible’ algorithms. You’ll see this kind of technique used extensively in functional programming, much as you’d see char * frequently in C programming or yield frequently in Ruby programming.

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

Sidebar

Related Questions

I have used many of the Convert.To..... functions for conversion , but I didn't
I have seen many recursive functions(mostly used in computing some mathematical operations e.g. factorial,
I have used Dictionary(TKey, TValue) for many purposes. But I haven't encountered any scenario
I have used maps many times before, but this is the first time I
I am using colorbox which I have used many times for different clients, but
I'm an ASP.NET MVC newbie, but have used many Model-View-Controller frameworks previously. I recently
I've been working on this problem for a few hours and have used many
How can I set an image for UITableViewController ? I have used many things
I have not used many lambda expressions before and I ran into a case
I have used Hibernate in the past and I share many people's frustration with

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.