We are cruising right along. Getting some of the finer details of list, recursion, closure, and pattern matching in Haskell down!
This is a big one. So much of the data structure and transforming is using lists! So we’ve got to get a really good understanding of this if we want to move forward and not fall on our faces. And by faces, I mean get over that hump and really understand Haskell and the great things it has to offer.
Side rant. Damn it there are a lot of updates. I mean like a lot a lot
Starting simple. Head and Tail
Prelude> head [1, 2, 3]
1
Prelude> head []
*** Exception: Prelude.head: empty list
Prelude>
The cons operator. This eluded me for such a long time. I kept getting it confused with a punctuation for a type signature. And frankly never stopped to understand the basic syntax of Haskell. Sure, I read some of the documentation. But I didn’t explicitly type it out like I am not, and it constantly came back to bit me in the ass. So what is cons ? it’s an infix operator, (cons is short for construct. like construct a list. get it?) So we take a value and “cons” it to a list. And we have a list. And a list can always be represented by a value ‘consed’ with a list.
Every element of a list MUST be the same type. Whether that’s string, char, tuples, whatever.
I like the examples that ‘desugar’ some of the syntax in steps.
Then we can concat with ’++’
Common List functions
elem: takes a value and a list and returns True/False if it’s in that List
List Final
Prelude> subseq start end list = take diff list where diff = end - start
Prelude|
Prelude> subseq 1 5 [1..10]
[1,2,3,4]
Prelude> subseq 3 5 [1..10]
[1,2]
Prelude>
ok missed the drop part, and that the start and end ARE the indexes making this WAY easier
Prelude> subseq start end xs = take (end - start + 1) (drop start xs)
Prelude> subseq 2 5 [1..10]
[3,4,5,6]
Prelude> subseq 3 5 [1..10]
[4,5,6]
Prelude> subseq 3 5 [10..20]
[13,14,15]
Prelude> subseq 2 7 "a puppy"
"puppy"
Prelude> drop 2 "a puppy"
"puppy"
Prelude> take 6 "puppy"
As we just learned. We don’t have looping functions like for, while, and until. Which is instinctively where my brain goes. When presented with a list.
No, not Higher Order Components.
Higher Order Functions!
JavaScript to Elm
Jesse Tomchak