If Something Is Not A List In Haskell
Asked Answered
C

3

5

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?

Carriole answered 11/12, 2011 at 11:59 Comment(2)
In short: you shouldn't ever need this. Haskell is statically typed. You know that a is an integer because you assigned an integer to it. The compiler will figure this out automatically. If you provide some more context about what you're trying to achieve, we can help better.Beore
Why do you need to know this? As jmg below says, Haskell is statically typed, and in all situations I can think of, you either already know or don't care.Atrophied
E
24

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.

Elexa answered 11/12, 2011 at 12:16 Comment(1)
"Haskell will make sure your function can only be called with a list." - if you want to know more, the term to google is "Type inference". See wikipedia, haskellwiki.Cymbal
S
6

Haskell is a statically typed, i.e. you know at compile time whether an identifier denotes something of type [Int] or of Int.

Serration answered 11/12, 2011 at 12:6 Comment(0)
R
4

There is ghci :t (:type) command for that:

> let a = 55
> :t a
a :: Integer
> let b = [1..5]
> :t b
b :: [Integer]

Hence, a is just an Integer, b is a list of Integer.

Royalty answered 11/12, 2011 at 12:3 Comment(1)
thanks but want to check it inside a funtion body. the let can be let a='v'.Carriole

© 2022 - 2024 — McMap. All rights reserved.