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

3

Is this correct?

[a] = a list with 1 single element
a = a list with as many elements as you want

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

    
asked by anonymous 07.10.2017 / 19:40

1 answer

3

Is not correct, when any function says func :: [a] -> a is telling us that it takes as a parameter any list (of any size) and returns an element of the same type from the list. The a variable can be of any type, if the list is of numbers then the result will be a number.

Let's look at an example:

The scope of the head function 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 (head of the list). It can be a list of any size, just test to verify.

The tail function, 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 can have 0 or more elements.

If it were an Equals type function it would look something like this:

eq :: a -> a -> Bool

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

    
07.10.2017 / 21:02