<?xml version="1.0" encoding="UTF-8" ?><rdf:RDF 
  xmlns="http://purl.org/rss/1.0/"
  xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  xmlns:atom="http://www.w3.org/2005/Atom"
  xmlns:dc="http://purl.org/dc/elements/1.1/"
  xml:lang="ja">
  <channel rdf:about="http://w.atwiki.jp/mamonbo/">
    <title>mamonbo @ ウィキ</title>
    <link>http://w.atwiki.jp/mamonbo/</link>
    <atom:link href="https://w.atwiki.jp/mamonbo/rss10.xml" rel="self" type="application/rss+xml" />
    <atom:link rel="hub" href="https://pubsubhubbub.appspot.com" />
    <description>mamonbo @ ウィキ</description>

    <dc:language>ja</dc:language>
    <dc:date>2014-09-24T20:41:37+09:00</dc:date>
    <utime>1411558897</utime>

    <items>
      <rdf:Seq>
                <rdf:li rdf:resource="https://w.atwiki.jp/mamonbo/pages/25.html" />
                <rdf:li rdf:resource="https://w.atwiki.jp/mamonbo/pages/16.html" />
                <rdf:li rdf:resource="https://w.atwiki.jp/mamonbo/pages/24.html" />
                <rdf:li rdf:resource="https://w.atwiki.jp/mamonbo/pages/18.html" />
                <rdf:li rdf:resource="https://w.atwiki.jp/mamonbo/pages/23.html" />
                <rdf:li rdf:resource="https://w.atwiki.jp/mamonbo/pages/17.html" />
                <rdf:li rdf:resource="https://w.atwiki.jp/mamonbo/pages/21.html" />
                <rdf:li rdf:resource="https://w.atwiki.jp/mamonbo/pages/22.html" />
                <rdf:li rdf:resource="https://w.atwiki.jp/mamonbo/pages/20.html" />
                <rdf:li rdf:resource="https://w.atwiki.jp/mamonbo/pages/19.html" />
              </rdf:Seq>
    </items>
	
		
    
  </channel>
    <item rdf:about="https://w.atwiki.jp/mamonbo/pages/25.html">
    <title>Haskell/真偽値</title>
    <link>https://w.atwiki.jp/mamonbo/pages/25.html</link>
    <description>
      ==等しいかとその他の比較==

(前回の復習)
前回の章で、Haskellで等号を変数や関数を定義するのに使う方法を見てきた。
例えば、以下のコード
&lt;blockquote&gt;
r = 5
&lt;/blockquote&gt;
で定義のスコープに沿って置き換わって意味の通じるところ全てで&lt;tt&gt;5&lt;/tt&gt;に置き換わる
&lt;tt&gt;r&lt;/tt&gt;が現れる。また、
&lt;blockquote&gt;
f x = x + 3
&lt;/blockquote&gt;
で数字が1つ後に続き(&lt;tt&gt;f&lt;/tt&gt;の引数として取る)、その数に3を足すのに置き換わる
&lt;tt&gt;f&lt;/tt&gt;が現れる。
(復習おわり)

しかし、数学では、等号は微妙に違っていて、同様に重要な意味でも使われる。
例えば、この問題について考える:

{| 
|valign=&quot;top&quot; style=&quot;border-right: 1px dashed SlateBlue&quot;|
&lt;b&gt;例題:&lt;/b&gt;次の方程式を解きなさい:

&lt;math&gt;x+3=5&lt;/math&gt;
|}

このような問題を見たとき、&lt;math&gt;5&lt;/math&gt;が&lt;math&gt;x+3&lt;/math&gt;の代わりになるとか、
その逆にすぐに関心を持つわけでない。代わりに、方程式&lt;math&gt;x+3=5&lt;/math&gt;を
&#039;&#039;命題&#039;&#039;、と理解し、ある数&lt;math&gt;x&lt;/math&gt;が3足すと結果として5となるという意味となる。
「方程式を解く」とは命題が真となるような&lt;math&gt;x&lt;/math&gt;の値を、存在するのならば、
見つけ出すということである。この例題では、初等代数学を用いれば、等式を&lt;math&gt;x=5-3&lt;/math&gt;,&lt;math&gt;x=2&lt;/math&gt;と変形し、目的の解を求めることが出来る。
また、解が等式を成立させることは元の等式に&lt;math&gt;x=2&lt;/math&gt;を代入することで
確認できる。例題では&lt;math&gt;2+3=5&lt;/math&gt;という明らかに真である結果になる。
(注:最新版が壊れていたのでここの部分に関しては旧版から持ってきた)

等しいかどうかを見るために値を比較することはプログラミングにおいても便利である。
Haskellではそのような比較をちょうど方程式のように見える自然な方法で書くことが出来る。
等号は既にものを定義するのに使われているので、代わりに&#039;&#039;等号２つ&#039;&#039;
(&lt;tt&gt;==&lt;/tt&gt;)を使う。実際の動きを見るには、GHCiを起動し上で書いた命題を以下のように入力すれば良い:

 Prelude&gt; 2 + 3 == 5
 True

&lt;math&gt;2 + 3&lt;/math&gt;は5に等しいためGHCiは&quot;True&quot;(真)と返す。では、真でない等式
を用いたらどうなるだろうか:

 Prelude&gt; 7 + 3 == 5
 False

快適で繋がりのある結果になった。次にこのような比較の中に自分で定義した関数を使ってみる。復習の部分で取り上げた関数&lt;tt&gt;f&lt;/tt&gt;を使ってみる:

 Prelude&gt; let f x = x + 3
 Prelude&gt; f 2 == 5
 True

&lt;tt&gt;f 2&lt;/tt&gt;はちょうど&lt;tt&gt;2 + 3&lt;/tt&gt;なので、予想通りの結果である。

等しいかを確かめるのに加えて、同じほど容易に2つの数値のどちらが大きいかを見るために比較することもできる。Haskellでは&lt;tt&gt;&lt;&lt;/tt&gt;(小なり)、&lt;tt&gt;&gt;&lt;/tt&gt;(大なり)、&lt;tt&gt;&lt;=&lt;/tt&gt;(以下)、&lt;tt&gt;&gt;=&lt;/tt&gt;(以上)といった多くの比較が利用でき、&lt;tt&gt;==&lt;/tt&gt;(等しい)と
ちょうど同様に機能する。簡単な応用例として、前回の章の円の面積を求める関数&lt;tt&gt;area&lt;/tt&gt;のそばに&lt;tt&gt;&lt;&lt;/tt&gt;を使って、ある半径の円の面積がある値より小さいか
を調べることが出来る。

 Prelude&gt; let area r = pi * r^2
 Prelude&gt; area 5 &lt; 50
 False

==ブール値==

今までの説明でGHCiである算術的命題が真か偽かを知ることができることを学んだ。
そのことはそれでよいのだが、これがどのようにプログラムを書くときに便利なの
だろうか？そして、GHCiがそのような&quot;質問&quot;に対して&quot;答える&quot;とき実際は何が起こっている
のだうか？何が起こっているのかを理解するために、一旦別の関連することについて考える。
GHCiに計算式を入力した時、式は&#039;&#039;評価&#039;&#039;され、結果となる数値が画面上に表示される:

 Prelude&gt; 2 + 2
 4

ここで計算式を等しいかの比較に変えると、似たようなことが起こる:

 Prelude&gt; 2 == 2
 True

表示される&#039;&#039;True&#039;&#039;が&#039;&#039;何&#039;&#039;なのか？確実に数のようには見えない。命題&lt;tt&gt;2 == 2&lt;/tt&gt;
が正しいかを示す何かと思うことが出来る。この視点から、&#039;&#039;True&#039;&#039;を&#039;&#039;値&#039;&#039;―総計や数量等の類を表すものではないが―としてみなすのは意味が通っている。ある命題が正しいかどうか
を表している。このような値のことを&#039;&#039;&#039;真偽値&#039;&#039;&#039;や&#039;&#039;&#039;ブール値&#039;&#039;&#039;という。
当然なことだが、ブール値は2つしか存在し得ない。それは、&lt;tt&gt;True&lt;/tt&gt;と
&lt;tt&gt;False&lt;/tt&gt;である。(Haskellでは先頭は大文字)

