background preloader

The Haskell Cheatsheet

The Haskell Cheatsheet

CIS 194 Mondays 1:30-3Towne 309 Class Piazza site Instructor: Brent Yorgey Email: byorgey at cisOffice: Levine 513Office hours: Friday 2-4pm TAs: Adi Dahiya (office hours: Thursdays 1-3pm, Moore 100)Zach Wasserman (office hours: Thursdays 12-1pm, Moore 100) Haskell is a high-level, pure functional programming language with a strong static type system and elegant mathematical underpinnings, and is being increasingly used in industry by organizations such as Facebook, AT&T, and NASA. Lenses In Pictures - adit.io You should know what a functor is before reading this post. Read this to learn about functors. Suppose you want to make a game: Ok, now how would you move this player? moveX (Mario (Point xpos ypos)) val = Mario (Point (xpos + val) ypos) Instead, lenses allow you to write something like this: location.x `over` (+10) $ player1 Or this is the same thing: over (location . x) (+10) player1 Lenses allow you to selectively modify just a part of your data: Much clearer! location is a lens. Fmap You probably know how fmap works, Doctor Watson (read this if you don’t): Well old chap, what if you have nested functors instead? You need to use two fmaps! Now, you probably know how function composition works: What about function composition composition? “If you want to do function composition where a function has two arguments”, says Sherlock, “you need (.).(.)!” “That looks like a startled owl”, exclaims Watson. “Indeed. The type signature for function composition is: (.) :: (b -> c) -> (a -> b) -> (a -> c) Setters

Typeclassopedia By Brent Yorgey, byorgey@cis.upenn.edu Originally published 12 March 2009 in issue 13 of the Monad.Reader. Ported to the Haskell wiki in November 2011 by Geheimdienst. This is now the official version of the Typeclassopedia and supersedes the version published in the Monad.Reader. Please help update and extend it by editing it yourself or by leaving comments, suggestions, and questions on the talk page. The standard Haskell libraries feature a number of type classes with algebraic or category-theoretic underpinnings. The goal of this document is to serve as a starting point for the student of Haskell wishing to gain a firm grasp of its standard type classes. Have you ever had any of the following thoughts? What the heck is a monoid, and how is it different from a monad? If you have, look no further! There are two keys to an expert Haskell hacker’s wisdom: Understand the types. The second key—gaining deep intuition, backed by examples—is also important, but much more difficult to attain.

The Monad.Reader | Binding your input since 2005 haskell - A monad is just a monoid in the category of endofunctors, what's the problem How to pick your string library in Haskell Notice. Following a critique from Bryan O’Sullivan, I’ve restructured the page. “How do the different text handling libraries compare, and when should we use which package?” There are, however, a lot more niche string handling libraries on Hackage. Binary or text? Based on these questions, here are where the string libraries of Hackage fall: Beyond in-memory encoding, there is also a question of source and target encodings: hopefully something normal, but occasionally you get Shift_JIS text and you need to do something to it. Unicode joke. Well done, mortal! Alt text.

Related: