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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:32:18+00:00 2026-05-27T15:32:18+00:00

How do i check if an object in Haskell is not a list? for

  • 0

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?

  • 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-27T15:32:19+00:00Added an answer on May 27, 2026 at 3:32 pm

    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.

    myFunction :: [a] -> String
    myFunction []  = "no elements!"
    myFunction [_] = "single element!"
    myFunction _   = "many elements!"
    

    then

    myFunction [42] -- >>> "single element!"
    myFunction 42   -- ERROR: not a list
    

    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.

    class MyClass a
      where myFunction :: a -> String
    
    instance MyClass Int
      where myFunction 0 = "zero!"
            myFunction 1 = "one!"
            myFunction _ = "something!"
    
    instance MyClass a => MyClass [a]
      where myFunction []  = "no elements!"
            myFunction [x] = "only " ++ myFunction x
            myFunction _   = "many elements!"
    

    then

    myFunction [42] -- >>> "only something!"
    myFunction 42   -- >>> "something!"
    

    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.

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

Sidebar

Related Questions

I regularly want to check if an object has a member or not. An
I'd like to check if an object is a number so that .ToString() would
Essentially, I want to check another object to see if an attribute is set
Almost every time I want to check object's equality to null I use the
Performance/good practice question - should I check if object exists or just delete it?
How do I check if an object is of a given type, or if
How do I check if an object property in JavaScript is undefined?
I would like to check whether an object (e.g. someObject ) is assignable (cast-able)
How do I check if an object given to me is an int[] in
I would like to check if an object is defined or exists using C#.

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.