![]() In such a case the three values won't have the same type, since the name and the phone number are strings, but contact counter will be a number, so lists wouldn't work. For instance, in a phonebook application we might want to handle the entries by crunching three values into one: the name, phone number, and the number of times we made calls. The elements of a tuple do not need to be all of the same type.We know exactly how many values we need for each point (two – the x and y coordinates), so tuples are applicable. For example, we might want a type for storing 2D coordinates of a point. Therefore, it makes sense to use tuples when you know in advance how many values are to be stored. Tuples have a fixed number of elements ( immutable) you can't cons to a tuple.Tuples and lists have two key differences: Tuples offer another way of storing multiple values in a single value. Human programmers (including this wikibook co-author) get confused all the time when working with lists of lists, and having restrictions on types often helps in wading through the potential mess. They are also one of the places where the Haskell type system truly shines. ![]() Lists of lists allow us to express some kinds of complicated, structured data (two-dimensional matrices, for example). For example, :, ] is valid and will produce, , ], and :, ] is valid and will produce, , ], but :, ] will produce an error message. Lists of different types of things cannot be consed, but the empty list can be consed with lists of anything. Why is the following list invalid in Haskell?.Can Haskell have lists of lists of lists? Why or why not?.Which of these are valid Haskell, and which are not? Rewrite in comma and bracket notation.Which of these are valid Haskell and which are not? Rewrite in cons notation.Let's sort through these implications with a few exercises: Lists of lists can be tricky sometimes because a list of things does not have the same type as a thing all by itself. Because lists are things too, lists can contain other lists! Try the following in the interpreter: Lists can contain anything - as long as they are all of the same type. Using double-quoted strings is just more syntactic sugar. For instance, instead of entering strings directly as a sequence of characters enclosed in double quotation marks, they may also be constructed through a sequence of Char values, either linked with (:) and terminated by an empty list or using the commas-and-brackets notation. That means values of type String can be manipulated just like any other list. Strings are just lists Īs we briefly mentioned in the Type Basics module, strings in Haskell are just lists of characters. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |