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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:16:02+00:00 2026-05-26T20:16:02+00:00

I need to write the function product in two ways: Using Guards Using if-then-else

  • 0

I need to write the function product in two ways:

  1. Using Guards
  2. Using if-then-else

So that the function return the product of m through n.

example:

product 3 5

returns 3*4*5 = 60

Thanks

  • 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-26T20:16:02+00:00Added an answer on May 26, 2026 at 8:16 pm

    This sounds like a homework problem so instead of just dropping code on you, let’s work through the problem:

    Type Signature

    Haskell is a functional language with strong typing so it is probably best to start by writing the type signature of our function. Your example shows two integer arguments and an integer return value. We code this as:

    product :: Int->Int->Int
    

    This reads as “product is a function that takes two Ints and returns an Int.” (there are other more correct ways to read this but that is for another day)

    Recursion

    we are going to use a common pattern in Haskell. Because we need to keep track of intermediate values in this case the partial product we will write a second function product' that will take an extra parameter, the running product.

    product' :: Int->Int->Int->Int
    product' accumulator current final = product' (accumulator*current) (current+1) final
    

    At every iteration this will take the most recent accumulated value multiply by current and pass that as the new accumulator, it will take current and add 1 to it passing it as the new current, and will pass final unchanged. To get it started we write our original function:

    product i f = product' 1 i f
    

    or in points-free notation

    product = product' 1
    

    The problem is the product' code will loop forever. We need a way to stop when we current is greater then final.

    Guards

    Rather then rewrite the book on guard patterns I’ll send you to the book. In short they let you a boolean before you do something. We’ll use them to stop our recursion.

    product' :: Int->Int->Int->Int
    product' accumulator current final 
        | current <= final = product' (accumulator*current) (current+1) final
        | otherwise = accumulator
    

    So long as current is less than or equal to final we continue to recurse once it’s not the final answer is in accumulator.

    If-Then-Else

    Guards can be replaced with if constructs (perhaps deeply nested) in a mechanical fashion.

    product' :: Int->Int->Int->Int
    product' accumulator current final = 
      if current <= final 
        then product' (accumulator*current) (current+1) final
        else accumulator
    

    Final Thoughts

    Don’t write code like this. There are a number of wonderfully generic higher level function that do just these types of things. Here is just one better way to write product:

    product i f = foldl (*) 1 [i..f]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to write, in JavaScript (I am using jQuery), a function that is
I need to write a function that returns all characters that occur 2 or
I need to write a function that takes 4 bytes as input, performs a
I need to write a function that is passed an instance of a CharField.
I need to write a delegate function that can 'wrap' some while/try/catch code around
I need to write a little ruby function that does word wrapping. I have
I need to be able to write a function that shows repeated words from
Basically what I need to do is write a function that takes in a
I need to write a function that does exactly opposite of preg_quote function. Simply
I need to write a function that takes some HTML, strips out the img

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.