ちなみにブール値という用語は数学者、哲学者である[http://ja.wikipedia.org/wiki/%E3%82%B8%E3%83%A7%E3%83%BC%E3%82%B8%E3%83%BB%E3%83%96%E3%83%BC%E3%83%AB ジョージ・ブール]にちなんで名付けられた。

===型の導入===

&lt;tt&gt;True&lt;/tt&gt;や&lt;tt&gt;False&lt;/tt&gt;が値であるという時、ただ単に類似をしているわけではない。Haskellではブール値は数値と同じ状態を持っていて、実際、数字とちょうど同じように
操作することができる。些細な例を挙げるとしたら、真偽値が等しいかの比較であろう:

 Prelude&gt; True == True
 True
 Prelude&gt; True == False
 False

確かに&lt;tt&gt;True&lt;/tt&gt;は&lt;tt&gt;True&lt;/tt&gt;に等しく、&lt;tt&gt;True&lt;/tt&gt;は&lt;tt&gt;False&lt;/tt&gt;に等しくない。では、&lt;tt&gt;2&lt;/tt&gt;が&lt;tt&gt;True&lt;/tt&gt;に等しいかすぐに答えられるか？

 Prelude&gt; 2 == True
 
 &lt;interactive&gt;:1:0:
     No instance for (Num Bool)
       arising from the literal `2&#039; at &lt;interactive&gt;:1:0
     Possible fix: add an instance declaration for (Num Bool)
     In the first argument of `(==)&#039;, namely `2&#039;
     In the expression: 2 == True
     In the definition of `it&#039;: it = 2 == True

正しい答えは&#039;&#039;比較できない&#039;&#039;である、なぜならこの質問は単に意味を成さないからである。
数字を数字でないものを比較することは出来ないし、ブール値とブール値でないものを比較することは出来ない。Haskellは上の式を取り入れ、出てきた醜いエラーメッセージは本質的に、
ちょうどこのことを言っている。分かりづらいごちゃごちゃをすべて無視すれば(最終的には
理解できるようになる)、上のメッセージは&lt;tt&gt;==&lt;/tt&gt;の左辺に数(&lt;tt&gt;Num&lt;/tt&gt;)があり、
それ故、数の類が右辺にあると思われた。しかし、ブール値(&lt;tt&gt;Bool&lt;/tt&gt;)は数でなく、
それ故、等しいかの比較は吹き飛んだのである。

故に、一般的な概念は値は&#039;&#039;&#039;型&#039;&#039;&#039;を持つということであり、これらの型がその値で何が出来て、何が出来ないのかを定義する。例えば今回の場合、&lt;tt&gt;True&lt;/tt&gt;は&lt;tt&gt;Bool&lt;/tt&gt;型の値であり、&lt;tt&gt;&lt;/tt&gt;&lt;tt&gt;&lt;/tt&gt;
The correct answer is you &#039;&#039;can&#039;t&#039;&#039;, because the question just does not make sense. It is impossible to compare a number with something that is not a number, or a boolean with something that is not a boolean. Haskell incorporates that notion, and the ugly error message we got is, in essence, stating exactly that. Ignoring all of the obfuscating clutter (which we will get to understand eventually), that message says that there was a number (&lt;tt&gt;Num&lt;/tt&gt;) on the left side of the &lt;tt&gt;==&lt;/tt&gt;, and so some kind of number was expected on the right side; however, a boolean value (&lt;tt&gt;Bool&lt;/tt&gt;) is not a number, and so the equality test exploded into flames.

Thus, the general concept is that values have &#039;&#039;&#039;types&#039;&#039;&#039;, and these types define what we can or cannot do with the values. In this case, for instance, &lt;tt&gt;True&lt;/tt&gt; is a value of type &lt;tt&gt;Bool&lt;/tt&gt;, as is &lt;tt&gt;False&lt;/tt&gt; (as for the &lt;tt&gt;2&lt;/tt&gt;, while there is a well-defined concept of number in Haskell the situation is slightly more complicated, so we will defer the explanation for a little while). Types are a very powerful tool because they provide a way to regulate the behaviour of values with rules which &#039;&#039;make sense&#039;&#039;, making it easier to write programs that work correctly. We will come back to the topic of types many times as they are very important to Haskell.

== Infix operators ==

What we have seen so far leads us to the conclusion that an equality test like &lt;tt&gt;2 == 2&lt;/tt&gt; is an expression just like &lt;tt&gt;2 + 2&lt;/tt&gt;, and that it also evaluates to a value in pretty much the same way. That fact is actually given a passing mention on the ugly error message we got on the previous example:

{{HaskellGHCi|1=
    In the expression: 2 == True
}}

Therefore, when we type &lt;tt&gt;2 == 2&lt;/tt&gt; in the prompt and GHCi &quot;answers&quot; &lt;tt&gt;True&lt;/tt&gt; it is just evaluating an expression. But there is a deeper truth involved in this process. A hint is provided by the very same error message: 

{{HaskellGHCi|1=
     In the first argument of `(==)&#039;, namely `2&#039;
}}

GHCi called &lt;tt&gt;2&lt;/tt&gt; the first &#039;&#039;argument&#039;&#039; of &lt;tt&gt;(==)&lt;/tt&gt;. In the previous module, we used &#039;&#039;argument&#039;&#039; to describe the values we feed a function with so that it evaluates to a result. It turns out that &lt;tt&gt;==&lt;/tt&gt; is just a function, which takes two arguments, namely the left side and the right side of the equality test. The only special thing about it is the syntax: Haskell allows two-argument functions with names composed only of non-alphanumeric characters to be used as &#039;&#039;infix operators&#039;&#039;, that is, placed between their arguments. The only caveat is that if you wish to use such a function in the &quot;standard&quot; way (writing the function name before the arguments, as a &#039;&#039;prefix operator&#039;&#039;) the function name must be enclosed in parentheses. So the following expressions are completely equivalent:

{{HaskellGHCi|1=
Prelude&gt; 4 + 9 == 13
True
Prelude&gt; (==) (4 + 9) 13
True
}}

This makes it clear how &lt;tt&gt;(==)&lt;/tt&gt; is a function with two arguments just like &lt;tt&gt;areaRect&lt;/tt&gt; from the previous module. What&#039;s more, the same considerations apply to the other &#039;&#039;relational operators&#039;&#039; we mentioned (&lt;tt&gt;&lt;&lt;/tt&gt;, &lt;tt&gt;&gt;&lt;/tt&gt;, &lt;tt&gt;&lt;=&lt;/tt&gt;, &lt;tt&gt;&gt;=&lt;/tt&gt;) and to the arithmetical operators (&lt;tt&gt;+&lt;/tt&gt;, &lt;tt&gt;*&lt;/tt&gt;, etc.) – all of them are just functions. This generality is an illustration of one of the strengths of Haskell: it is a language with very few &quot;special cases&quot;, which helps to keep things simple. In general, we can say that all tangible things in Haskell are either values or functions.&lt;ref&gt;In case you found this statement bold, know that we will go even further in due course.&lt;/ref&gt;

== Boolean operations ==

To see both truth values and infix operators in action, let&#039;s consider the boolean operations which manipulate truth values as in logic propositions. Haskell provides us three basic functions for that purpose:

* &lt;tt&gt;(&amp;&amp;)&lt;/tt&gt; performs the &#039;&#039;and&#039;&#039; operation. Given two boolean values, it evaluates to &lt;tt&gt;True&lt;/tt&gt; if both the first and the second are &lt;tt&gt;True&lt;/tt&gt;, and to &lt;tt&gt;False&lt;/tt&gt; otherwise.

{{HaskellGHCi|1=
Prelude&gt; (3 &lt; 8) &amp;&amp; (False == False)
True
Prelude&gt; (&amp;&amp;) (6 &lt;= 5) (1 == 1) 
False
}}

* &lt;tt&gt;(||)&lt;/tt&gt; performs the &#039;&#039;or&#039;&#039; operation. Given two boolean values, it evaluates to &lt;tt&gt;True&lt;/tt&gt; if either the first or the second are &lt;tt&gt;True&lt;/tt&gt; (or if both are true), and to &lt;tt&gt;False&lt;/tt&gt; otherwise.

{{HaskellGHCi|1=
Prelude&gt; (2 + 2 == 5) {{!!}} (2 &gt; 0)
True
Prelude&gt; ({{!!}}) (18 == 17) (9 &gt;= 11)
False
}}

* &lt;tt&gt;not&lt;/tt&gt; performs the negation of a boolean value; that is, it converts &lt;tt&gt;True&lt;/tt&gt; to &lt;tt&gt;False&lt;/tt&gt; and vice-versa.

{{HaskellGHCi|1=
Prelude&gt; not (5 * 2 == 10)
False
}}

Another relational operator is &#039;&#039;not equal to&#039;&#039;. It is already provided by Haskell as the &lt;tt&gt;(/=)&lt;/tt&gt; function, but if we were to implement it ourselves, a natural way would be:

&lt;source lang = &quot;haskell&quot;&gt;
x /= y = not (x == y)
&lt;/source&gt;

Note that it is perfectly legal syntax to write operators infix, even when defining them. Completely new operators can also be created out of ASCII symbols (which means mostly the common symbols used on a keyboard).

== Guards ==

Now we have explored what is really happening with boolean operators, but we&#039;ve done little more than testing one-line expressions here. We still don&#039;t know how this can be used to make real programs. We will tackle this issue by introducing &#039;&#039;guards&#039;&#039;, a feature that relies on boolean values and allows us to write more interesting functions.

Let us implement the absolute value function. The absolute value of a number is the number with its sign discarded&lt;ref&gt;Technically, that just covers how to get the absolute value of a &#039;&#039;real&#039;&#039; number, but let&#039;s ignore this detail for now.&lt;/ref&gt;; so if the number is negative (that is, smaller than zero) the sign is inverted; otherwise it remains unchanged. We could write the definition as:

&lt;math&gt;|x| = \begin{cases} x, &amp; \mbox{if }  x \ge 0  \\ -x,  &amp; \mbox{if } x &lt; 0. \end{cases} &lt;/math&gt; 

