Q:

Using lists in Haskell

I'm trying to learn list in Haskell but I have a error that I don't understand
I've been trying to definition arguments, but honestly I don't understand too much of haskell yet.
lefgNLista:: [Int]->[Int]->Int
lefgNLista []=[]
lefgNLista (fre1:fre2) fre3  = if fre1 == fre3 then 1+lefgNLista else fre1 : lefgNLista fre2

Also tried like this but got another error
lefgNLista []=[]=0

I expect the function to count the amount of N numbers in the list, e.g. 
ghci> lefgNLista [1,3,5,7,8,8,2,3,8] 8 
3

A:

Here is a possible definition explained item by item.
I will omit the type. You can give this function the type of [Int]->[Int]->Int if you want, but it's not the best possible type. The compiler is able to infer the best type for you.
There are two parts in the definition, one for the empty list and one for non-empty lists.
How many elements are in an empty list? Zero. How many of those are equal to any given value? Also zero.
lefgNLista [] _ = 0
--         []             ⇒ the empty list
--            _           ⇒ "any given element", we are not looking at it, hence no name
--              =         ⇒ "is defined as"
--                0       ⇒ that's zero

OK now let's look at a non-empty list. This part has two further cases. We look at the first element of the list. Either it equals to what we are counting, or not.
lefgNLista (x:xs) y | x == y = 1 + lefgNLista xs y
                    | otherwise =  lefgNLista xs y
--         (x:xs)                                  ⇒ A customary way to name a list pattern
--                y                                ⇒ The value we want to count
--                  |                              ⇒ Pronounced "when"
--                    x == y                       ⇒ Self evident
--                           =                     ⇒ "is defined as"
--                             1 + lefgNLista xs y ⇒ Compute for the rest of list and add 1 
--                  | otherwise =                  ⇒ Self evident
--                                 lefgNLista xs y ⇒ Just compute for the rest of list

So what is the type of this inferred by Haskell?
Prelude λ :t lefgNLista
lefgNLista :: (Num p, Eq t) => [t] -> t -> p

This is not as intimidating as it seems. You can read this as "when p is some kind of number, and t is some kind of stuff you can check for equality, the type is [t] -> t -> p". So this function works for Int, Double, (Int, Int) or any other type you can compare — even for other lists!
Prelude λ lefgNLista [[1,2], [3,5], [1,2]] [1,2]
2

What about Num p? Int is not the only type you can count with. You may need Integer if your lists are extremely long, or you may want to use your own fancy number-like type, but this is probably way too advanced at this point.

