Writing Code Using Lists

As you saw in the last chapter, lists of values are enclosed in square brackets, and separated by commas. We can make lists of any type we want, but a list can only hold a single type. Here are some examples of different types of lists. Try creating them in ghci:

 λ​ listOfNums = [1, 2, 3]
 λ​ listOfFloats = [1.1, 2.2, 3.3]
 λ​ listOfStrings = [​"hello"​, ​"world"​]

There’s also a special type of list that you’ve already used extensively: strings! In Haskell, regular strings are simply lists of characters. When you create a string using double quotes, it’s really just a nice way of writing a list of individual characters. We can see this for ourselves in ghci:

 λ​ [​'h'​,​'e'​,​'l'​,​'l'​,​'o'​] == ​"hello"
 

Get Effective Haskell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.