How do i check if an object in Haskell is not a list? for instance i want to know if
let a = 55, a is a list or just a number?
How do i check if an object in Haskell is not a list? for
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
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.
You don’t check. You do.
But really, what are you trying to do here?
If you are trying to ensure your function can only be called with a list
Haskell will make sure your function can only be called with a list. If you try to call your function with a non-list, this will cause a compile error.
e.g.
then
If you want your function to do something sensible whether it is called with a list or with something else
Use a typeclass: write different versions of your function for when it is called with a list and for when it is called with other types (within reason); Haskell then ensures that the proper version of your function is called.
e.g.
then
Often the list version of your function will want to call the non-list version of your function and combine the results in some way.
Which is appropriate in your situation depends on exactly what you’re trying to do. If a typeclass is appropriate, you may be able to reuse a standard typeclass.