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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T18:13:39+00:00 2026-06-06T18:13:39+00:00

I wrote my first program in Haskell today. It compiles and runs successfully .

  • 0

I wrote my first program in Haskell today. It compiles and runs successfully. And since it is not a typical “Hello World” program, it in fact does much more than that, so please congrats me 😀

Anyway, I’ve few doubts regarding my code, and the syntax in Haskell.

Problem:

My program reads an integer N from the standard input and then, for each integer i in the range [1,N], it prints whether i is a prime number or not. Currently it doesn’t check for input error. 🙂

Solution: (also doubts/questions)

To solve the problem, I wrote this function to test primality of an integer:

is_prime :: Integer -> Bool
is_prime n = helper n 2
        where
          helper :: Integer -> Integer -> Bool
          helper n i  
              | n < 2 * i = True
              | mod n i > 0 = helper n (i+1)
              | otherwise = False

It works great. But my doubt is that the first line is a result of many hit-and-trials, as what I read in this tutorial didn’t work, and gave this error (I suppose this is an error, though it doesn’t say so):

prime.hs:9:13:
    Type constructor `Integer' used as a class
    In the type signature for `is_prime':
      is_prime :: Integer a => a -> Bool

According to the tutorial (which is a nicely-written tutorial, by the way), the first line should be: (the tutorial says (Integral a) => a -> String, so I thought (Integer a) => a -> Bool should work as well.)

is_prime :: (Integer a) => a -> Bool

which doesn’t work, and gives the above posted error (?).

And why does it not work? What is the difference between this line (which doesn’t work) and the line (which works)?


Also, what is the idiomatic way to loop through 1 to N? I’m not completely satisfied with the loop in my code. Please suggest improvements. Here is my code:

--read_int function
read_int :: IO Integer
read_int = do
     line <- getLine
     readIO line

--is_prime function
is_prime :: Integer -> Bool
is_prime n = helper n 2
        where
          helper :: Integer -> Integer -> Bool
          helper n i  
              | n < 2 * i = True
              | mod n i > 0 = helper n (i+1)
              | otherwise = False

main = do
       n <- read_int
       dump 1 n
       where
           dump i x = do 
                 putStrLn ( show (i) ++ " is a prime? " ++ show (is_prime i) )
                 if i >= x 
                    then putStrLn ("")
                  else do
                    dump (i+1) x
  • 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-06T18:13:40+00:00Added an answer on June 6, 2026 at 6:13 pm

    You are misreading the tutorial. It would say the type signature should be

    is_prime :: (Integral a) => a -> Bool
    --       NOT Integer a
    

    These are different types:

    • Integer -> Bool
      • This is a function that takes a value of type Integer and gives back a value of type Bool.
    • Integral a => a -> Bool
      • This is a function that takes a value of type a and gives back a value of type Bool.
      • What is a? It can be any type of the caller’s choice that implements the Integral type class, such as Integer or Int.

    (And the difference between Int and Integer? The latter can represent an integer of any magnitude, the former wraps around eventually, similar to ints in C/Java/etc.)


    The idiomatic way to loop depends on what your loop does: it will either be a map, a fold, or a filter.

    Your loop in main is a map, and because you’re doing i/o in your loop, you need to use mapM_.

    let dump i = putStrLn ( show (i) ++ " is a prime? " ++ show (is_prime i) )
     in mapM_ dump [1..n]
    

    Meanwhile, your loop in is_prime is a fold (specifically all in this case):

    is_prime :: Integer -> Bool
    is_prime n = all nondivisor [2 .. n `div` 2]
            where
              nondivisor :: Integer -> Bool
              nondivisor i = mod n i > 0
    

    (And on a minor point of style, it’s conventional in Haskell to use names like isPrime instead of names like is_prime.)

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

Sidebar

Related Questions

This is my first C program. hello world! I'm sure this is no problem
I wrote my first program almost fifty years ago (yes, coding is still a
I ported a little Haskell program I wrote from Mac to Windows. It's a
I just wrote my first OpenMP program that parallelizes a simple for loop. I
I wrote my first sample scala program and it looks like this: def main(args:
I wrote a little program to calculate the first 18 triples (x,y,z) with x<y<z
I just wrote my first Python program and it works! It is a program
This is my first program using Haskell. I'm writing it to put into practice
I just wrote my first nodejs program using the node-mongodb-native driver. I used the
I just start coding in PHP, i wrote my first php + mysql program

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.