Here, the actual expression to be used for calculating &lt;math&gt;|x|&lt;/math&gt; depends on a set of propositions made about &lt;math&gt;x&lt;/math&gt;. If &lt;math&gt;x \ge 0&lt;/math&gt; is true, then we use the first expression, but if &lt;math&gt;x &lt; 0&lt;/math&gt; is the case, then we use the second expression instead. We need a way to express this decision process in Haskell. Using guards, the implementation could look like this:&lt;ref&gt;&lt;tt&gt;abs&lt;/tt&gt; is also provided by Haskell, so in a real-world situation you don&#039;t need to worry about providing an implementation yourself.&lt;/tt&gt;&lt;/ref&gt;

{{HaskellExample|1=The abs function.|2=&lt;source lang=&quot;haskell&quot;&gt;
abs x
    | x &lt; 0     = 0 - x
    | otherwise = x
&lt;/source&gt;}}

Remarkably, the above code is about as readable as the corresponding mathematical definition. Let us dissect the components of the definition:

* We start just like a normal function definition, providing a name for the function, &lt;tt&gt;abs&lt;/tt&gt;, and saying it will take a single parameter, which we will name &lt;tt&gt;x&lt;/tt&gt;.

* Instead of just following with the &lt;tt&gt;=&lt;/tt&gt; and the right-hand side of the definition, we entered a line break, and, following it, the two alternatives, placed in separate lines.&lt;ref&gt;We &#039;&#039;could&#039;&#039; have joined the lines and written everything in a single line, but in this case it would be a lot less readable.&lt;/ref&gt; These alternatives are the &#039;&#039;guards&#039;&#039; proper. Note that the whitespace is not just for aesthetic reasons; it is necessary for the code to be parsed correctly.

