What is the difference between [a] and "a" in Haskhell?

Asked

Viewed 51 times

3

That is correct??

[a] = a list with only 1 element
a = a list of all the elements you want

Ps: I have some doubts because some functions are of the type [a]->a but also read , for example [2,3]

1 answer

3


It’s not right when some function says func :: [a] -> a is informing us that it takes as parameter any list (of any size) and returns an element of the same type of the list. The variable to can be of any type, if the list is numbers then the result will be a number.

Let’s take an example:

The scope of the function head in Haskell (:t head):

head :: [a] -> a
head [1,2,3] -- 1
head [a,b,c] -- a  

This function returns the first element of a list (list head). It can be a list of any size, just test to check.

Already the function Tail, which returns the tail of the list, has a slightly different scope:

tail :: [a] -> [a]
tail [1,2,3] -- [2,3]
tail [a,b,c] -- [b,c]

Since the tail may have 0 or more elements.

If it was an Equals function it would be something like this:

eq :: a -> a -> Bool

That is, it takes two parameters of the same type and returns true/false. To learn more about Haskell recommend this online book.

  • Thank you very much :p , already understood

  • Could you mark the answer as accepted? Below the arrows v has a green OK.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.