189 points · by kmooney · 11y ago · 120 comments
http://www.xent.com/pipermail/fork/Week-of-Mon-20070219/0441...
After I read it, I immediately began learning Haskell. Since then, Haskell made my brain melt multiple times. But it has also enabled me to build an expression parser and analytic differentiator that I use every day (among other things, but that's what I'm most proud of). Such a program would be a lot harder to create, if it weren't for Haskell's ridiculously powerful type system and pattern matching.
Also, after learning the Haskell way of things, I recognized the reasons for many issues I had with code written in another programming language. Don't just go out there and do object-oriented programming because everybody else seems to do it, often it is imperative (pun intended) to use the declarative (functional) programming paradigm to keep you out of debugging hell.
edit: clarification
Then you realize that Haskell has been sleeping around. A lot.
It's time to get tested.
Although, I think, if someone reads it who's not familiar with programming languages, the reactions, when you tell them what Haskell and Python are, might range from hilarious to WTF.
Edit: The book was enjoyable and a delight to read, but I started struggling to keep up from Chapter 8. as it went into Monoids and Monads, I feel like I need to read other tutorials before coming back to it.
Don't read the "Monad tutorials" and especially do not stop programming in Haskell until you "learn monads". Find one of the respected books, read it and do the exercises.
You do not need to thoroughly understand monads in order to do some IO programming just like a violinist does not have to understand the physics of vibrating strings or you don't need to be an auto mechanic to drive to the grocery store.
Write the "Guess the Number" game in Haskell using IO and you should understand the basics and that gets you far. Reading and writing to/from files and network sockets, etc works pretty much the same as you're used to from imperative programming languages. "Guess the number" contains just enough control flow (repetition, conditionals) to get you started.
I've written tens of thousands of lines of Haskell code, including production code at work, and I still don't know what a Monoid is. I have only a faint theoretical understanding of monads, but I am fluent writing IO code and applying monads to other problems like parsing and error handling.
You should only study monads when you're fluent doing some basic imperative programming in Haskell or if you are a math student and have a keen interest in category theory.
That is surprising. They show up all over in fairly common libraries - for instance, Writer collects a monoidal value. The very simplest notion is "monoid is for things you can combine". Taken from math, a monoid is a set of things (a type) for which you have an operation (called mappend in Haskell) which you can combine associatively, where the set contains an element (mempty in Haskell) that is an identity with respect to that operation.
Some common monoids are:
Anything list-like:
mappend appends two lists
mempty is the empty list
Integers over addition (newtype Sum)
mappend adds two integers
mempty is zero
Tuple of monoids:
mappend is componentwise mappend
mempty is (mempty, mempty)
Functions where the domain and range are the same:
mappend is function composition
mempty is idI agree completely that it's not necessary (and maybe even detrimental) to learn monads to use Haskell's IO, but I think this claim is dubious. Not too many imperative languages have lazy IO and this feature can be a surprising hurdle for the Haskell beginner who is versed in other languages.
My advice is to use the language until you gain an intuition for them. Despite being rather simple, I found it was difficult to fully appreciate them without a decent amount of exposure. Really though, monads aren't what Haskell is all about, and don't let an incomplete understanding of them stop you from diving in.
[1] http://www.reddit.com/r/haskell/comments/2vm0ed/is_there_a_g...
http://homepages.inf.ed.ac.uk/wadler/topics/monads.html
http://homepages.inf.ed.ac.uk/wadler/
Alternatively, if you really are allergic to LaTeX, or beards, the recently Oscared sigfpe’s “You Could Have Invented Monads! (And Maybe You Already Have.)”
http://blog.sigfpe.com/2006/08/you-could-have-invented-monad...
There are no other monad tutorials. Just forget about them.
A monadic data type is a container that can be mapped and flattened.
- It's a container, because it stores has an inner type, which refers to the data it contains. For example, think of the type parameter that describes the elements of a list.
- It is "mappable" in the sense that its contained type can be converted to another type by providing an operation from the first type to the second type. Imagine converting a list of strings to a list of integers, where each integer is the length of the corresponding string.
- It can be "flattened" in the sense that if its inner type is an extra nesting of the container type, it can be flattened to a just one level of the container type. For example, a list of lists of integers can be flattened to just a list of integers by concatenating the sublists.
- It seems kind of trivial to say, but there has to be a way to construct a container given one of the elements it's supposed to contain.
There are also a couple of rule these operations have to fulfill. They're kind of abstract when stated on their own, but make intuitive sense when you consider examples.
Pretty simple, huh? What's cool about it is that you can assign all sorts of metadata to the container type and semantics to the flatten operation, and you get a nice representation of a pipeline of operations, where each one depends on the all the previous steps. This is basically just imperative programming -- each step depends on the current context and can optionally mutate that context, affecting subsequent steps.
You might ask, "but isn't imperative programming what we're trying not to do?" In the most general sense with mutable state everywhere, yes. But monads give you a way to describe imperative logic in very tightly controlled contexts.
No, that is one use for Monads. A Monad is just something that has a bind (>>=) function and a return function.
You realise that this is the most useless description ever? While I'm sure it's right, it doesn't actually mean anything. Why's it a good abstraction for doing <X>, better than any other abstraction? If it abstracts over a gazillion different things, why not have special-purpose abstractions like other languages (and thus probably make the language a lot simpler to use)?
Your description is on the same level as "programming languages are structured text".
I don't mean that as any sort of "gotcha", just some additional clarification - especially if people read your "just" too strong.
The GP's explanation of the concrete features (with simple examples!) is one of the most useful explanations of monads I've seen. It isn't complete, but as a gateway to a complete explanation it is extremely good.
http://adit.io/posts/2013-04-17-functors,_applicatives,_and_...
http://www.stephanboyer.com/post/9/monads-part-1-a-design-pa...
[1] http://book.realworldhaskell.org/
[2] https://www.edx.org/course/introduction-functional-programmi...
"If we think of a list as a monster, here's what's what."
- It needs some brain melting to learn but it ultimately makes you a better coder. Time is not wasted on learning it. - A new exciting mindset !
- When you feel you're doing something repetitive, it's nearly sure there is a way to do it effortless.
- Advanced stuff
There's Real World Haskell by Don Stewart (http://book.realworldhaskell.org/read/index.html), though the order of exposition is different and your mileage may vary with the exercises.
I did find that "Programming in Haskell" by Graham Hutton was a very clear read and had good programming exercises.
(and you can make it better if you find anything to complain about! but seriously, it's superb)
https://github.com/facebook/Haxl
My 2 cents: definitely _also_ learn OCaml down the line. But:
- my pet peeve with OCaml is the lack of a builtin type for Unicode strings (the only other language still commonly used with this shortcoming is PHP), and this could be an issue if you plan to write some web app code that needs to be internationalized
- OCaml is strict, Haskell is one of the very few languages that is non-strict: aside from the didactic purpose of exposing yoursefl to something different, that's a feature that can help reasoning about what your code is doing:
http://augustss.blogspot.co.uk/2011/05/more-points-for-lazy-...
(then again, there're obviously downsides to non-strictness, and you might decide down the line that you prefer to write strict code in production... but it's good to know the alternatives)
In context of this question, It seems that learning both Haskell and oCaml is what people recommend.
If I'm learning a language for my own enjoyment, I'll learn whatever I want. If I'm learning a language to try to develop my career, I'll learn something that people want to pay for.
That said, I agree with berdario that you should learn both languages instead of only one of them. There is lots of terminology that is shared between FP languages too so it shouldn't be very hard either.
That is: At least 90% of code is not CPU-limited. It's fast enough. So, as an initial approach, ignoring performance is fine.
The problem is that, if and when you run into real performance issues, you need to know some tools for dealing with them. If this book doesn't teach you those, well... it's a language introduction. It's not an advanced text. Maybe it would be the better for a warning or two, a paragraph here or there, but performance is (quite properly) not the point for the book.
I guess I mean to say that I really like this book, and would enjoy more from the author teaching fast Haskell, to supplement this book about simple Haskell.
"You're fat! Lose some weight, fatty!","You're supposedly normal. Pffft, I bet you're ugly!" and "You're a whale, congratulations!", "Sure, we could just type them all out but obviously that's not a solution for gentlemen who demand excellence from their programming languages."
"You're healthy at every size! Continue being body positive, healthy!", "You're supposed normal. Pfft, society has an unrealistic standard of beauty!" and "You're absolutely perfect with no flaws, defects, or relevant analogies that can be made, congratulations!", "Sure, we could just type them all out but obviously that's not a solution for genderfluid feministia who demand equality from all xer programming languages."
I think it's unlikely that he was trying to be malicious and is probably just not sensitive to what it's like to be on the receiving end of some of his examples.
While I respect your sensibility, I would have made the same kind of remarks without thinking it would hurt someone. I understand it could, but it wouldn't have been my intent. Also remember that your position is very very US-centric. But I am all for communication : explain your point to the author and propose something else that is as funny as the current text.
But saying the author chose not to is simply dishonest.
I guess LYAH and I just aren't a good "culture fit."
It's a real shame too -- without these completely unnecessary comments that add no value, this is an excellent way to learn Haskell! I've become comfortable with my whale-like properties and I'm male, so I had the lovely privilege of learning from this particular resource. It would be great if people not in my position could too.
Maybe the author did some research and determined that the added "humor" and "casual attitude" engages more readers than it turns away. I'd still want to know the demographics of both groups involved, because I suspect one of the groups looks a lot like me and the other does not.
Edit: This lack of practical approach is what makes me hesitate in contrast to clojure (just to mention a language), especially if the only goal is to become again a "better developer" rediscovering functional terms and abstractions also documented in other languages.
> For example there is almost no information about how to write a smartphone videogame in haskell
This has been done before but most of the "hard stuff" here is getting your game built and deployed on a smartphone. You can find Haskell for Android articles if you look around.
There are examples of game programming in Haskell too.
> It is possible to interoperate with the host language?
There is no concept of "host" lanugage, Haskell emits native code.
You can call native code from Haskell and Haskell code from C or other native languages.
> Does it perform well for high performance activities?
Haskell is good enough for most "high performance" activities (it's one of the highest performing high level languages according to the debian shootout), but caveats may apply with long running processes and garbage collection, etc.
> Is it possible to isolate some kind of events in a thread?
Yes. Haskell has very nice threading facilities and high-level concurrency primitives, including stuff similar to "channels" from golang.
These guys are doing it pretty seriously: http://keera.co.uk/blog/all-posts/
And frankly a smartphone videogame is a pretty specialized use case; a lot of pretty mainstream languages (perl?) don't have a good story for how to do that. In fact I've never heard of anyone writing a smartphone game in Clojure - I guess you could do one for Android since it's just java bytecode, but does anyone?
"Practical" for most programmers is server-side business apps, and Haskell is very very good at those.
> Does it perform well for high performance activities?
Yes, very much so.
Javascript is easier, ghcjs (while still a bit tricky to install) is 1.0 and lets you compile arbitrary GHC Haskell (including all extensions, lightweight threads and STM) to JS for both Node.js and the browser. Template Haskell support is on the way too.
Calling the C FFI from Haskell is fairly trivial in my experience. If you have structs you want to manipulate from inside haskell it's a bit cumbersome and confusing initially, but if you're just using primitive types and pointers it's easy.
Haskell does very well for high-performance when it comes to IO, as-of GHC 7.8's new IO manager the fastest Software-Defined Networking (SDN) application is written in Haskell [1]. In terms of pure computation bound performance Haskell, does well enough, optimising truly high-performance Haskell is as much arcane art as optimising C (we're talking "I want microsecond inner-loops" level of optimising). In general I would say that you should expect Haskell to be around JIT Java levels of speed for most cases where the code is not blatantly inefficient. So certainly an order of magnitude faster than python/ruby.
Lots of people complain about performance reasoning in Haskell being hard, which is partially true. But mostly, IMO it is simply different from doing so in C, so you need to unlearn/relearn all your performance tweaking skills.
As for Haskell vs Clojure and becoming a better developer. I think Clojure looks like an interesting language, but I would recommend learning Haskell anyway. Why? Because Haskell is one of the "most different" languages in which people are still writing lots of real world code. Which means that it will expose you to the largest number of new concepts while still having decent practical library support. Key features, IMO are: Purity, type system, laziness, and type classes.
Incidentally, I would urge everyone to please stop using this silly "transpile" word. Compile is already source-to-source and introducing a new silly sounding word for it isn't helping anyone.
[1] - http://haskell.cs.yale.edu/wp-content/uploads/2013/08/hask03...
I don't agree. First, of all, the naive solution in C is often pretty fast, while the naive solution in Haskell often has obnoxious runtime properties.
Then, most C optimization performance reasoning applies to Haskell as well: keep your data small, in contiguous memory, etc. The first thing that complicates optimization in Haskell is that defacto Haskell data structures are the opposite: pointer/thunk-rich. So, you usually end up with libraries such as 'vector', doing the heavy lifting in the ST monad.
Then, laziness adds even more complications, both in that it adds a new class of bugs (of course, Haskell removes buffer overflows et al.) which tend to be relatively hard to debug (the heap explodes in less expected places).
When you write production code, you usually accumulate some monads. Unfortunately, the relatively obvious way to use multiple monads (monad transformers) is slow. So, you have to roll your own monad stacks:
https://wiki.haskell.org/Performance/Monads
---
In the end, even something as simple as reading a list integers in the IO monad can be an exercise in frustration:
http://www.joachim-breitner.de/blog/620-Constructing_a_list_...
The same can't be claimed for C. Even a newcomer could do it.
Only if you need it to do it in less than 164616 bytes, otherwise I'd just use:
replicateM 1000 (return 42)How was Haskell not "as safe as it claimed to be"? What tooling was not very good? What tooling was missing for you?
* Cabal Hell: https://news.ycombinator.com/item?id=8011267
* Installing libraries is slow
* The Haskell platform is not batteries included
* JSON parsing seem to be more complicated than it needs to be
* Also got confused by keywords naming: https://news.ycombinator.com/item?id=5689951
* I found Yesod API to be inelegant. I found it odd that it needs so many non-standard compiler extensions, which might be a sign that the language is not expressive enough.
* One-letter variables seem to be idiomatic. This is unappealing to me because I try to make my code readable.
* For the same reason, frequent use of special characters in operators (, <>, ///, etc.) in the standard library and in third-party libraries was not appealing.
I was disappointed because I was attracted by referential integrity, strong compiler checks, pattern matching and function composition but overall I was left with the impression that the Haskell environment was not very well polished from a software engineering point of view.