notes

Personal notes
git clone git://git.laack.co/notes.git
Log | Files | Refs

Lists.md (1812B)


      1 # Lists (Haskell)
      2 
      3 **Source:** Effective Haskell
      4 
      5 **Chapter:** 1
      6 
      7 ## Usage
      8 
      9 ### Basics
     10 
     11 Definition of a list is simple:
     12 
     13 [1,2,3]
     14 
     15 [1..10]
     16 
     17 ["one", "two", "three"]
     18 
     19 These are all valid list definitions. The second one works similar to the seq command as it fills in all numbers between the lower and upper bound (inclusive on both sides).
     20 
     21 More complex examples:
     22 
     23 [2,4..10] -> [2,4,6,8,10]
     24 
     25 [4,3..1] -> [4,3,2,1]
     26 
     27 [0,10..100] -> [0,10,20,30,40,50,60,70,80,90,100]
     28 
     29 In these examples, we see the difference between the first two numbers is used to fill in the numbers between the second number and the upper bound. 
     30 
     31 [0,10..97] -> [0,10,20,30,40,50,60,70,80,90]
     32 
     33 We see our rule enumerates all numbers [lower, upper].
     34 
     35 A few other examples that needn't be explained, but illustrate the point:
     36 
     37 [10..1] -> []
     38 
     39 [10..10] -> [10]
     40 
     41 ### Math Relation
     42 
     43 A nice way to think about this is from the set theoretic perspective.
     44 
     45 I can state the even numbers in $N_0$ up to 10 as the set $\{0, 2, ... 10\}$, or I could define them in Haskell with [0,2..10]. The main differences I notice between the two is that $\{0,-2,...10\}$ doesn't make sense as a set definition, but [0,-2..10] works in Haskell (although it evaluates to []), and that $\{0,23,...100\}$ doesn't make much sense, but in Haskell [0,23..100] is the same as [0,23,46,69,92].
     46 
     47 ### Arithmetic Expressions
     48 
     49 [10 + 2, 10 * 2, 10 - 2, 10 / 2] -> [12.0, 20.0, 8.0, 5.0]
     50 
     51 ### Types
     52 
     53 Lists can't have mixed types. 
     54 
     55 [10, "ten"] is invalid. It also has implicit type casting so [10, 10.0] is valid and evaluates to [10.0, 10.0].
     56 
     57 ### Lists of Tuples
     58 
     59 Lists can contain tuples to get around the issue of lists not being able to store multiple types of values. Despite this, each of the contained tuples must then be the same size and have the same types at each index.