Difference between revisions of "10分で学ぶHaskell"

From HaskellWiki
Jump to navigation Jump to search
Line 80: Line 80:
ビルドは <tt>ghc --make Test.hs</tt> でできます。実行ファイル <tt>Test</tt> ができるはずです。([[Windows]]では<tt>Test.exe</tt>となります)ここで<hask>if</hask>式も一緒に習得できてしまいましたね。
ビルドは <tt>ghc --make Test.hs</tt> でできます。実行ファイル <tt>Test</tt> ができるはずです。([[Windows]]では<tt>Test.exe</tt>となります)ここで<hask>if</hask>式も一緒に習得できてしまいましたね。
The first non-space character after <hask>do</hask> is special. In this case, it's the <tt>p</tt> from <hask>putStrLn</hask>. Every line that starts in the same column as that <hask>p</hask> is another statement in the <hask>do</hask> block. If you indent more, it's part of the previous statement. If you indent less, it ends the <hask>do</hask> block. This is called "layout", and Haskell uses it to avoid making you put in statement terminators and braces all the time. (The <hask>then</hask> and <hask>else</hask> phrases have to be indented for this reason: if they started in the same column, they'd be separate statements, which is wrong.)
(Note: Do '''not''' indent with tabs if you're using layout. It technically still works if your tabs are 8 spaces, but it's a bad idea. Also, don't use proportional fonts -- which apparently some people do, even when programming!)
(Note: Do '''not''' indent with tabs if you're using layout. It technically still works if your tabs are 8 spaces, but it's a bad idea. Also, don't use proportional fonts -- which apparently some people do, even when programming!)

Revision as of 02:15, 4 December 2009



最も普及しているHaskellのコンパイラは GHC です。GHCは http://www.haskell.org/ghc/download.html からダウンロードできます。GHCのバイナリは GNU/Linux FreeBSD MacOSWindowsSolarisで動作します。GHCをインストールすると、ghc ghciという2つのプログラムが入っているのが確認できます。最初のghcの方はHaskellのライブラリやアプリケーションをバイナリコードにコンパイルします。 ghciはインタプリタで、Haskellコードを書いてすぐに結果を得ることができる環境です。



 Prelude> 3 * 5
 Prelude> 4 ^ 2 - 1
 Prelude> (1 - 5)^(3 * 2 - 4)


 Prelude> "Hello"
 Prelude> "Hello" ++ ", Haskell"
 "Hello, Haskell"


 Prelude> succ 5
 Prelude> truncate 6.59
 Prelude> round 6.59
 Prelude> sqrt 2
 Prelude> not (5 < 3)
 Prelude> gcd 21 14


I/O actionsはコンソールからの入出力を行うのに使います。有名なものの例は:

 Prelude> putStrLn "Hello, Haskell"
 Hello, Haskell
 Prelude> putStr "No newline"
 No newlinePrelude> print (5 + 4)
 Prelude> print (1 < 2)

putStr関数 と putStrLn関数 は文字列をターミナルに出力します。print関数はどんな型の値でも出力します。(文字列をprintするときは、引用符が前後につきます)

ひとつの表現の中で複数の I/O アクションを使いたい場合は、doブロックを使います。アクションはセミコロンで区切られます。

 Prelude> do { putStr "2 + 2 = " ; print (2 + 2) }
 2 + 2 = 4
 Prelude> do { putStrLn "ABCDE" ; putStrLn "12345" }

読み込みは getLineStringを返します)かreadLn(どんな型の値でも返します)でできます。<-シンボルはI/Oアクションの結果に名前を束縛するために使います。

 Prelude> do { n <- readLn ; print (n^2) }



main = do putStrLn "What is 2 + 2?"
          x <- readLn
          if x == 4
              then putStrLn "You're right!"
              else putStrLn "You're wrong!"

ビルドは ghc --make Test.hs でできます。実行ファイル Test ができるはずです。(WindowsではTest.exeとなります)ここでif式も一緒に習得できてしまいましたね。


(Note: Do not indent with tabs if you're using layout. It technically still works if your tabs are 8 spaces, but it's a bad idea. Also, don't use proportional fonts -- which apparently some people do, even when programming!)


So far, not a single type declaration has been mentioned. That's because Haskell does type inference. You generally don't have to declare types unless you want to. If you do want to declare types, you use :: to do it.

 Prelude> 5 :: Int
 Prelude> 5 :: Double

Types (and type classes, discussed later) always start with upper-case letters in Haskell. Variables always start with lower-case letters. This is a rule of the language, not a naming convention.

You can also ask ghci what type it has chosen for something. This is useful because you don't generally have to declare your types.

 Prelude> :t True
 True :: Bool
 Prelude> :t 'X'
 'X' :: Char
 Prelude> :t "Hello, Haskell"
 "Hello, Haskell" :: [Char]

(In case you noticed, [Char] is another way of saying String. See the section on lists later.)

Things get more interesting for numbers.

 Prelude> :t 42
 42 :: (Num t) => t
 Prelude> :t 42.0
 42.0 :: (Fractional t) => t
 Prelude> :t gcd 15 20
 gcd 15 20 :: (Integral t) => t

These types use "type classes." They mean:

  • 42 can be used as any numeric type. (This is why I was able to declare 5 as either an Int or a Double earlier.)
  • 42.0 can be any fractional type, but not an integral type.
  • gcd 15 20 (which is a function call, incidentally) can be any integral type, but not a fractional type.

There are five numeric types in the Haskell "prelude" (the part of the library you get without having to import anything):

  • Int is an integer with at least 30 bits of precision.
  • Integer is an integer with unlimited precision.
  • Float is a single precision floating point number.
  • Double is a double precision floating point number.
  • Rational is a fraction type, with no rounding error.

All five are instances of the Num type class. The first two are instances of Integral, and the last three are instances of Fractional.

Putting it all together,

 Prelude> gcd 42 35 :: Int
 Prelude> gcd 42 35 :: Double
     No instance for (Integral Double)

The final type worth mentioning here is (), pronounced "unit." It only has one value, also written as () and pronounced "unit."

 Prelude> ()
 Prelude> :t ()
 () :: ()

You can think of this as similar to the void keyword in C family languages. You can return () from an I/O action if you don't want to return anything.


Basic data types can be easily combined in two ways: lists, which go in [square brackets], and tuples, which go in (parentheses).

Lists are used to hold multiple values of the same type.

 Prelude> [1, 2, 3]
 Prelude> [1 .. 5]
 Prelude> [1, 3 .. 10]
 Prelude> [True, False, True]

Strings are just lists of characters.

 Prelude> ['H', 'e', 'l', 'l', 'o']

The : operator appends an item to the beginning of a list. (It is Haskell's version of the cons function in the Lisp family of languages.)

 Prelude> 'C' : ['H', 'e', 'l', 'l', 'o']

Tuples hold a fixed number of values, which can have different types.

 Prelude> (1, True)
 Prelude> zip [1 .. 5] ['a' .. 'e']

The last example used zip, a library function that turns two lists into a list of tuples.

The types are probably what you'd expect.

 Prelude> :t ['a' .. 'c']
 ['a' .. 'c'] :: [Char]
 Prelude> :t [('x', True), ('y', False)]
 [('x', True), ('y', False)] :: [(Char, Bool)]

Lists are used a lot in Haskell. There are several functions that do nice things with them.

 Prelude> [1 .. 5]
 Prelude> map (+ 2) [1 .. 5]
 Prelude> filter (> 2) [1 .. 5]

There are two nice functions on ordered pairs (tuples of two elements):

 Prelude> fst (1, 2)
 Prelude> snd (1, 2)
 Prelude> map fst [(1, 2), (3, 4), (5, 6)]

Also see how to work on lists

Function 定義

We wrote a definition of an IO action earlier, called main:

main = do putStrLn "What is 2 + 2?"
          x <- readLn
          if x == 4
              then putStrLn "You're right!"
              else putStrLn "You're wrong!"

Now, let's supplement it by actually writing a function definition and call it factorial. I'm also adding a module header, which is good form.

module Main where

factorial n = if n == 0 then 1 else n * factorial (n - 1)

main = do putStrLn "What is 5! ?"
          x <- readLn
          if x == factorial 5
              then putStrLn "You're right!"
              else putStrLn "You're wrong!"

Build again with ghc --make Test.hs. And,

 $ ./Test
 What is 5! ?
 You're right!

There's a function. Just like the built-in functions, it can be called as factorial 5 without needing parentheses.

Now ask ghci for the type.

 $ ghci Test.hs
 << GHCi banner >>
 Ok, modules loaded: Main.
 Prelude Main> :t factorial
 factorial :: (Num a) => a -> a

Function types are written with the argument type, then ->, then the result type. (This also has the type class Num.)

Factorial can be simplified by writing it with case analysis.

factorial 0 = 1
factorial n = n * factorial (n - 1)


A couple extra pieces of syntax are helpful.

secsToWeeks secs = let perMinute = 60
                       perHour   = 60 * perMinute
                       perDay    = 24 * perHour
                       perWeek   =  7 * perDay
                   in  secs / perWeek

The let expression defines temporary names. (This is using layout again. You could use {braces}, and separate the names with semicolons, if you prefer.)

classify age = case age of 0 -> "newborn"
                           1 -> "infant"
                           2 -> "toddler"
                           _ -> "senior citizen"

The case expression does a multi-way branch. The special label _ means "anything else".


Everything used so far in this tutorial is part of the Prelude, which is the set of Haskell functions that are always there in any program.

The best road from here to becoming a very productive Haskell programmer (aside from practice!) is becoming familiar with other libraries that do the things you need. Documentation on the standard libraries is at http://haskell.org/ghc/docs/latest/html/libraries/. There are modules there with:

module Main where

import qualified Data.Map as M

errorsPerLine = M.fromList
    [ ("Chris", 472), ("Don", 100), ("Simon", -5) ]

main = do putStrLn "Who are you?"
          name <- getLine
          case M.lookup name errorsPerLine of
              Nothing -> putStrLn "I don't know you"
              Just n  -> do putStr "Errors per line: "
                            print n

The import says to use code from Data.Map and that it will be prefixed by M. (That's necessary because some of the functions have the same names as functions from the prelude. Most libraries don't need the as part.)

If you want something that's not in the standard library, try looking at http://hackage.haskell.org/packages/hackage.html or this wiki's applications and libraries page. This is a collection of many different libraries written by a lot of people for Haskell. Once you've got a library, extract it and switch into that directory and do this:

 runhaskell Setup configure
 runhaskell Setup build
 runhaskell Setup install

On a UNIX system, you may need to be root for that last part.


Languages: en zh/cn ja