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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:59:42+00:00 2026-06-09T15:59:42+00:00

How do you increment a variable in a functional programming language? For example, I

  • 0

How do you increment a variable in a functional programming language?

For example, I want to do:

main :: IO ()
main = do
    let i = 0
    i = i + 1
    print i

Expected output:

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-06-09T15:59:43+00:00Added an answer on June 9, 2026 at 3:59 pm

    Simple way is to introduce shadowing of a variable name:

    main :: IO ()                  -- another way, simpler, specific to monads:
    main = do                         main = do
        let i = 0                         let i = 0
        let j = i                         i <- return (i+1)
        let i = j+1                       print i
        print i                    -- because monadic bind is non-recursive
    

    Prints 1.

    Just writing let i = i+1 doesn’t work because let in Haskell makes recursive definitions — it is actually Scheme’s letrec. The i in the right-hand side of let i = i+1 refers to the i in its left hand side — not to the upper level i as might be intended. So we break that equation up by introducing another variable, j.

    Another, simpler way is to use monadic bind, <- in the do-notation. This is possible because monadic bind is not recursive.

    In both cases we introduce new variable under the same name, thus "shadowing" the old entity, i.e. making it no longer accessible.

    How to "think functional"

    One thing to understand here is that functional programming with pure — immutable — values (like we have in Haskell) forces us to make time explicit in our code.

    In imperative setting time is implicit. We "change" our vars — but any change is sequential. We can never change what that var was a moment ago — only what it will be from now on.

    In pure functional programming this is just made explicit. One of the simplest forms this can take is with using lists of values as records of sequential change in imperative programming. Even simpler is to use different variables altogether to represent different values of an entity at different points in time (cf. single assignment and static single assignment form, or SSA).

    So instead of "changing" something that can’t really be changed anyway, we make an augmented copy of it, and pass that around, using it in place of the old thing.

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

Sidebar

Related Questions

Possible Duplicate: ++ operator in Scala I want to increment an Int variable in
I want to increment a variable and - if a particular condition is fulfilled
Basically I want to increment the name of the variable. What is the correct
Can I create a javascript variable and increment that variable when I press a
I am new to jQuery. I am having a variable increment in my code
I have the following variable: pageID = 7 I'd like to increment this number
I want to increment $x in this function to get next record in table.
I want to wirte a function with variable arguments in this way: static void
I have a very specific application where I need an auto-increment variable with persistent
I'm trying to increment a variable in JavaScript by clicking on a link, it

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.