I know the "xs" can be used for expressing the rest elements in a list but I totally have no idea what the "ls" mean in Haskell?
ls
is not a predefined thing. It is whatever you bind it to, just like xs
.
For instance, I think you've seen examples like this:
sum [] = 0
sum (x:xs) = x + sum xs
The variable xs
, that you just defined here, gets bound (will have the value of) the rest of the list because of the pattern (x:xs)
. But this could equally well have been written as:
sum [] = 0
sum (l:ls) = l + sum ls
We prefer not to call a variable l
though, because it is easily confused with the digit 1
(or even the pipe symbol |
on really messed up fonts).
We could even write:
sum [] = 0
sum (head:tail) = head + sum tail
where we reuse the names of the built-in prelude functions head
and tail
, but this is bound to lead to confusion.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With