Lists Flashcards
Define Lists
It stores elements of the same type
What does let keyword do?
Helps to define a constant in GHCI environment
show syntax of defining a list using let
let a = [1,2,3,4,5]
how to concatenate two lists together
using ++ operator. For example [1,2,3,4] ++ [4,5,6]
Define ++ operator’s drawback
++ operator appends the second list to the end of the first. Due to lazy evaluation, Haskell will attempt to walk through every element in the first list. So if the first list is long, then the ++ operation will take a long time to complete
Describe the cons operator
Symbol “:”. This adds an element of the list to the front. For example, ‘A’ : “ small cat” will produce “A small cat”
Advantage of cons operator
it is fast
Describe [],[[]] and [[],[]]
First is an empty list. Second is a list which contains an empty list. Third is a list which contains two empty lists
Describe !! operator
!! operator can be used to get an element at a particular index from a list.
Give an example for !! operator
“Ramesh” !! 2 will return ‘m’
From what value indeices in Haskell start 1 or 0?
0
Can lists within a list be of different types
no
Can lists within a list can be of different lengths
yes
what operators can be used to compare lists
, >=, ==, /=
In what order list comparison is done in Haskell
Lexicographic order. Left to right