Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "ls" mean in Haskell?

Tags:

haskell

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?

like image 379
XIA Yang Avatar asked Nov 29 '22 17:11

XIA Yang


1 Answers

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.

like image 82
Thomas Avatar answered Dec 05 '22 04:12

Thomas