Jump to content

Haskell Error

LtStaffel
Go to solution Solved by sup_{LIFE},

There are a few type errors in your code.

theFilter :: Integer -> Integer

has an incorrect type signature because the function produces the value True, which is a data constructor for the Bool type. So theFilter should be Integer -> Bool.

 

The function body is also incorrect. Your second guard

en `mod` 2 == True

is comparing values of two different types. mod produces a value with an instance of the Integral typeclass, and True is (as mentioned above) a data constructor for the Bool type. == requires the values being compared to have the same type (Eq a => a -> a -> Bool).

 

Also in the second guard, you call

theFilter (en / 2)

but / is Fractional a => a -> a -> a, and theFilter takes an Integer argument. You want either quot or div depending on the behavior you are looking for.

 

theMainer :: Integer -> Boolean

is wrong because there is no Boolean type constructor. What you are looking for is Bool, so theMainer should be Integer -> Bool. The other problem with this function is that the value produced by each guard has a different type. Your function signature says the function gives a Bool value, and your second guard (otherwise) matches that, but show (in your first guard) has the signature Show a => a -> String.

 

Finally, you are using a top-level expression (map theMainer numbers), which is not allowed in Haskell. If you want to see the result of map, either remove it from the file and do it from ghci, or put it into main:

 

main = print $ map theMainer numbers

 

Hello,

 

I do not see anything wrong with my haskell program but I am new to haskell so I can't figure out what is wrong.

I get an identation or bad bracket error while trying to compile.

Code:

numbers = [1..10000000000000000000]

theFilter :: Integer -> Integer
theFilter en
    | en == 1 = True
    | en `mod` 2 == True = theFilter (en / 2)
    | otherwise = theFilter ((3 * en) + 1)

theMainer :: Integer -> Boolean
theMainer passed
    | theFilter passed == True = show passed ++ " can get to 1."
    | otherwise = False

map theMainer numbers

Thanks in advance!

-LtStaffel

Join the Appleitionist cause! See spoiler below for answers to common questions that shouldn't be common!

Spoiler

Q: Do I have a virus?!
A: If you didn't click a sketchy email, haven't left your computer physically open to attack, haven't downloaded anything sketchy/free, know that your software hasn't been exploited in a new hack, then the answer is: probably not.

 

Q: What email/VPN should I use?
A: Proton mail and VPN are the best for email and VPNs respectively. (They're free in a good way)

 

Q: How can I stay anonymous on the (deep/dark) webzz???....

A: By learning how to de-anonymize everyone else; if you can do that, then you know what to do for yourself.

 

Q: What Linux distro is best for x y z?

A: Lubuntu for things with little processing power, Ubuntu for normal PCs, and if you need to do anything else then it's best if you do the research yourself.

 

Q: Why is my Linux giving me x y z error?

A: Have you not googled it? Are you sure StackOverflow doesn't have an answer? Does the error tell you what's wrong? If the answer is no to all of those, message me.

 

Link to comment
Share on other sites

Link to post
Share on other sites

There are a few type errors in your code.

theFilter :: Integer -> Integer

has an incorrect type signature because the function produces the value True, which is a data constructor for the Bool type. So theFilter should be Integer -> Bool.

 

The function body is also incorrect. Your second guard

en `mod` 2 == True

is comparing values of two different types. mod produces a value with an instance of the Integral typeclass, and True is (as mentioned above) a data constructor for the Bool type. == requires the values being compared to have the same type (Eq a => a -> a -> Bool).

 

Also in the second guard, you call

theFilter (en / 2)

but / is Fractional a => a -> a -> a, and theFilter takes an Integer argument. You want either quot or div depending on the behavior you are looking for.

 

theMainer :: Integer -> Boolean

is wrong because there is no Boolean type constructor. What you are looking for is Bool, so theMainer should be Integer -> Bool. The other problem with this function is that the value produced by each guard has a different type. Your function signature says the function gives a Bool value, and your second guard (otherwise) matches that, but show (in your first guard) has the signature Show a => a -> String.

 

Finally, you are using a top-level expression (map theMainer numbers), which is not allowed in Haskell. If you want to see the result of map, either remove it from the file and do it from ghci, or put it into main:

 

main = print $ map theMainer numbers

 

Link to comment
Share on other sites

Link to post
Share on other sites

21 hours ago, sup_{LIFE} said:

-snip-

Got it working, thanks!

Join the Appleitionist cause! See spoiler below for answers to common questions that shouldn't be common!

Spoiler

Q: Do I have a virus?!
A: If you didn't click a sketchy email, haven't left your computer physically open to attack, haven't downloaded anything sketchy/free, know that your software hasn't been exploited in a new hack, then the answer is: probably not.

 

Q: What email/VPN should I use?
A: Proton mail and VPN are the best for email and VPNs respectively. (They're free in a good way)

 

Q: How can I stay anonymous on the (deep/dark) webzz???....

A: By learning how to de-anonymize everyone else; if you can do that, then you know what to do for yourself.

 

Q: What Linux distro is best for x y z?

A: Lubuntu for things with little processing power, Ubuntu for normal PCs, and if you need to do anything else then it's best if you do the research yourself.

 

Q: Why is my Linux giving me x y z error?

A: Have you not googled it? Are you sure StackOverflow doesn't have an answer? Does the error tell you what's wrong? If the answer is no to all of those, message me.

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×