We can omit print in ghci and the result will be the same. Prelude > recurseForever n = n : recurseForever ( n + 1 ) Prelude > print $ take 3 $ recurseForever 5 That means we can write functions that recurse forever without a base case, like the following: In Haskell a function is only evaluated enough to satisfy the demands of the main action (by default, at least). Lazy EvaluationĪnother very unusual core aspect of Haskell is laziness. It strikes me as the most distinctive aspect of Haskell. I think it’s worth emphasizing this point. When the Haskell runtime executes an action, that’s when it will actually perform the I/O, but it’s done as a separate step. We can imagine these as data structures that describe what the desired side effects are. If a function looks as though it’s doing I/O, don’t be fooled, it’s not - at least not directly! That means functions can never have any direct side effects at all. It’s easy to miss the significance of this though. The introduction mentions that all functions in Haskell are pure. Prelude > secondItem ( Cons first ( Cons second rest )) = second Prelude > secondItem items 4 100% Pure Since values in Haskell are immutable, it is possible to match a value against the way it was constructed in the first place! This feature is used a lot in Haskell.įor example, we can define a custom list type and use it to create a list consisting of the values 3, 4, and 5 as follows: Pattern Matchingįor me, the pattern matching was the most unusual feature in this part of the book. There is also an introduction to recursive functions and the holy trinity of higher-order functions, map, filter and fold (also known as reduce in some languages). We are introduced to functions, pattern matching, and conditional logic. Miran does a great job of jumping right into Haskell code in a gentle way that avoids intimidating theory or notation. These early chapters are about getting started. I just had to take my time and work through everything carefully and in detail. I took some time away and returned to it later, determined to make it to the end this time. I found the first two parts fairly easy to get through, but on my first attempt I ran out of steam when I reached the chapters about functors and monads (11 and 12). Chapters 11-14: Monoids functors applicative functors monads zippers.Chapters 8-10: Making our own types and typeclasses I/O solving problems.Chapters 1-7: Intro to types and typeclasses pattern matching recursion higher-order functions modules.LYAHFGG! has a flat structure of 14 chapters, but I tend to think of it more in terms of 3 big parts: Used print versions are also available at Amazon. LYAHFGG! is available for free online, or can be purchased as an e-book from the official Web site. I also included some of my own thoughts, observations, and code samples. These notes are not comprehensive - they’re just kind of a brain dump of the things that stood out for me, either for being interesting, useful, or tricky. While the book isn’t perfect - nothing is - I found it to be quite accessible in introducing the core concepts behind Haskell. It’s definitely the friendliest introduction to Haskell out there. Terms like functor, monoid, and monad can add to the impression that it’s something really complicated. Perusing it at random, Haskell code doesn’t look much like the code many of us are used to in Java, JavaScript, C#, Python, Ruby, etc. I’d been curious, but also a bit intimidated by the idea of learning Haskell. In the past few weeks I’ve gone over the book Learn You a Haskell for Great Good! by Miran Lipovača. Book Notes: Learn You a Haskell for Great Good!
0 Comments
Leave a Reply. |