Haskell Tutorial for C Programmers - Introduction
Written by Eric Etheridge
Table of ContentsEach major section has its own page.
多くの人がCやC++、Java、Python、Pascalのような 命令型言語(imprerative langauges)に慣れています。 計算機科学を学ぶ学生にとって、Haskellは奇妙でobtuseなものです。 このチュートリアルはC/C++、Python、JavaもしくはPascalに慣れ親しんだ人が 読者であると仮定しています。私がこれを書いたのはC/C++、Javaから Haskellのような言語に移る難しさを学生が乗り越えるのを手助けするような チュートリアルが書かれていないと感じたからです。
I write this assuming that you have checked out the Gentle Introduction to Haskell, but still don't understand what's going on.
私はこのチュートリアルを、Gentle Introducntion to Haskellをすでにチェックしているけれども まだそれが理解できない人が読者であると仮定しています。
Haskell is not 'a little different,' and will not 'take a little time.' It is very different and you cannot simply pick it up, although I hope that this tutorial will help.
Haskellは“ちょっとちがう”ものではなく、“時間をあまり取らない”ものでもありません。 とても違ったもので、単純に違いを拾い集めることはできません。でも、わたしは このチュートリアルが助けになることを望んでいます。
If you play around with Haskell, do not merely write toy programs. Simple problems will not take advantage of Haskell's power. Its power shines mostly clearly when you attack difficult tasks. Haskell's tools dramatically simplify your code.
Haskellで遊ぼうというのなら、トイプログラムを書いてはいけません。 単純な問題はHaskellの力を引き出しません。Haskellの力は難しいタスクに挑戦するときに 明らかになります。Haskellのツールは劇的にあなたのコードを単純にします。
I am going to put many pauses in this tutorial because learning Haskell hurt a lot, at least for me. I needed breaks, and my brain hurt while I was trying to understand.
私はこのチュートリアルで区切りを多くいれようと思っています。なぜなら、Haskellを 学ぶことは少なくとも私にとっては大きな苦痛であるからです。私には休憩が必要で、 私の脳は理解しようとしている間痛めつけられているのです。
Haskell has both more flexibility and more control than most languages. Nothing that I know of beats C's control, but Haskell has everything C does unless you need to control specific bytes in memory. So I call Haskell powerful, rather than just 'good.'
Haskellは大部分の言語よりもよりフレキシブルで、多くの制御を持っています。 Cの制御を叩きのめすものを私は知りませんが、HaskellはCが持っているすべてのものを 特定の大きさのメモリー領域で制御する必要がない限りは持っています。 ですから、わたしはHaskellが単に‘良い’のではなくパワフルであると呼ぶのです。
I wrote this tutorial because Haskell was very hard for me to learn, but now I love it. "Haskell is hard!" "You can't write code the way I know how!" "My brain hurts!" "There aren't any good references!" That's what I said when I was in college. There were good references, but they didn't cover the real problem: coders know C.
私がこのチュートリアルを書いたのは、Haskellが私にとってとても学ぶのが辛いもので あったにもかかわらず私がいまはそれを愛しているからです。 "Haskellは難しい!" "あなたは私が知っているやり方でコードを書くことができない!" "私の脳は痛めつけられている!" "良いリファレンスがない!" これらは私が大学にいたときに私が言っていたことです。 良いリファレンスはありますが、それらは本当の問題 コーディングする人はCを知っている ということをカバーしていません。
This abstract was pieced together by Mark Evans, here, from my own work. I have had no contact with Mark Evans, but since he did't contact me when he editted together this abstract from my work and posted it on lambda-the-ultimate, I doubt he'll care that I've taken that edit and used it as my abstract here. If he wishes, he may contact me regarding the legal status of this work. For now, I assume I still hold the copyright on all of it, including the abstract (but see the license section below).
If you don't have bzip2, you can get the latest version at www.bzip.org.
TODO: I should also offer a postscript version or something.
This work is licensed under a Creative Commons Attribution-ShareAlike 2.5 License.
I write this assuming that you have checked out the following tutorial, the Gentle Introduction to Haskell, but found that you still don't understand what's going on:
That tutorial is a good reference for basic syntax. In this tutorial we will skip most of that until later when it is clear why function definition is so important in Haskell. Here is another tutorial, the Tour of the Haskell Syntax, with much more specific information:
Refer to it, too. It has the appropriate syntax for most of the things I discuss, without requiring you to know Haskell's depths. Please read both of these after, during, or even before reading this tutorial.
One of the best references is the source code for the Prelude, the file "Prelude.hs", where all of the general-purpose functions are defined. You can find it in Hugs's install directories someplace. If any function shows up that you don't understand, you can look up its definition and figure out what it's really doing. This is a very good practice for those unfamiliar with general Haskell use.
Another resource is the GHC Hierarchical Libraries documentation. These have the types and module names for everything in GHC, and most of the time have meaningful commentary:
There is also a newer version of that documentation for the 'newAPI' in the most recent CVS version of GHC. Since writing the first draft of this tutorial, HOpenGL moved to the main version, so this link is not necessary for finding HOpenGL documentation. However, I still include it because some of GHC's newest extensions, such as HOpenAL (note the 'AL') are here, as of 7/3/05:
Finally, if you don't already have them, get and install both Hugs and GHC. Hugs will let you play around and learn, and GHC will be necessary for your more advanced tasks later on. GHCi (GHC interactive) comes with GHC, and is a close substitute for Hugs. It is a little harder (I think) to play around with. It may be equal in utility now, so check it out, too. If you use Debian, Hugs and GHC are packages. For everyone else, the homepages are here:
Otherwise, read this tutorial and those mentioned above first, then check out our HOpenGL tutorial if you like. If we've written well enough you should be able to not only use Haskell but HOpenGL as well. On with the tutorial.
As the tutorial progresses, one thing should become clear about Haskell: its real power comes into play when you attack difficult problems. Because of this, I use some difficult problems in this tutorial. Don't worry if you don't understand the solutions after reading the tutorial once. Haskell is not a toy language, and even a moderately sized set of functions will include several of Haskell's complicated tools all working together. This has left educators with a dilemma: do I use ridiculously simple code in order to cover a single topic at once, or do I use something actually useful and try to explain all the pieces and how they all fit together? Many tutorials and lessons have chosen the former, but I prefer the latter. That means that each example requires a lot of explaining. Often concepts must be explained once in extremely simplistic terms, and then explained again later after other related topics have also been briefly discussed. As you read this tutorial, remember this: Haskell's real power is the fact that all of its pieces fit so well together, not just that they are good pieces.