lists
?In Haskell, lists
are a data structure that stores multiple items of the same type.
[1,2,3] -- A list of integers
['a', 'b', 'c'] -- a list of characters
The code below shows how to check if a list
is empty.
isEmpty :: [a] -> BoolisEmpty = \myList ->case myList of[] -> True -- if the list is empty, return true_ -> False -- otherwise, return false-- call function to check if different lists are emptymain = print(isEmpty [1,2,3], isEmpty ["a"], isEmpty [])
In the code above:
isEmpty
that accepts a list
as an argument and returns a Bool
. The placeholder a
indicates that the list
may be of any data type.isEmpty
takes a single list
as a parameter.case-of
statement to check whether or not myList
is an empty list
.myList
is empty, then the function returns True
in line 4; otherwise, it returns False
in line 5.isEmpty
function thrice, with a different list
each time. The first two calls to isEmpty
involve lists
that are not empty, i.e., [1,2,3]
and ["a"]
, so it returns False
for both. The final call to isEmpty
uses an empty list as the parameter, so the function returns True
.