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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T18:50:51+00:00 2026-06-18T18:50:51+00:00

I am trying to understand how to write functions using tail recursion in Haskell.

  • 0

I am trying to understand how to write functions using tail recursion in Haskell. In my example below, the function takes in a list and outputs the maximum value in the list. My intention is to use the c variable to store the current max. I was wondering if someone can explain how using tail recursion would work for this instance?

    myMax [] c = error "max of empty list"
    myMax [x] c = x
    myMax (x:xs) c = 
                if x > myMax xs then c = x
                else myMax xs c

--currently getting a parse error
  • 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-18T18:50:52+00:00Added an answer on June 18, 2026 at 6:50 pm

    There are a couple things to think about here. First You don’t want the user to have to enter some beginning value, so we want a function that takes only a list as its parameter. Since you want a tail recursive implementation we do need a function that takes a second parameter though, so we’ll create an inner function named go which takes the current max and the remaining list.

    myMax [] = error "Empty List"
    myMax (x:xs) = go x xs  -- Initialize current max to head of list.
      where
        -- go takes the current max as the first argument and the remaining list
        -- as the second.
        -- m is the current max, if there are no more elements it is the max.
        go m [] = m 
        -- Otherwise we compare m to the current head.
        -- If the head (y) is greater than m it becomes the current max.
        go m (y:ys) = if m > y then go m ys else go y ys
    

    Note that we never changed the value of any variable here. We update the current max value
    by passing it as a parameter to the next step in the function. This is critical to understand in Haskell because mutating variables is not allowed.

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

Sidebar

Related Questions

I am trying to write a function that will understand how to login using
I'm trying to write a recursive power function without using math.h / pow(), but
Im trying to understand how to create a custom print function. (using python 2.7)
I am trying to write a function that takes as arguments the name of
I am trying to understand type members in Scala. I wrote a simple example
I'm trying to write a simple PDF viewer using CGPDFDocument , based on QuartzDemo.
I am trying to understand the Simulink (continuous) transfer function. In the documentation for
I am trying to implement a Client/Server model using TCpClient , with its Networkstream.Write()/Read()
I trying to marshaling C# code from WinApi functions.. But i understand.. WHYYY WHYY
I'm learning Haskell and stuck trying to understand the type system. I'm trying to

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.