* Each of the guards begins with a pipe character, &lt;tt&gt;|&lt;/tt&gt;. After the pipe, we put an expression which evaluates to a boolean (also called a boolean condition or a &#039;&#039;predicate&#039;&#039;), which is followed by the rest of the definition – the equals sign and the right-hand side which should be used if the predicate evaluates to &lt;tt&gt;True&lt;/tt&gt;.

* The &lt;tt&gt;otherwise&lt;/tt&gt; case is used when none of the preceding predicates evaluate to &lt;tt&gt;True&lt;/tt&gt;. In this case, if &lt;tt&gt;x&lt;/tt&gt; is not smaller than zero, it must be greater than or equal to zero, so the final predicate could have just as easily been &lt;tt&gt;x &gt;= 0&lt;/tt&gt;; but &lt;tt&gt;otherwise&lt;/tt&gt; works just as well.

{{body note |There is no syntactical magic behind &lt;tt&gt;otherwise&lt;/tt&gt;. It is defined alongside the default variables and functions of Haskell as simply

&lt;source lang = &quot;haskell&quot;&gt;otherwise = True&lt;/source&gt;

This definition makes &#039;&#039;otherwise&#039;&#039; a catch-all guard. As evaluation of the guard predicates is sequential, the &lt;tt&gt;otherwise&lt;/tt&gt; predicate will only be reached if none of the other ones evaluates to &lt;tt&gt;True&lt;/tt&gt; (so make sure you always place &#039;&#039;otherwise&#039;&#039; as the last guard!). In general, it is a good idea to always provide an &lt;tt&gt;otherwise&lt;/tt&gt; guard, because a rather ugly runtime error will be produced if none of the predicates is true for some input. 
}}

{{body note |You might be wondering why we wrote &lt;tt&gt;0 - x&lt;/tt&gt; and not simply &lt;tt&gt;-x&lt;/tt&gt; to denote the sign inversion. Truth is, we could have written the first guard as

&lt;source lang = &quot;haskell&quot;&gt;    | x &lt; 0    = -x &lt;/source&gt;

and it would have worked just as well. The only issue is that this way of expressing sign inversion is actually one of the few &quot;special cases&quot; in Haskell, in that in this case the &lt;tt&gt;-&lt;/tt&gt; is &#039;&#039;not&#039;&#039; a function that takes one argument and evaluates to &lt;tt&gt;0 - x&lt;/tt&gt;, but just a syntactical abbreviation. While very handy, this shortcut occasionally conflicts with the usage of &lt;tt&gt;(-)&lt;/tt&gt; as an actual function (the subtraction operator), which is a potential source of annoyance (for example, try writing three minus negative-four without using any parentheses for grouping). Here, we wrote &lt;tt&gt;0 - x&lt;/tt&gt; explicitly so that we could take the opportunity to point out this issue.
}}

=== &lt;code&gt;where&lt;/code&gt; and Guards ===

&lt;code&gt;where&lt;/code&gt; clauses are particularly handy when used with guards. For instance, consider a function which computes the number of (real) solutions for a [[w:Quadratic equation|quadratic equation]], &lt;math&gt;ax^2 + bx + c = 0&lt;/math&gt;: &lt;!-- I don&#039;t like this example, but still couldn&#039;t think of anything more meaningful with only bare numbers. Suggestions are welcome... --&gt;

&lt;source lang = &quot;haskell&quot;&gt;
numOfSolutions a b c
    | disc &gt; 0  = 2
    | disc == 0 = 1
    | otherwise = 0
        where
        disc = b^2 - 4*a*c
&lt;/source&gt;

The &lt;tt&gt;where&lt;/tt&gt; definition is within the scope of all of the guards, sparing us from repeating the expression for &lt;tt&gt;disc&lt;/tt&gt;.

{{Haskell/NotesSection}}


{{Haskell navigation|chapter=Haskell Basics|noexercises=1}}

{{Auto category}}    </description>
    <dc:date>2014-09-24T20:41:37+09:00</dc:date>
    <utime>1411558897</utime>
  </item>
    <item rdf:about="https://w.atwiki.jp/mamonbo/pages/16.html">
    <title>Wikibooks:Haskell勝手に日本語訳</title>
    <link>https://w.atwiki.jp/mamonbo/pages/16.html</link>
    <description>
      [[Haskell-logo.png]]

このページは[http://en.wikibooks.org/wiki/Haskell WikibooksのHaskellのページ(英語版)]の日本語訳である。
なぜ日本語版に上げないのかというと、書き言葉で訳せる気がしないということと、
私自身がHaskell初心者でこれを書きながら学ぼうと思っているからである。

元がクリエイティブ・コモンズ 表示 - 継承 3.0(CC BY-SA 3.0)なので、これもCC BY-SA 3.0
となります。

&#039;&#039;&#039;Haskell&#039;&#039;&#039;は[http://ja.wikipedia.org/wiki/%E9%96%A2%E6%95%B0%E5%9E%8B%E8%A8%80%E8%AA%9E 関数型言語]である。もしプログラミングの経験があるのなら、
[[Haskell/概要|概要]]を見て、Haskellがどのように動くのかをいくらか見、他の言語とどう違うかを見てみよう。

Haskellは幾らかの点に置いて他と異なっている:
*Haskellは&#039;&#039;純粋&#039;&#039;関数型言語である。もし同じ関数を同じ引数で2回違うところで呼び出すと、2回とも全く同じ値を返す。([http://ja.wikipedia.org/wiki/%E5%8F%82%E7%85%A7%E9%80%8F%E9%81%8E%E6%80%A7 参照透過性]という)
*Haskellはtypeclassやgeneralized algebraic data typesと言った(それぞれ型クラス、一般代数データ型といったところか)洗練された特徴を持つモダンな型付けシステムを提供する。(このような用語はすぐに滑らかに口から出るようになる)
*Haskellは&#039;&#039;遅延型&#039;&#039;つまり最終的な結果を得るために必要なときだけ計算をする言語でもある。
(modernをカタカナで訳すのはコンピュータ界隈ではよくある話。対義語の古いって言ってもだいたい20世紀だったりするからかと)

純粋な関数だけを取り扱ってコードをずっと簡単に理解し、それぞれの関数の正しさを解析し、証明しさえするのが簡単になるからHaskellプログラマはHaskellを享受するのである。その上、先進的な型システムが馬鹿げた間違い、真面目な間違い両方を見つけるのを助けてくれる。

この本では、Haskellのとても初歩的なところから先の特徴までと、コンピュータプログラミング一般両方について紹介することを目標にしている。経験のあるプログラマにはこの工程に大いに辛抱強くいてもらいたい。絶対に、最も慣れ親しんだ言語はHaskellを大いに異なっていて、
君たちの言語からの習慣がHaskellの仕組みを理解するのの障壁になるかもしれない。仕組みは単純なのだが異なるのだ。離れていて数学的な関数型プログラマの頭を通して世界を見るための学習を重要なことを理解していかなる言語の境界を遥かに超えたところへもたらしてくれる、素晴らしい新しい世界での冒険として捉えてほしい。

==概要==
この本は初級編、中級編、より最近の問題を取り扱う実践編に分かれている。実践編ではほとんど初心者編で習ったことのみ使う。

(ここにページ作成に貢献するように説得する文章が書いてある)
{| 
|valign=&quot;top&quot; style=&quot;border-right: 1px dashed SlateBlue&quot;|
本家ではプロンプトの場合は実線の枠、ソースファイルの場合は点線枠の囲いがあったが、@wikiでは点線枠が出せそうにないのでそれぞれ赤枠(行頭スペース)、青枠(blockquoteタグ)で代用している。
|}
==初級編==
[[Haskell/変数と関数]]
Haskellの基本といくつかのよく使われるライブラリで単純なプログラムが作れるようになる。

ほとんどの章には理解度を調べるために役立つ演習問題がある。それぞれの章の最後に解答のリンクが貼ってあるので、知識の正確さを確かめたり、行き詰まった時に答えを知ることができるようになっている。

{|width=&quot;100%&quot; cellspacing=&quot;0&quot; cellpadding=&quot;15&quot; valign=&quot;top&quot; style=&quot;margin:0.8em 0;border:1px solid MediumAquamarine;background:honeydew&quot;

|valign=&quot;top&quot; style=&quot;border-right: 1px dashed MediumAquamarine&quot;|

===Haskellの基本===
*[[Haskell/環境の構築|環境の構築]]
*[[Haskell/変数と関数|変数と関数]]
*[[Haskell/真偽値|真偽値]]
*[[Haskell/型の基本その1|型の基本その1]]
*[[Haskell/リストとタプル|リストとタプル]]
*[[Haskell/型の基本その2|型の基本その2]]
*Next steps
*Building vocabulary
*[[Haskell/簡単な入出力|簡単な入出力]]

|valign=&quot;top&quot; style=&quot;border-right: 1px dashed MediumAquamarine&quot;|

===Haskell初級第一===
*{{Haskell chapter/Elementary Haskell|sep=
*}}

|valign=&quot;top&quot; style=&quot;border-right: 1px dashed MediumAquamarine&quot;|

===Haskell初級第二===
*{{Haskell chapter/Intermediate Haskell|sep=
*}}

|valign=&quot;top&quot;|

===モナド===
*{{Haskell chapter/Monads|sep=*|subsep=&amp;nbsp;-&amp;nbsp;|breaksep=
**|firstsep=
*}}

|}

==中級編==
This section introduces wider functional programming concepts such as different data structures and type theory. It will also cover more practical topics like concurrency.

{|width=&quot;100%&quot; cellspacing=&quot;0&quot; cellpadding=&quot;15&quot; valign=&quot;top&quot; style=&quot;margin:0.8em 0; border: 1px solid SlateBlue; background:#F7F7FF&quot;

|valign=&quot;top&quot; style=&quot;border-right: 1px dashed SlateBlue&quot;|

===Haskell中級===
*{{Haskell chapter/Advanced Haskell|sep=
*}}

|valign=&quot;top&quot; style=&quot;border-right:1px dashed SlateBlue&quot;|

===型で遊ぶ===
*{{Haskell chapter/Fun with Types|sep=
*}}

|valign=&quot;top&quot; style=&quot;border-right:1px dashed SlateBlue&quot;|

=== Wider Theory ===
*{{Haskell chapter/Wider Theory|sep=
*}}

|valign=&quot;top&quot;|

=== Haskell Performance ===
*{{Haskell chapter/Haskell Performance|sep=
*}}

|}

== Haskell in Practice ==
Day-to-day issues of working with Haskell include items such as knowing the standard libraries, building graphical interfaces, and working with databases. You should be able to jump directly to this section from the beginner&#039;s track.

{|width=&quot;100%&quot; cellspacing=&quot;0&quot; cellpadding=&quot;15&quot; style=&quot;margin-top:0.8em;margin-bottom:0.8em;border:1px solid gold;background:cornsilk&quot;

|valign=&quot;top&quot; style=&quot;border-right: 1px dashed gold&quot;|

=== Libraries Reference ===
*{{Haskell chapter/Libraries Reference|sep=*|subsep=&amp;nbsp;-&amp;nbsp;|breaksep=
**|firstsep=
*}}

|valign=&quot;top&quot; style=&quot;border-right: 1px dashed gold&quot;|

=== General Practices ===
*{{Haskell chapter/General Practices|sep=
*}}

|valign=&quot;top&quot;|

=== Specialised Tasks  ===
*{{Haskell chapter/Specialised Tasks|sep=
*}}

|}

== Appendices ==
:[[/Syntactic sugar/]]
:[[/Solutions|Answers to exercises]]
:[[/Authors/|Authors and Acknowledgements]]

== About the book ==
:[[/Notes for contributors/]]
:[[/Style conventions/]]
:[[/To do/]]
:[[:Category:Haskell/Templates|Templates for the Haskell wikibook]]
:[[/Experimental Modules/|Experimental Modules]]
:[[/List of topics/|List of topics]]

== Other Haskell tutorials ==
* [http://www.haskell.org/haskellwiki/Meta-tutorial Haskell Meta-tutorial] - the tutorial to find other tutorials
* [http://www.learnyouahaskell.com/Chapters Learn You a Haskell for Great Good] - Tutorial aimed at beginners who may have experience in imperative programming languages but haven&#039;t programmed in a functional language before. Freely available online under a CC-BY-NC-SA license; also released as a conventional book.
* [http://book.realworldhaskell.org/ Real World Haskell] -  an O&#039;Reilly book, available online at no charge (CC-BY-NC license). Built around case studies of practical applications.
* [[Write Yourself a Scheme in 48 Hours]] - (imported here) An alternate approach to teaching Haskell (and perhaps Scheme), aimed at a more advanced audience (though not necessarily one that knows how to program!)
* [[/YAHT|Yet Another Haskell Tutorial]] - (imported here) is aimed at beginners and takes a practical approach to things.

== Additional resources ==
* [http://www.haskell.org/haskellwiki/Learning_Haskell haskell.org - Resources for learning Haskell].
* [http://dev.stephendiehl.com/hask/ What I Wish I Knew When Learning Haskell] - A wide-ranging collection of concise summaries of many intermediate and advanced Haskell topics. Released in the public domain. 
* [http://www.cs.kent.ac.uk/people/staff/sjt/craft2e/ The resource page] for the &#039;&#039;Haskell: the craft of functional programming&#039;&#039; book.

{{status|75%}}    </description>
    <dc:date>2014-09-18T13:54:14+09:00</dc:date>
    <utime>1411016054</utime>
  </item>
    <item rdf:about="https://w.atwiki.jp/mamonbo/pages/24.html">
    <title>初回購入特典</title>
    <link>https://w.atwiki.jp/mamonbo/pages/24.html</link>
    <description>
      初回購入特典は
2007で00の部分がメガネになっているやつ
プチプチした後のプチプチ
偽造1円玉
HDDVDプレイヤー

嘘です。    </description>
    <dc:date>2014-09-05T11:45:03+09:00</dc:date>
    <utime>1409885103</utime>
  </item>
    <item rdf:about="https://w.atwiki.jp/mamonbo/pages/18.html">
    <title>Haskell/変数と関数</title>
    <link>https://w.atwiki.jp/mamonbo/pages/18.html</link>
    <description>
      &#039;&#039;この章に出てくるすべての例はHaskellのソースファイルに入力しGHCないしHugsに読み込ませることで評価することができる。入力の頭の&quot;Prelude&gt;&quot;のプロンプトは含めないこと。プロンプトが出ているときは、GHCiといった環境にコードを入力してよい。そうでない時は、コードをファイルに入れて実行すること。&#039;&#039;
(斜体にするのは英字だけみたい。そしてEnter入ると切れるみたい。)

==変数==

GHCiを電卓として使う方法はもう見た。もちろん、この方法は短い計算に取ってのみ実用的である。より長い計算やHaskellのプログラムを書くのには中間結果を追跡したい。

中間結果は&#039;&#039;変数&#039;&#039;に格納でき、名前で呼び出すことができる。1つの変数には1つの&#039;&#039;値&#039;&#039;が入っていて、変数が使われた時に変数名が値に代わる。例えば、以下に示すような計算である。

 ghci&gt; 3.1416 * 5^2
 78.53999999999999

これは円の面積公式&lt;math&gt;A = \pi r^2&lt;/math&gt;による半径&lt;code&gt;5&lt;/code&gt;の円のおよその面積である。&lt;math&gt;\pi (\approx 3.1416)&lt;/math&gt;の数桁打つのも、この数桁をきっかり覚えるのさえも面倒臭い。実際、プログラミングの重要な気の持ちようの1つは我々の頭が思う存分より面白い考えを扱うようにするために、非思考的な繰り返しや丸暗記を機械に任せることだ。今回の場合なら、Haskellはすでに10桁以上の&lt;math&gt;\pi&lt;/math&gt;を格納している&lt;code&gt;pi&lt;/code&gt;という変数が入っている。

 ghci&gt; pi
 3.141592653589793
 ghci&gt; pi * 5^2
 78.53981633974483

注意:変数&lt;code&gt;pi&lt;/code&gt;とそれが持っている値&lt;code&gt;3.141592653589793&lt;/code&gt;は計算上では相互交換可能に使われうる。
(後ろに9が続くのは10進数で有限小数でも2進数では無限小数なために精度落ちするため。
ちなみに私は3.14159265358979323846264338327950288419716939937510まで覚えている)

==Haskellのソースファイル==

一瞬だけ使うわけでないコードを書くときはいつでも、コードを拡張子が&lt;tt&gt;.hs&lt;/tt&gt;のHaskellのソースファイルに保存する。基本的に、.hsファイルはプレーンテキストである。
テキストエディタにコーディングに合った予測機能が必要なら[http://ja.wikipedia.org/wiki/%E3%83%86%E3%82%AD%E3%82%B9%E3%83%88%E3%82%A8%E3%83%87%E3%82%A3%E3%82%BF Wikipediaのテキストエディタの記事]から始めてみるのが良いだろう。&lt;font style=&quot;font-size: 2px;&quot;&gt;Vim&lt;/font&gt;や&#039;&#039;&#039;Emacs&#039;&#039;&#039;がHaskellプログラマの間でよく採用されている。(小生Emacsユーザーなのです、スミマセン)

しっかりとしたソースコードエディタには読解を楽にするために適切にでコードに色を付ける&#039;&#039;構文ハイライト&#039;&#039;が付いている。

きちんと管理するためにこの本での演習で作ることになるHaskellファイルを保存するためのディレクトリ(Windowsで言うところのフォルダ)を作っておくこと。このディレクトリを&lt;tt&gt;HaskellWikibook&lt;/tt&gt;なりと呼ぶことにする。そしてそのディレクトリ内に&lt;tt&gt;Varfun.hs&lt;/tt&gt;という名前のファイルを作り以下のコードを書く:
&lt;blockquote&gt;
r = 5.0
&lt;/blockquote&gt;
このコードは変数&lt;code&gt;r&lt;/code&gt;を&lt;code&gt;5.0&lt;/code&gt;と定義している。自分で変数を設定すると後の計算をするのが容易になる。

注意:行頭にスペースが入っていないことを確かめること、というのもHaskellはスペースに厳格なのである。

次に、ターミナルを&lt;tt&gt;HaskellWikibooks&lt;/tt&gt;ディレクトリに持って行き、GHCiを起動し
&lt;code&gt;:load&lt;/code&gt;コマンドを使って&lt;tt&gt;Varfun.hs&lt;/tt&gt;ファイルを読みこむ:

 Prelude&gt; :load Varfun.hs
 [1 of 1] Compiling Main             ( Varfun.hs, interpreted )
 Ok, modules loaded: Main.

&lt;code&gt;:load&lt;/code&gt;は&lt;code&gt;:l&lt;/code&gt;と略せる。(今回なら&lt;code&gt;:l Varfun.hs&lt;/code&gt;のように略せる)

もしGHCiが&lt;code&gt;Could not find module &#039;Varfun.hs&#039;&lt;/code&gt;(&#039;Varfun.hs&#039;モジュールを見つけられませんでした)といったエラーを出した場合、違うディレクトリにいる可能性がある。
&lt;code&gt;:cd&lt;/code&gt;コマンドでGHCiの中でディレクトリを変えることが出来る。(例えば、
&lt;code&gt;:cd HaskellWikibook&lt;/code&gt;)

ファイルがロードされたのなら、新しく定義した変数&lt;code&gt;r&lt;/code&gt;を計算に使うことが出来る。

 *Main&gt; r
 5.0
 *Main&gt; pi * r^2
 78.53981633974483

なので、半径5の円の面積を計算するために、単に&lt;code&gt;r = 5.0&lt;/code&gt;と定義して、よく知られている円の面積公式&lt;math&gt;\pi r^2&lt;/math&gt;を入力する。これにより毎回数を書く必要が無くなり、とても便利である。

この機能はとても便利だったので、更に定義を加えよう:ソースファイルの内容以下のように変更する。

&lt;blockquote&gt;
r = 5.0&lt;br&gt;
area = pi * r ^ 2
&lt;/blockquote&gt;
ファイルを保存し、新たな内容を読みこませるために&lt;code&gt;:reload&lt;/code&gt;(省略形は&lt;code&gt;:r&lt;/code&gt;)コマンドをGHCiに入力する。(このことはこのページの最後まで続く)

 *Main&gt; :reload
 Compiling Main             ( Varfun.hs, interpreted )
 Ok, modules loaded: Main.
 *Main&gt;


今、&lt;code&gt;r&lt;/code&gt;と&lt;code&gt;area&lt;/code&gt;の2つの変数が利用可能である。

 *Main&gt; area
 78.53981633974483
 *Main&gt; area / r
 15.707963267948966

{| 
|valign=&quot;top&quot; style=&quot;border-right: 1px dashed SlateBlue&quot;|
注意

GHCiプロンプトからソースファイルを使わずに直接変数を定義することも可能である。詳しいことを省略すると、これをするための構文は&lt;code&gt;let&lt;/code&gt;という&#039;&#039;キーワード&#039;&#039;(特別な意味を持つ語、変数名や関数名として使うことが出来ない)を使う。
(小生は&#039;&#039;予約語&#039;&#039;と習ったがキーワードというのが最近の流行りなのだろうか)
例えば以下の通り:
 Prelude&gt; let area = pi * 5 ^ 2
便宜上このように&#039;&#039;let&#039;&#039;を時々使うが、このやり方は一旦少しでも複雑な仕事に移ると不便である。そのため、最初からソースファイルを使うことを強調している。
(letは数学な世界で仮定の意味だと思われ、BASICにもだいたい省略可能でLETはある)
|}

{| 
|valign=&quot;top&quot; style=&quot;border-right: 1px dashed SlateBlue&quot;|
注意

GHCはコンパイラとしても利用可能である。(つまり、自分の書いたHaskellファイルをインタプリタに頼らずに実行できるスタンドアロンプログラムに変換するためにGHCを使うことが出来る。)やり方については[[Haskell/Standalone programs|スタンドアロンプログラム]]の章で後々説明する。(Glasgow Haskell &#039;&#039;Compiler&#039;&#039;ですから出来なきゃ詐欺)
|}

==コメント==
続きに行く前に、プログラム内に文章をコードとして扱われずに入れることが可能であることを理解するのが良い。これは&#039;&#039;コメント&#039;&#039;を使うことによって出来る。Haskellではコメントは&lt;code&gt;-- &lt;/code&gt;で始め、行の最後まで続く:

&lt;blockquote&gt;
x = 5     -- 変数xは5&lt;br&gt;
y = 6     -- 変数yは6&lt;br&gt;
-- z = 7
&lt;/blockquote&gt;

この場合、&lt;code&gt;x&lt;/code&gt;と&lt;code&gt;y&lt;/code&gt;は定義されているが、&lt;code&gt;z&lt;/code&gt;は定義されていない。さらに、コメントは代わりの構文&lt;code&gt;{-(コメント)-}&lt;/code&gt;を使うことでどこにでも入れることも出来る。(日本語も可能)

&lt;blockquote&gt;
x = {-可能だからというだけの理由でやっている-} 5&lt;br&gt;
&lt;/blockquote&gt;

一般にコメントは読者が混乱するかもしれないプログラムの部分を説明するのにその場所に使われる。
しかしながら使いすぎには注意すること。コメントが多すぎるとプログラムが読みにくくなる。
また、対応するコードを変更した時はコメントが古いのになったり、不正確であったり、間違いを引き起こしがちにならないようにいつもコメントを注意深く更新すること。

==命令型言語での変数==
もしCのような命令型プログラミング言語に既に慣れているのなら、Haskellの変数があなたの知っている変数というものととても異なっていることに気付くことになる。では今からその理由と違いについて説明してゆく。

もしプログラミング経験がないのなら、この部分を読み飛ばして[[#関数|関数]](うまく動作しせん)から読むのが良いかもしれない。


命令型言語の場合と異なり、Haskellでの変数は&#039;&#039;不変である&#039;&#039;。(←重要 斜体が出ないので)
つまり、一度定義すると、中の値は決して変化しない、イミュータブルなのである。例えば、以下のコードは動かない:

&lt;blockquote&gt;
r = 5&lt;br&gt;
r = 2
&lt;/blockquote&gt;

関数型プログラミング言語での変数は計算機のメモリの位置というよりも数学的な変数の方がより関連している。数学の授業では一つの問題の中で絶対に決して変数の中の値が変わることは無かっただろう。それ同様に、Haskellではコンパイラは上のコードに対し&quot;multiple declarations of &lt;code&gt;r&lt;/code&gt;&quot;(&lt;code&gt;r&lt;/code&gt;の多重宣言)とエラーを返すのだ。
計算機に何をするかを明示的に伝えることがある&#039;&#039;命令型プログラミング&#039;&#039;に慣れている人々にとってはこれを「最初&lt;code&gt;r = 5&lt;/code&gt;と設定して、次に&lt;code&gt;r = 2&lt;/code&gt;と変える」
と読むのに慣れているかもしれない。しかしながら、関数型プログラミングでプログラムは計算機のメモリで何をするのかを明らかにする責任がある。

これは命令型言語との大きな違いの別の例である:

&lt;blockquote&gt;
r = r + 1
&lt;/blockquote&gt;

これは(命令型言語で考えがちな)&quot;変数&lt;code&gt;r&lt;/code&gt;のインクリメント&quot;ではなく、
実際はそれ自身の観点では、&lt;code&gt;r&lt;/code&gt;の再帰的定義である。(詳しくは[[Haskell/再帰|再帰]]を後々説明する。命令型言語で起こるようなことと大きな違いがあることだけを覚えておけば良い)もし&lt;code&gt;r&lt;/code&gt;が前に何かしらの値で定義されていたのなら、
&lt;code&gt; r = r + 1&lt;/code&gt;はHaskellではエラーメッセージを出す。
これは数学的文脈で明白に間違いである&lt;math&gt;5 = 5 + 1&lt;/math&gt;と言うのに同類である。
(逆に技術の教科書には、命令型プログラミングの&lt;code&gt;a = a + 1&lt;/code&gt;に慣れろと書いてあった)

変数の値がプログラム中変わらないので、変数はいかなる順序でも定義できる。例えば、以下のコードの断片は全く同じことをする:

{|border=&quot;0&quot;
|-
||
&lt;blockquote&gt;
y = x * 2&lt;br&gt;
x = 3
&lt;/blockquote&gt;
||
&lt;blockquote&gt;
x = 3&lt;br&gt;
y = x * 2
&lt;/blockquote&gt;
|}

物事を好きな順番で書いても良い、つまり、「&lt;code&gt;x&lt;/code&gt;が&lt;code&gt;y&lt;/code&gt;の前に宣言された」かその逆かを気にする必要はない。また、このことは一回しかものを宣言できないことの理由でもある。もし2回以上宣言できたとすると、上のコードが曖昧になってしまう。もちろんのことだが、それでも&lt;code&gt;y&lt;/code&gt;を使うには&lt;code&gt;x&lt;/code&gt;の値が必要であるが、これは具体的な数値が必要になるまで重要でない。

今までの説明で、もしかしたら変数が変化しないHaskellで実際一体何が出来るのかと思っているかもしれない。しかし、見捨てないでくれ、この本の残りの部分で説明しようと思っていることだが、変数を一つも変更すること無くこの世のどんなプログラムも書けるようになる。
実際変数が変わらないお陰で&#039;&#039;かなり&#039;&#039;楽になる、というのもそれのお陰でプログラムの挙動がずっと一層予測しやすくなるからだ。これは純粋関数型プログラミングの重要な特徴である。
そして、そのことで命令型プログラミングのやり方や考え方とかなり違ったそれらが必要なのである。

==関数==
では、異なる半径の円の面積を計算したいとする。例えば、半径3の別の円の面積を計算してみよう。書いたばかりのプログラムから作るとすれば、&lt;code&gt;r&lt;/code&gt;は既に&lt;code&gt;5&lt;/code&gt;と定義されている。プログラム全体を&lt;code&gt;r = 3&lt;/code&gt;と変更することが出来る。しかしすると、最初の円が計算できなくなる。代替策は新たな変数&lt;code&gt;r2&lt;/code&gt;と&lt;code&gt;r2&lt;/code&gt;で計算される面積のための変数&lt;code&gt;area2&lt;/code&gt;を定義することだ。

&lt;ref&gt;
この例が示すように、変数名には英字の他に数字含んで良い。(日本語はファイルからだとダメ、プロンプトからは可能)ただし、変数は小文字から始め&#039;&#039;なければならない&#039;&#039;が残りの部分については文字、数字、アンダースコア(_)、シングルクォート(&#039;)(と日本語がギリギリ)からなる任意の文字列を使うことが出来る(日本語には名前の1文字目の制約はない)。
&lt;/ref&gt;

2つの円を対象とした新しいソースファイルは以下のとおり:

&lt;blockquote&gt;
r  = 5&lt;br&gt;
area  = pi*r^2&lt;br&gt;
r2 = 3&lt;br&gt;
area2 = pi*r2^2
&lt;/blockquote&gt;

明らかなことだが、このやり方は円の面積公式を言葉通りに繰り返しているから不満足である。この頭使っていない繰り返しを無くすために一回だけ書いて異なる半径を適用させるのが好ましい。これがこそまさに&#039;&#039;関数&#039;&#039;のが出来ることである。

&#039;&#039;関数&#039;&#039;は&#039;&#039;引数&#039;&#039;値(&#039;&#039;パラメータ&#039;&#039;ともいう)を取り結果の値を与える(これは本質的に数学の関数と同じである)。Haskellでの関数の定義は単純である:変数を定義するように、ただし左辺に付ける関数の引数に気を払うこと。例えば、以下のコードは&lt;code&gt;r&lt;/code&gt;と名前を付けた1引数に依る関数&lt;code&gt;area&lt;/code&gt;の定義である。

&lt;blockquote&gt;
area r = pi * r^2
&lt;/blockquote&gt;

構文を注意深く見てみよう:関数名が最初に来て(上の例では&lt;code&gt;area&lt;/code&gt;)
続いてスペースが一個そして引数(上の例では&lt;code&gt;r&lt;/code&gt;)とがある。
そして=の次に、関数の定義が既に定義した物々とともに筋通りに引数を使う公式がとしてある。

そして、関数の&#039;&#039;呼び出し&#039;&#039;ごとに引数へ異なる値を入れることが出来る。コードをファイルに保存しGHCiに読み込ませて以下を試す:
 *Main&gt; area 5
 78.53981633974483
 *Main&gt; area 3
 28.274333882308138
 *Main&gt; area 17
 907.9202768874502

ゆえに、この関数を異なる半径で呼び出して、いかなる半径の円の面積も計算できる。

ここでの関数は数学的に以下のように定義される
:&lt;math&gt;A(r) = \pi \cdot r^2&lt;/math&gt;
&lt;math&gt;A(5) = 78.54&lt;/math&gt;や&lt;math&gt;A(3) = 28.27&lt;/math&gt;のように数学では、パラメータは括弧によって囲まれる。しかし、Haskellのコードは括弧を付けても機能するが普通は省略される。Haskellは&#039;&#039;関数型&#039;&#039;言語なので、常に関数を使うことになる、なので可能である場合ならいつも余分な記号を最小にしたいのである。

また、括弧は&#039;&#039;式&#039;&#039;(値を与えるコード全てのこと)をともに評価させるためにまとめるのに使われる。以下の2つの式がいかに違う解釈をされるかに注意:

&lt;blockquote&gt;
5 * 3 + 2       -- 15 + 2 = 17 (乗算は加算の前になされる)&lt;br&gt;
5 * (3 + 2)     -- 5 * 5 = 25 (括弧のおかげ)&lt;br&gt;
area 5 * 3      -- (area 5) * 3&lt;br&gt;
area (5 * 3)    -- area 15
&lt;/blockquote&gt;

Haskellがどのように&lt;code&gt;+&lt;/code&gt;や&lt;code&gt;*&lt;/code&gt;と言った他のどの演算子よりも&#039;&#039;優先順序が先&#039;&#039;であるかに注意。これは例えば、数学で乗算が加算よりも先になされることと同じ流れである。

==評価==
GHCiに式を入力したときに厳密に何が起こるのかを理解してみよう。Enterキーを押した後、
GHCiは渡された式を&#039;&#039;評価&#039;&#039;をする。これはそれぞれの関数を定義に置き換え、一つの値になるまで計算をするということだ。例えば、&lt;code&gt;area 5&lt;/code&gt;の評価は以下のように進行する。

    area 5
 =&gt;    {左辺area rを右辺pi * r^2に変える}
    pi * 5^2
 =&gt;    {変数piを数値に変える}
    3.141592653589793 * 5^2
 =&gt;    {べき乗(^)を適用}
    3.141592653589793 * 25
 =&gt;    {乗算(*)を適用}
    78.53981633974483

ここに示す通り、関数の&#039;&#039;適用&#039;&#039;や&#039;&#039;呼び出し&#039;&#039;というのは関数の定義の左辺を右辺に変えるということである。最後の工程として、GHCiは最終結果を画面に表示する。

関数を更に挙げてゆく:

&lt;blockquote&gt;
double x    = 2*x&lt;br&gt;
quadruple x = double (double x)&lt;br&gt;
square x    = x*x&lt;br&gt;
half   x    = x / 2
&lt;/blockquote&gt;
(quadrupleは定義を見れば分かる通り4倍のこと)

{|style=&quot;margin:0 auto&quot;
|valign=&quot;top&quot; style=&quot;border-right: 1px dashed SlateBlue&quot;|
&lt;p style=&quot;text-align:center;&quot;&gt;演習問題&lt;/p&gt;
*GHCiによって&lt;code&gt;quadruple 5&lt;/code&gt;がどのように評価されるかを説明しなさい
*引数の半分から12を引く(引いた結果を返す)関数を定義しなさい
|}

==複数のパラメータ==

もちろん、関数は2つ以上の引数を持つことも出来る。例えば、以下は幅と高さ(lengthとなっている)が与えられた長方形の面積を計算する関数である:

&lt;blockquote&gt;
areaRect l w = l * w
&lt;/blockquote&gt;

 *Main&gt; areaRect 5 10
 50

三角形の面積を計算する別の例:

&lt;math&gt;\left(A = \frac{bh}{2}\right)&lt;/math&gt;(底辺×高さ÷2で求める)

&lt;blockquote&gt;
areaTriangle b h = (b * h) / 2
&lt;/blockquote&gt;

 *Main&gt; areaTriangle 3 9
 13.5

例から分かる通り、複数の引数はスペース(全角半角は問わない)で分ける。これも
式をまとめるのに括弧を使わなければならないことがある理由である。例えば、値
&lt;code&gt;x&lt;/code&gt;を4倍するのに以下のようには書けない

&lt;blockquote&gt;
quadruple x = double double x
&lt;/blockquote&gt;

理由はこの書き方は&lt;code&gt;double&lt;/code&gt;という名前の関数を2つの引数&lt;code&gt;double&lt;/code&gt;と&lt;code&gt;x&lt;/code&gt;に適用する意図となるからである、というのも&#039;&#039;関数は他の関数の引数になりうる&#039;&#039;(後々理由を説明する)。代わりの、引数の周りに括弧を付けなければならない:

&lt;blckquote&gt;
quadruple x = double (double x)
&lt;/blockquote&gt;

引数は常に与えられた順番通りに渡される。例えば:

&lt;blockquote&gt;
subtract x y = x - y
&lt;/blockquote&gt;

 *Main&gt; subtract 10 5
 5
 *Main&gt; subtract 5 10
 -5

ここの例では&lt;code&gt;subtract 10 5&lt;/code&gt;は&lt;code&gt;10 - 5&lt;/code&gt;に評価するが、
&lt;code&gt;subtract 5 10&lt;/code&gt;は順番が違うので&lt;code&gt;5 - 10&lt;/code&gt;に評価する。

{|style=&quot;margin:0 auto&quot;
|valign=&quot;top&quot; style=&quot;border-right: 1px dashed SlateBlue&quot;|
&lt;p style=&quot;text-align:center;&quot;&gt;演習問題&lt;/p&gt;
*直方体の体積を求める関数を書きなさい
*有名なギザのピラミッドに用いられた石の数をおおよそで求めなさい
ヒント:ピラミッドの体積と1ブロックの体積を見積もる必要がある。(Wikipediaに大きさが載っているが答えの石の数も書いてあるのが難点。また中に穴が開いているということ気にしなくて良い)
|}

==関数を組み合わせることについての意見==

言わずもがな、加算&lt;code&gt;(+)&lt;/code&gt;や乗算&lt;code&gt;(*)&lt;/code&gt;といった予め定義された関数がが使えるのと同様に、新しい関数を定義するのに既に定義した関数を使うことができる。(Haskellでは演算子は関数として定義されている)例えば、正方形の面積を計算するために、
長方形の面積を計算する関数を再利用することが出来る:

&lt;blockquote&gt;
areaRect l w = l * w&lt;br&gt;
areaSquare s = areaRect s s
&lt;/blockquote&gt;

 *Main&gt; areaSquare 5
 25

そもそも、正方形は辺の長さが等しい長方形なだけである。

この原則はかなり単純に思えるかもしれないが、とても強力である。特に、数字の代わりに他のオブジェクトで計算し始めるときはそうである。

{|style=&quot;margin:0 auto&quot;
|valign=&quot;top&quot; style=&quot;border-right: 1px dashed SlateBlue&quot;|
&lt;p style=&quot;text-align:center;&quot;&gt;演習問題&lt;/p&gt;
*円柱の体積を計算する関数を書きなさい。円柱の体積は円形である底面積(この章で既にこの関数は作った)に高さを掛けて求める。
|}

==ローカル定義==
===&lt;code&gt;where&lt;/code&gt;節===
関数を定義する際、関数に&#039;&#039;ローカルな&#039;&#039;中間の結果を定義することはよくある。例えば、
3辺の長さ&lt;code&gt;a&lt;/code&gt;、&lt;code&gt;b&lt;/code&gt;、&lt;code&gt;c&lt;/code&gt;が与えられている三角形の面積を計算する[http://ja.wikipedia.org/wiki/%E3%83%98%E3%83%AD%E3%83%B3%E3%81%AE%E5%85%AC%E5%BC%8F ヘロンの公式]&lt;math&gt;A = \sqrt{s(s-a)(s-b)(s-c)}&lt;/math&gt;を考える:

&lt;blockquote&gt;
heron a b c = sqrt (s*(s-a)*(s-b)*(s-c))&lt;br&gt;
□・・where&lt;br&gt;
□・・s = (a+b+c) / 2&lt;br&gt;
&lt;/blockquote&gt;

変数&lt;code&gt;s&lt;/code&gt;は三角形の周の長さの半分で平方根を求める関数&lt;code&gt;sqrt&lt;/code&gt;
の引数の中に4回書き出すのはただ長いだけで退屈である。

実は単に定義を順に書き連ねるだけではうまくいかない――

&lt;blockquote&gt;
heron a b c = sqrt (s*(s-a)*(s-b)*(s-c))  -- sはここでは定義されていない&lt;br&gt;
s = (a+b+c) / 2                           -- a,b,cはここで定義されていない
&lt;/blockquote&gt;

――なぜなら変数&lt;code&gt;a&lt;/code&gt;、&lt;code&gt;b&lt;/code&gt;、&lt;code&gt;c&lt;/code&gt;は関数&lt;code&gt;heron&lt;/code&gt;の右辺の中のみで利用可能であるが、ここに書かれている&lt;code&gt;s&lt;/code&gt;の定義は&lt;code&gt;heron&lt;/code&gt;の右辺の外であるからだ。この式を右辺の一部にするために、
&lt;code&gt;where&lt;/code&gt;キーワードを使う必要がある。

下に続く定義と区別するために&lt;code&gt;where&lt;/code&gt;とローカル定義がスペース4つで&#039;&#039;インデント&#039;&#039;されていることに注意。(スペースがうまく出力されないので&quot;□・・&quot;と書いた。
入力の際にはスペースに変えること)以下はローカルとトップレベルの定義を併用した例である。([[Haskell/変数と関数/fig1.png|fig.1]])

&lt;blockquote&gt;
areaTriangleTrig  a b c = c * height / 2   --三角法を用いた方法&lt;br&gt;
□・・where&lt;br&gt;
□・・cosa   = (b^2 + c^2 - a^2) / (2*b*c) --余弦定理&lt;br&gt;
□・・sina   = sqrt (1 - cosa^2)           --cos x ^2+sin x ^ 2 =1&lt;br&gt;
□・・height = b*sina&lt;br&gt;
areaTriangleHeron a b c = result           --ヘロンの公式を用いた方法&lt;br&gt;
□・・where&lt;br&gt;
□・・result = sqrt (s*(s-a)*(s-b)*(s-c))&lt;br&gt;
□・・s      = (a+b+c)/2
&lt;/blockquote&gt;

==スコープ==
さっきの例を注意深く見ると、変数名&lt;code&gt;a&lt;/code&gt;、&lt;code&gt;b&lt;/code&gt;、&lt;code&gt;c&lt;/code&gt;を
2つの面積を計算する関数のそれぞれ1回ずつ、計2回ずつ使っていることに気づくだろう。
これが正しく動作する仕組みについて説明してゆく。

幸運にも、以下のコードの断片はは嬉しくない驚きは無い:

 Prelude&gt; let r = 0
 Prelude&gt; let area r = pi * r ^ 2
 Prelude&gt; area 5
 78.53981633974483

ここでの&quot;嬉しくない驚き&quot;とは&lt;code&gt;let r = 0&lt;/code&gt;の定義が入り込んで、areaの結果
&lt;code&gt;0&lt;/code&gt;を返すことである。これは&lt;code&gt;r&lt;/code&gt;を2回目に定義したとき、1回目の
&lt;code&gt;r&lt;/code&gt;と&#039;&#039;違う&#039;&#039;&lt;code&gt;r&lt;/code&gt;について話題にしているからだ。これは実生活でも
起こることだ。あなたは、Johnという名前の人を何人知っているか？Johnという名前の人
に関して興味のあることは、殆どの場合、友達と&quot;John&quot;について話ができることであり、
文脈により、どのJohnを指しているのかが友達がわかることで、このことを&#039;&#039;[http://ja.wikipedia.org/wiki/%E3%82%B9%E3%82%B3%E3%83%BC%E3%83%97 スコープ]&#039;&#039;と呼ぶ。

スコープの専門的な裏側については説明しないつもりである(少なくとも今回は)。なので、
パラメータの値は関数の定義でどの変数を呼び出したかによらず厳格に関数を呼び出す時に渡した通りであるということを覚えておくだけでよい。
{| 
|valign=&quot;top&quot; style=&quot;border-right: 1px dashed SlateBlue&quot;|
補足
上の例では&lt;code&gt;r&lt;/code&gt;は一番最後の定義に則るようにも見える。しかしこれは
 Prelude&gt;r
を試すことで間違いであると判る。(0と帰ってくる)よって、この2つの&lt;code&gt;r&lt;/code&gt;
は別物である。
|}

==まとめ==
#変数は値を格納する。実際はHaskellの任意の式を格納する。
#変数は変化しない。
#関数は再利用するコードを書くのに役立つ。
#関数は複数個パラメータを持つことも出来る。

そしてコメントはソースファイルの中のコードでない文章であることも学んだ

[[Haskell/解答/変数と関数|演習問題の解答]]

[[Haskell]]

[[Haskell/真偽値型|次:真偽値型]]    </description>
    <dc:date>2014-08-21T15:54:43+09:00</dc:date>
    <utime>1408604083</utime>
  </item>
    <item rdf:about="https://w.atwiki.jp/mamonbo/pages/23.html">
    <title>Haskell/変数と関数/fig1.png</title>
    <link>https://w.atwiki.jp/mamonbo/pages/23.html</link>
    <description>
      #ref(tri.png)
製作者:Mamonbo
ライセンス:パブリックドメイン(頭使わなくてもこんなもの作れるため)    </description>
    <dc:date>2014-08-21T15:45:51+09:00</dc:date>
    <utime>1408603551</utime>
  </item>
    <item rdf:about="https://w.atwiki.jp/mamonbo/pages/17.html">
    <title>Haskell-logo.png</title>
    <link>https://w.atwiki.jp/mamonbo/pages/17.html</link>
    <description>
      @wikiでのwikipedia like modeでは画像の挿入は検討中とのこと
ちなみにこのページは@wiki mode
#ref(Haskell-logo.png)
ライセンス:パブリックドメイン    </description>
    <dc:date>2014-08-21T15:21:36+09:00</dc:date>
    <utime>1408602096</utime>
  </item>
    <item rdf:about="https://w.atwiki.jp/mamonbo/pages/21.html">
    <title>Haskell/環境の構築</title>
    <link>https://w.atwiki.jp/mamonbo/pages/21.html</link>
    <description>
      この章ではHaskellでコードを書くときに必要となるプログラムのインストール方法について説明する。

==Haskellのインストール==
Haskellは&#039;&#039;プログラミング言語&#039;&#039;、人が計算機がどう振る舞うべきかをを表せる言語である。
料理のレシピを書くような感じで、あなたがレシピを書いて計算機がレシピを実行してくれる。

Haskellのプログラムを使うためには、Haskell&#039;&#039;コンパイラ&#039;&#039;と呼ばれる特別なプログラムが必要である。コンパイラはHaskellで書かれたコードを受け取って計算機が理解出来るより原始的な言語である&#039;&#039;機械語&#039;&#039;に翻訳する。
(機械が理解出来るのが機械語で、(設計さえやれば)バイナリである必要は無くBrainf*ckでも機械語になりうる　という話も)

上の料理の例えを使えば、あなたがレシピなるHaskellプログラムを書いて、料理人にあたるコンパイラプログラムが実際の材料を合わせて食べられる料理にあたる実行可能ファイルを作る作業をするのである。もちろん、完成した料理から簡単にはレシピを知ることは出来ない、つまり
コンパイル後は実行可能ファイルからHaskellのコードを知ることが出来ない。

Haskell学習を始めるために&#039;&#039;&#039;[http://hackage.haskell.org/platform/ Haskellプラットフォーム]をダウンロードしインストールする&#039;&#039;&#039;。中には&quot;Glasgow Haskell Compiler&quot;略してGHCと他の必要なものすべてが入っているだろう。

もしただHaskellを試してみたいとか完全なコンパイラをダウンロード、インストールするのが
嫌なら[http://www.haskell.org/hugs/ Hugs]が使える。もしくはオンライン管理のインタプリタである[http://www.tryhaskell.org/ TryHaskell]で遊んでみるのもいいかもしれない。
なお、この本での説明ではすべてGHC用になる。
([http://ideone.com/ Ideone]でもオンラインで実行できる)


{| 
|valign=&quot;top&quot; style=&quot;border-right: 1px dashed SlateBlue&quot;|
補足

UNIXユーザーへ(他の人は飛ばして良い):

GHCについてはソースからコンパイルするのはお薦めしない。特に初回のインストールにはお薦めしない。GHCはそれ自身がほとんどHaskellで書かれているのでソースから人力でブートストラップを試みるのはとても技が混む。その上、ビルド作業は&#039;&#039;実に&#039;&#039;時間が掛かり大量の記憶領域を消費する。それでも本当にGHCをソースからビルドしたいのなら[http://hackage.haskell.org/trac/ghc/wiki/Building Building and Porting GHC at the GHC homepage(GHCのホームページにあるGHCのビルドと移植)]を参照のこと。(もちろんのことだが、それらに加えて英文読解という壁がある)

短くまとめるとソースからコンパイルする代わりにHaskell Platformをダウンロードすることを強く薦める。
|}

==第一歩==
[http://hackage.haskell.org/platform/ Haskell Platform]をインストールした後は、初めてのHaskellのコードを書く時である。

そのために、&#039;&#039;&#039;GHCi&#039;&#039;&#039;(iは&#039;interactive&#039;の略)と呼ばれるプログラムを使う。やり方はOSによって以下の通り:

*Windows:&#039;&#039;&#039;スタート&#039;&#039;&#039;をクリック、&#039;&#039;&#039;ファイル名を指定して実行&#039;&#039;&#039;、&#039;cmd&#039;と入力しEnterキーを押し、そして&lt;code&gt;ghci&lt;/code&gt;と入力しもう一度Enterキーを押す
(Windows 8ではスタートメニューからアプリ一覧を出し右上のボックスに&#039;cmd&#039;と入力)

(cmdのところにghciでも可)
*MacOS:&quot;Applications/Utilities&quot;(&quot;アプリケーション/ユーティリティ&quot;とかになっているかも)フォルダにある&quot;ターミナル&quot;(端末? Mac持ってないからわからない)を開き&lt;code&gt;ghci&lt;/code&gt;と入力しEnterキーを押す
*ターミナル(端末)を開きghciプログラムを実行

すると以下の様なのが出力されるはずである:
 GHCi, version 7.6.3: http://www.haskell.org/ghc/  :? for help
 Loading package ghc-prim ... linking ... done.
 Loading package integer-gmp ... linking ... done.
 Loading package base ... linking ... done.
 Prelude&gt; 


最初の部分はGHCiのバージョンである。そして次に基本パッケージを読み込んでいること伝えている。なのでGHCに付属する組み込み関数やモジュールのほとんどがつかえる。最後に、&lt;code&gt;Prelude&gt;&lt;/code&gt;の部分はいわゆる&#039;&#039;プロンプト&#039;&#039;である。ここの部分にコマンドを入力して、GHCiが結果を返す。

(Preludeは前置きといったところ)

これで初めてのHaskellのコードを書く準備は整った。特に今回は基本的な計算をやってみよう:

 Prelude&gt; 2 + 2
 4
 Prelude&gt; 5 + 4 * 3
 17
 Prelude&gt; 2 ^ 5
 32

Haskellの演算子は他の言語のそれと似ている、具体的には&lt;code&gt;+&lt;/code&gt;は加算、&lt;code&gt;*&lt;/code&gt;は乗算、&lt;code&gt;^&lt;/code&gt;べき乗(&lt;math&gt;a ^ b&lt;/math&gt;)である。注目すべき点として2番目の例でHaskellは標準的な演算順序にの則って計算している。(掛け算は足し算より先　ということ)

これで、Haskellを電卓として使うことが出来る。実のところ、Haskellはいつも基本的には電卓なのである、しかしながらとても強力で数字以外のオブジェクトー文字やリストや関数や木や他のプログラムまでも―も扱える。(ここで出てきた用語に慣れていなくても心配しなくてよい)

GHCiはとても強力な開発環境である。この本が進むに連れ、ソースコードの入ったファイルをGHCiに読み込ませ、ファイルの別々の部分を評価する方法を学ぶ。
{| 
|valign=&quot;top&quot; style=&quot;border-right: 1px dashed SlateBlue&quot;|
補足

ここで「評価」という言葉が出てきたので説明を入れておくと、Lisp界隈では実行のことをよく「評価」という。そして&#039;&#039;Haskell共和国&#039;&#039;は&#039;&#039;Lisp国&#039;&#039;と陸続きである。(Conrad Barski, M.D.談)
|}

今のところ全部理解しているのなら次の章へ準備ができている。次章では初めての関数に沿ってHaskellの基本的な概念をいくらか紹介していく。(理解していない人はトークのページを使ってこのWikibookを改善するのに力を貸して)(といっても小生には質問しないで)

[[Haskell]]

[[Haskell/変数と関数|次:変数と関数]]    </description>
    <dc:date>2014-08-19T21:09:03+09:00</dc:date>
    <utime>1408450143</utime>
  </item>
    <item rdf:about="https://w.atwiki.jp/mamonbo/pages/22.html">
    <title>Haskell</title>
    <link>https://w.atwiki.jp/mamonbo/pages/22.html</link>
    <description>
      [[Wikibooks:Haskell勝手に日本語訳]]へ    </description>
    <dc:date>2014-08-15T13:16:19+09:00</dc:date>
    <utime>1408076179</utime>
  </item>
    <item rdf:about="https://w.atwiki.jp/mamonbo/pages/20.html">
    <title>プログラム</title>
    <link>https://w.atwiki.jp/mamonbo/pages/20.html</link>
    <description>
      //haskell
ghci&gt; 3.1416 * 5^2
78.53999999999999    </description>
    <dc:date>2014-08-13T18:20:56+09:00</dc:date>
    <utime>1407921656</utime>
  </item>
    <item rdf:about="https://w.atwiki.jp/mamonbo/pages/19.html">
    <title>チルノ</title>
    <link>https://w.atwiki.jp/mamonbo/pages/19.html</link>
    <description>
      ==サンドボックス!==

{|width=&quot;100%&quot;
|valign=&quot;top&quot;|
ひょう
|}    </description>
    <dc:date>2014-08-13T18:14:11+09:00</dc:date>
    <utime>1407921251</utime>
  </item>
  </rdf:RDF>
