<?xml version="1.0" encoding="Shift_JIS" ?>
<!-- <?xml-stylesheet href="xsl.xml" type="text/xsl"?> -->
<rdf:RDF
 xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
 xmlns="http://purl.org/rss/1.0/"
 xmlns:dc="http://purl.org/dc/elements/1.1/"
 xmlns:content="http://purl.org/rss/1.0/modules/content/"
>
    <channel rdf:about="http://www.hyuki.com/haskell/rss">
        <title>Haskell</title>
        <link>http://www.hyuki.com/haskell</link>
        <description>Haskell勉強中。</description>
        <items>
            <rdf:Seq>
                
                    <rdf:li rdf:resource="http://www.hyuki.com/haskell/200606#i20060610223315" />
                
                    <rdf:li rdf:resource="http://www.hyuki.com/haskell/200503#i20050327220127" />
                
                    <rdf:li rdf:resource="http://www.hyuki.com/haskell/200501#i20050113111044" />
                
                    <rdf:li rdf:resource="http://www.hyuki.com/haskell/200501#i20050113092902" />
                
                    <rdf:li rdf:resource="http://www.hyuki.com/haskell/200501#i20050108221142" />
                
                    <rdf:li rdf:resource="http://www.hyuki.com/haskell/200501#i20050108153507" />
                
                    <rdf:li rdf:resource="http://www.hyuki.com/haskell/200501#i20050107232117" />
                
                    <rdf:li rdf:resource="http://www.hyuki.com/haskell/200501#i20050105204527" />
                
                    <rdf:li rdf:resource="http://www.hyuki.com/haskell/200501#i20050104223056" />
                
                    <rdf:li rdf:resource="http://www.hyuki.com/haskell/200501#i20050103231209" />
                
                    <rdf:li rdf:resource="http://www.hyuki.com/haskell/200412#i20041229215308" />
                
                    <rdf:li rdf:resource="http://www.hyuki.com/haskell/200412#i20041229175243" />
                
                    <rdf:li rdf:resource="http://www.hyuki.com/haskell/200412#i20041229165834" />
                
                    <rdf:li rdf:resource="http://www.hyuki.com/haskell/200412#i20041228215300" />
                
                    <rdf:li rdf:resource="http://www.hyuki.com/haskell/200412#i20041228144015" />
                
            </rdf:Seq>
        </items>
        <image rdf:resource="" />
    </channel>

    <image rdf:about="">
        <title>Haskell</title>
        <link>http://www.hyuki.com/haskell</link>
        <url></url>
    </image>

    
        <item rdf:about="http://www.hyuki.com/haskell/200606#i20060610223315">
            <title>Haskellグループ</title>
            <link>http://www.hyuki.com/haskell/200606#i20060610223315</link>
            <description>最近のHaskell勉強日記は、はてなグループで書いています。</description>
            <dc:date>2006-06-10T22:33:15+09:00</dc:date>
            
            <content:encoded><![CDATA[
                <p>最近のHaskell勉強日記は、はてなグループで書いています。
</p>
<ul><li>
<a href="http://haskell.g.hatena.ne.jp/hyuki/">Haskellの勉強 - 結城浩のHaskell日記</a>
</li>
<li>
<a href="http://haskell.g.hatena.ne.jp/">haskellグループ</a>
</li></ul>

            ]]></content:encoded>
        </item>
    
        <item rdf:about="http://www.hyuki.com/haskell/200503#i20050327220127">
            <title>既約分数クイズ</title>
            <link>http://www.hyuki.com/haskell/200503#i20050327220127</link>
            <description>既約分数クイズに対するHaskell版解答集。</description>
            <dc:date>2005-03-27T22:01:27+09:00</dc:date>
            
            <content:encoded><![CDATA[
                <p><a href="http://www.hyuki.com/dig/frac.html">既約分数クイズ</a>に対するHaskell版解答集。
</p>
<ul><li>
<a href="http://www.sampou.org/cgi-bin/haskell.cgi?p=Programming%3a%b6%cc%bc%ea%c8%a2%3a%c0%b0%bf%f4%cf%c0&l=jp#1">nobsunによる</a>
</li>
<li>
<a href="http://d.hatena.ne.jp/Nabetani/20050325#p1">Nabetaniさんによる</a>
</li>
<li>
<a href="http://d.hatena.ne.jp/KimuraShinichi/20050327/1111919561">KimuraShinichiさんによる</a>
</li>
<li>
<a href="http://www.hyuki.com/dig/fracans.html">既約分数クイズに対する答え</a>
</li></ul>

            ]]></content:encoded>
        </item>
    
        <item rdf:about="http://www.hyuki.com/haskell/200501#i20050113111044">
            <title>Yuki::RSSをHaskellに移植→中断</title>
            <link>http://www.hyuki.com/haskell/200501#i20050113111044</link>
            <description>適当な大きさの例題として、Yuki::RSSをHaskellに移植しようと考えた。作りかけたけれど、それ以前にハッシュテーブルや、タグライブラリなどを作る/使うほうがよいとわかって中断。</description>
            <dc:date>2005-01-13T11:10:44+09:00</dc:date>
            
            <content:encoded><![CDATA[
                <p>適当な大きさの例題として、
<a href="../techinfo/yukirss.html">Yuki::RSS</a>をHaskellに移植しようと考えた。
作りかけたけれど、それ以前にハッシュテーブルや、
タグライブラリなどを作る/使うほうがよいとわかって中断。
</p>
<pre class="display">\begin{code}

attr :: String -&gt; String -&gt; String
attr a v = a ++ &quot;=\&quot;&quot; ++ v ++ &quot;\&quot;&quot;

xmlns_rdf   = attr &quot;xmlns:rdf&quot; &quot;http://www.w3.org/1999/02/22-rdf-syntax-ns#&quot;
xmlns       = attr &quot;xmlns&quot; &quot;http://purl.org/rss/1.0/&quot;
xmlns_dc    = attr &quot;xmlns:dc&quot; &quot;http://purl.org/dc/elements/1.1/&quot;

xmlhead enc = &quot;&lt;?xml &quot; ++ (attr &quot;version&quot; &quot;1.0&quot;) ++ &quot; &quot; ++ (attr &quot;encoding&quot; enc) ++ &quot; ?&gt;&quot;

data Rss = Rss Head Channel Body
data Head = Head [(String, String)]
data Channel = Channel [(String, String)]
data Body = Body [Item]
data Item = Item [(String, String)]

addItem :: Rss -&gt; Item -&gt; Rss
addItem (Rss h c (Body is)) i = (Rss h c (Body (is++[i])))

instance Show Rss where
    show (Rss h c b) = (headstr h) ++ (channelstr c) ++ (bodystr b)
        where
            itemstr :: Item -&gt; String
            headstr :: Head -&gt; String
            channelstr :: Channel -&gt; String
            bodystr :: Body -&gt; String

            headstr (Head []) = error &quot;headstr&quot;
            headstr (Head ((&quot;encoding&quot;,enc):xs)) = (xmlhead enc) ++ &quot;...\n&quot;
            headstr (Head ((x,y):xs)) = headstr (Head xs)
            headstr _ = error &quot;headstr&quot;

            channelstr _ = &quot;...Channel...\n&quot;

            bodystr (Body (x:xs)) = (itemstr x) ++ (bodystr (Body xs))
            bodystr _ = &quot;\n&quot;

            itemstr _ = &quot;...item...\n&quot;

rss1 = Rss
    (Head [
        (&quot;version&quot;, &quot;1.0&quot;),
        (&quot;encoding&quot;, &quot;Shift_JIS&quot;)
    ])
    (Channel [
        (&quot;title&quot;, &quot;Site Title&quot;),
        (&quot;link&quot;, &quot;http://www.example.com/index.html&quot;),
        (&quot;about&quot;, &quot;http://www.example.com/rss.rdf&quot;),
        (&quot;description &quot;, &quot;The description of your site&quot;)
    ])
    (Body [
    ])

rss2 = addItem rss1
    (Item [
        (&quot;title&quot;, &quot;Item Title&quot;),
        (&quot;link&quot;, &quot;http://www.example.com/item.html&quot;),
        (&quot;description&quot;, &quot;Yoo, hoo, hoo&quot;),
        (&quot;dc_date&quot;, &quot;2003-12-06T01:23:45+09:00&quot;)
    ])

\end{code}
</pre>
<pre>Main&gt; rss1
&lt;?xml version=&quot;1.0&quot; encoding=&quot;Shift_JIS&quot; ?&gt;...
...Channel...


Main&gt; rss2
&lt;?xml version=&quot;1.0&quot; encoding=&quot;Shift_JIS&quot; ?&gt;...
...Channel...
...item...
</pre>


            ]]></content:encoded>
        </item>
    
        <item rdf:about="http://www.hyuki.com/haskell/200501#i20050113092902">
            <title>Default definitions</title>
            <link>http://www.hyuki.com/haskell/200501#i20050113092902</link>
            <description>CRAFT 12.3を読んでいる。</description>
            <dc:date>2005-01-13T09:29:02+09:00</dc:date>
            
                <dc:subject>craft</dc:subject>
            
            <content:encoded><![CDATA[
                <p>CRAFT 12.3を読んでいる。
</p><p>(==)と(/=)の定義が以下のようになっているのを見て、最初は驚いた。
</p>
<pre>class Eq a where
    (==) :: a -&gt; a -&gt; Bool
    x == y = not (x /= y)

    (/=) :: a -&gt; a -&gt; Bool
    x /= y = not (x == y)
</pre>
<p>無限ループになってしまうような気がしたからだ。
でも、そうではなく、
Eqのinstanceが、(==)と(/=)のどちらか一方でも定義すればよいということになる。
</p><p>Javaでメソッドのoverrideというと、スーパークラスのメソッドをoverrideすることを意味するが、
Haskellではclassの演算子をinstanceがoverrideするらしい。
</p><p>練習してみよう。
</p>
<pre class="display">module Main where

data Color = Red | Pink | Green | Grass | Blue | Skyblue

instance Eq Color where
    Red == Pink = True
    Green == Grass = True
    Blue == Skyblue = True
    _ == _ = False

t1 = Red == Pink
t2 = Red /= Pink
t3 = Green == Skyblue
t4 = Green /= Skyblue
</pre>
<pre>Main&gt; t1
True
Main&gt; t2
False
Main&gt; t3
False
Main&gt; t4
True
</pre>
<p>ここでは、Colorというtypeに対して演算子(==)だけを定義している。
しかしちゃんと、(==)と(/=)が使えるようになっている。
</p><p>ちなみに、ColorというtypeはEqというclassのinstanceである。
Javaのclassはtypeとほぼ同義だが、Haskellのclassはtypeの集まりである。
用語が微妙に違うので注意が必要である。
</p><p>ところで、もしも(==)と(/=)の両方を定義しなかったらどうなるだろう。
以下のように、スタックオーバーフローを起こす。
</p>
<pre class="display">module Main where

data Color = Red | Pink | Green | Grass | Blue | Skyblue

instance Eq Color

t = Red == Pink
</pre>
<pre>Main&gt; t
ERROR - C stack overflow
</pre>


            ]]></content:encoded>
        </item>
    
        <item rdf:about="http://www.hyuki.com/haskell/200501#i20050108221142">
            <title>class, instance</title>
            <link>http://www.hyuki.com/haskell/200501#i20050108221142</link>
            <description>CRAFT 12.2, 12.3を見ながら、classの練習。</description>
            <dc:date>2005-01-08T22:11:42+09:00</dc:date>
            
                <dc:subject>craft</dc:subject>
            
            <content:encoded><![CDATA[
                <p>CRAFT 12.2, 12.3を見ながら、classの練習。
</p>
<pre class="display">\begin{code}
module Main where
import Char

class Incrementable a where
    inc :: a -&gt; a

instance Incrementable Char where
    inc c = chr ((ord c) + 1)

t1 = inc '3'
t2 = inc 'A'

instance Incrementable a =&gt; Incrementable [a] where
    inc = map inc

t3 = inc ['A'..'D']
t4 = inc &quot;HAL&quot;

\end{code}
</pre>
<pre>Main&gt; t1
'4'
Main&gt; t2
'B'
Main&gt; t3
&quot;BCDE&quot;
Main&gt; t4
&quot;IBM&quot;
</pre>


            ]]></content:encoded>
        </item>
    
        <item rdf:about="http://www.hyuki.com/haskell/200501#i20050108153507">
            <title>Do-it-yourself operators</title>
            <link>http://www.hyuki.com/haskell/200501#i20050108153507</link>
            <description>CRAFT 3.7 (p.51)とAppendix Cを見ながら、C言語風の % で `mod` の代わりにしてみる。</description>
            <dc:date>2005-01-08T15:35:07+09:00</dc:date>
            
                <dc:subject>craft</dc:subject>
            
            <content:encoded><![CDATA[
                <p>CRAFT 3.7 (p.51)とAppendix Cを見ながら、C言語風の % で `mod` の代わりにしてみる。
</p>
<pre class="display">\begin{code}
module Main where

(%) :: Integral a =&gt; a -&gt; a -&gt; a
infixl 7 %
(%) = mod

t1 = 7 `mod` 3
t2 = 7 % 3
t3 = 7 `mod` (-3)
t4 = 7 % (-3)
t5 = 7 `mod` 0
t6 = 7 % 0

\end{code}
</pre>
<pre>Main&gt; t1
1
Main&gt; t2
1
Main&gt; t3
-2
Main&gt; t4
-2
Main&gt; t5

Program error: divide by zero

Main&gt; t6

Program error: divide by zero

</pre>


            ]]></content:encoded>
        </item>
    
        <item rdf:about="http://www.hyuki.com/haskell/200501#i20050107232117">
            <title>Introduction to Functional Programming using Haskell</title>
            <link>http://www.hyuki.com/haskell/200501#i20050107232117</link>
            <description>アマゾンからIntroduction to Functional Programming using Haskellが届いた。でも、これを読むのはThe Craft of Functional Programmingを読み終えてからになるだろう。</description>
            <dc:date>2005-01-07T23:21:17+09:00</dc:date>
            
                <dc:subject>book</dc:subject>
            
            <content:encoded><![CDATA[
                <p>アマゾンから
Introduction to Functional Programming using Haskell
が届いた。
でも、これを読むのはThe Craft of Functional Programmingを読み終えてからになるだろう。
</p>
<ul><li>
<a href="http://www.amazon.co.jp/exec/obidos/ASIN/0134843460/wwwhyukcomhir-22/">『Introduction to Functional Programming Using Haskell』Richard Bird</a>
</li></ul>
            ]]></content:encoded>
        </item>
    
        <item rdf:about="http://www.hyuki.com/haskell/200501#i20050105204527">
            <title>:find</title>
            <link>http://www.hyuki.com/haskell/200501#i20050105204527</link>
            <description>The Craft of Functional Programmingをはじめから読んでいる。これはとてもていねいなよい教科書である。</description>
            <dc:date>2005-01-05T20:45:27+09:00</dc:date>
            
                <dc:subject>craft</dc:subject>
            
            <content:encoded><![CDATA[
                <p>The Craft of Functional Programmingをはじめから読んでいる。
これはとてもていねいなよい教科書である。
</p><p>Hugsのコマンドに:findというのがあるのを知る。
たとえば、
<em> :find map </em>
と入力すると、mapの定義をエディタで見ることができる。
</p><p>秀丸エディタを設定するには、たとえば、次のようなバッチファイルからHugsを起動すると楽。
</p>
<pre>&quot;C:\Program Files\Hugs98\Hugs&quot; -E&quot;C:\Hidemaru\Hidemaru.exe /j%%d %%s&quot; %1 %2 %3 %4 %5 %6 %7 %8 %9
</pre>


            ]]></content:encoded>
        </item>
    
        <item rdf:about="http://www.hyuki.com/haskell/200501#i20050104223056">
            <title>Haskellの本</title>
            <link>http://www.hyuki.com/haskell/200501#i20050104223056</link>
            <description>アマゾンからHaskellの本が届いた。</description>
            <dc:date>2005-01-04T22:30:56+09:00</dc:date>
            
                <dc:subject>craft</dc:subject>
            
            <content:encoded><![CDATA[
                <p>アマゾンからHaskellの本が届いた。
</p>
<ul><li>
<a href="http://www.amazon.co.jp/exec/obidos/ASIN/0201342758/wwwhyukcomhir-22/">『Haskell: The Craft of Functional Programming (2nd Ed.) 』Simon Thompson</a>
</li></ul>
            ]]></content:encoded>
        </item>
    
        <item rdf:about="http://www.hyuki.com/haskell/200501#i20050103231209">
            <title>テンポを落とします</title>
            <link>http://www.hyuki.com/haskell/200501#i20050103231209</link>
            <description>あけましておめでとうございます。昨年末は妙にHaskellに燃えてしまいましたが、今年はもう少しじっくり学んでいこうと思います。ので、更新はぼちぼちと。</description>
            <dc:date>2005-01-03T23:12:09+09:00</dc:date>
            
            <content:encoded><![CDATA[
                <p>あけましておめでとうございます。
昨年末は妙にHaskellに燃えてしまいましたが、
今年はもう少しじっくり学んでいこうと思います。
ので、更新はぼちぼちと。
</p>


            ]]></content:encoded>
        </item>
    
        <item rdf:about="http://www.hyuki.com/haskell/200412#i20041229215308">
            <title>CommonHaskellIdioms</title>
            <link>http://www.hyuki.com/haskell/200412#i20041229215308</link>
            <description>CommonHaskellIdiomsを眺めている。</description>
            <dc:date>2004-12-29T21:53:08+09:00</dc:date>
            
                <dc:subject>hawiki</dc:subject>
            
            <content:encoded><![CDATA[
                <p><a href="http://haskell.org/hawiki/CommonHaskellIdioms">CommonHaskellIdioms</a>を眺めている。
</p><p><a href="http://haskell.org/hawiki/PointFreeStyle">PointFreeStyle</a>は、引数を書かず（考えず）、関数のcompositionで関数を定義するスタイル。好ましいスタイル。
</p><p><a href="http://haskell.org/hawiki/HigherOrderFunctions">HigherOrderFunctions</a>のページでは、
関数の共通の振る舞いをくくりだす話が書かれている。
</p>

            ]]></content:encoded>
        </item>
    
        <item rdf:about="http://www.hyuki.com/haskell/200412#i20041229175243">
            <title>モナドで連番付け(2)</title>
            <link>http://www.hyuki.com/haskell/200412#i20041229175243</link>
            <description>SOE 18.4 State Monadsを見ながら、自分でわかりやすいように書き換えたり、コメントをつけたり、道に迷ったり。</description>
            <dc:date>2004-12-29T17:52:43+09:00</dc:date>
            
                <dc:subject>monad, soe</dc:subject>
            
            <content:encoded><![CDATA[
                <p>SOE 18.4 State Monadsを見ながら、
自分でわかりやすいように書き換えたり、
コメントをつけたり、道に迷ったり。
</p><p>「関数」というものを直接イメージするのがまだ苦手なので、引数を補って考えている。
</p><p>状態が暗黙のうちに流れていく様子はだいぶわかったつもり。
</p>
<pre class="display">\begin{code}
module Main where

-- Lstはリストを表す型。
data Lst a = Nil | MakeLst a (Lst a) deriving (Show, Eq)

-- Stateは状態。数の累積に使う。
type State = Integer

-- Numberingは要素の数を数えるための型。
-- newtype Numbering aを使うと、Numberingはaと本質的に同じ型。
-- 汎用に作っているので、とりあえずLstとは無関係。
newtype Numbering a = Numbering (State -&gt; (a, State))

-- State -&gt; (a, State) という型は「状態を渡されると、値に状態をくっつけて返す関数」を表す型
-- そうか！「状態」は暗黙のうちにbindを経由して渡っていくんだな。

-- Numbering型をMonadのインスタンスにする。これで&gt;&gt;=やreturnが使える（使えるように定義する）
instance Monad Numbering where
    -- return xは、Numbering fという関数で表現しよう。
    -- fというのは、どういう関数か。f s は「xに対して、流れてくる状態sをくっつけて返す関数」である。
    return x = Numbering f
        where
            f s = (x, s)

    -- 「Numbering fの結果を元にnextfする」ということを「Numbering g」という関数で表現しよう。
    -- gというのはどういう関数か。g sは「h t」である。
    -- tとはどういう状態か。「f sしたときの結果の状態である」
    -- hとはどんな関数か。「f sしたときの値に対してnextfする関数」である。
    -- このあたりで道に迷ってしまった …(@_@)
    Numbering f &gt;&gt;= nextf = Numbering g
        where
            g s = h t
                where
                    (l, t) = f s
                    Numbering h = nextf l

number :: Lst a -&gt; Lst Integer
number l = fst (f 0)
    where
        Numbering f = numb l
            where
                numb :: Lst a -&gt; Numbering (Lst Integer)
                numb Nil = do
                    return Nil
                numb (MakeLst _ l) = do
                    n &lt;- nextNumber
                    nl &lt;- numb l
                    return (MakeLst n nl)
                        where
                            nextNumber :: Numbering Integer
                            nextNumber = Numbering f
                                where
                                    f n = (n+1, n+1)

s1 = MakeLst &quot;A&quot; (MakeLst &quot;B&quot; (MakeLst &quot;C&quot; Nil))
n1 = number s1

\end{code}
</pre>
<pre>Main&gt; s1
MakeLst &quot;A&quot; (MakeLst &quot;B&quot; (MakeLst &quot;C&quot; Nil))
Main&gt; n1
MakeLst 1 (MakeLst 2 (MakeLst 3 Nil))
</pre>


            ]]></content:encoded>
        </item>
    
        <item rdf:about="http://www.hyuki.com/haskell/200412#i20041229165834">
            <title>MaybeならぬPerhaps</title>
            <link>http://www.hyuki.com/haskell/200412#i20041229165834</link>
            <description>SOE 18.2.1 Other Instances of Monadをちらちらみながら、MaybeならぬPerhapsを作ってみる。Nothingの代わりに「最初にエラーが起きたときの文字列を保持するError String」を作ってみる。</description>
            <dc:date>2004-12-29T16:58:34+09:00</dc:date>
            
                <dc:subject>monad</dc:subject>
            
            <content:encoded><![CDATA[
                <p>SOE 18.2.1 Other Instances of Monadをちらちらみながら、
MaybeならぬPerhapsを作ってみる。
Nothingの代わりに「最初にエラーが起きたときの文字列を保持するError String」を作ってみる。
</p><p>doの表記を手で置換してみると、意味がよく分かる。
</p>
<pre class="display">\begin{code}
module Main where

data Perhaps a = Error String | Value a deriving (Show,Eq)

instance Monad Perhaps where
    Value x &gt;&gt;= k = k x
    Error s &gt;&gt;= k = Error s
    return x      = Value x
    fail s        = Error s

a1 = Value 123 &gt;&gt;= (\x -&gt;
     Value x   &gt;&gt;= (\y -&gt;
     Value y   &gt;&gt;= (\z -&gt;
     Value z       )))

t1 = do
        x &lt;- Value 123
        y &lt;- Value x
        z &lt;- Value y
        return z

a2 = Value 123        &gt;&gt;= (\x -&gt;
     Value x          &gt;&gt;= (\y -&gt;
     Error &quot;Too bad.&quot; &gt;&gt;= (\_ -&gt;
     Value y          &gt;&gt;= (\z -&gt;
     Value z          ))))

t2 = do
        x &lt;- Value 123
        y &lt;- Value x
        Error &quot;Too bad.&quot;
        z &lt;- Value y
        return z

a3 = Value 123        &gt;&gt;= (\x -&gt;
     Value x          &gt;&gt;= (\y -&gt;
     Error &quot;Too bad.&quot; &gt;&gt;= (\_ -&gt;
     Value y          &gt;&gt;= (\z -&gt;
     Error &quot;Invalid.&quot; &gt;&gt;= (\_ -&gt;
     Value z          )))))

t3 = do
        x &lt;- Value 123
        Error &quot;Too bad.&quot;
        y &lt;- Value x
        Error &quot;Invalid.&quot;
        z &lt;- Value y
        return z

\end{code}
</pre>
<pre>Main&gt; a1
Value 123
Main&gt; t1
Value 123
Main&gt; a2
Error &quot;Too bad.&quot;
Main&gt; t2
Error &quot;Too bad.&quot;
Main&gt; a3
Error &quot;Too bad.&quot;
Main&gt; t3
Error &quot;Too bad.&quot;
</pre>

            ]]></content:encoded>
        </item>
    
        <item rdf:about="http://www.hyuki.com/haskell/200412#i20041228215300">
            <title>「型クラス」と「型構築子クラス」</title>
            <link>http://www.hyuki.com/haskell/200412#i20041228215300</link>
            <description>nobsunから、またアドバイスをいただきました。いつもありがとうございます。ちょうど、以下でご説明いただいた部分を考えていたところなので、よい整理になりました。</description>
            <dc:date>2004-12-28T21:53:00+09:00</dc:date>
            
                <dc:subject>monad, nobsun</dc:subject>
            
            <content:encoded><![CDATA[
                <p>nobsunから、またアドバイスをいただきました。
いつもありがとうございます。
ちょうど、以下でご説明いただいた部分を考えていたところなので、
よい整理になりました。
</p>
<p><em>nobsunから</em>
</p>
<pre>結城さん

モナドの考察、たいへん興味深く拝見しています。

以下は、ひょっとして、ヒントになるかもしれないと思って書いています。
(モナドになると、だいぶんおよび腰になってしまうのは、私自身、
モナドの意味そのものをよく理解していわけでないからです。)

Haskell のクラスには細かく分類すると 2 種類あります。
「型クラス」と「型構築子クラス」です。

- Ord、Eq、Show などは「型クラス」
- Functor、Monad、MonadPlus などは「型構築子クラス」

「型クラス」は、そのインスタンスである型の値(これは first class)の性質を
規定するものですが、「型構築子クラス」は、そのインスタンスである型構築子
(これは first class ではありません)の性質を規定するものです。

data List a = Nil | Cons a (List a)

というデータ型を定義したとき、Nil、Cons はデータ構築子で、
List は型構築子です。たとえば、Eq のインスタンスであるという宣言は

instance Eq (List a) where
  Nil       == Nil       = True
  Cons x xs == Cons y ys = xs == ys
  _         == _         = False

のようなインスタンス宣言になります。これは、(List a)が Eq のインスタンス
ということですね。一方、Functor のインスタンスであるという宣言は

instance Functor List where
  fmap f Nil         = Nil
  fmap f (Cons x xs) = Cons (f x) (fmap f xs)

これは、型構築子 List が Functor クラスであるという宣言になります。
このように見ると、型構築子クラスは型クラスに比べて抽象度が高いと
いえますね。
</pre>


            ]]></content:encoded>
        </item>
    
        <item rdf:about="http://www.hyuki.com/haskell/200412#i20041228144015">
            <title>モナドで連番付け</title>
            <link>http://www.hyuki.com/haskell/200412#i20041228144015</link>
            <description>SOE 18.4 State Monadsをみながら、自作のListの要素に連番を付けるプログラムを書いてみる。いちおう動くが、まだ納得していない。</description>
            <dc:date>2004-12-28T14:40:15+09:00</dc:date>
            
                <dc:subject>monad, soe</dc:subject>
            
            <content:encoded><![CDATA[
                <p>SOE 18.4 State Monadsをみながら、
自作のListの要素に連番を付けるプログラムを書いてみる。
いちおう動くが、まだ納得していない。
</p>
<pre class="display">\begin{code}
module Main where

data Lst a = Val a | Lst a (Lst a) deriving (Show, Eq)
numbering :: Lst a -&gt; Lst Integer
numbering l = fst (numb l 0)
    where
        numb :: Lst a -&gt; Integer -&gt; (Lst Integer, Integer)
        numb (Val a) n =
            let
                n' = n + 1
                ls' = Val n'
            in
                (ls', n')

        numb (Lst a ls) n =
            let
                n' = n + 1
                (ls', n'') = numb ls n'
                ls'' = Lst n' ls'
            in
                (ls'', n'')

a0 = Val &quot;A&quot;
a1 = Lst &quot;A&quot; (Lst &quot;B&quot; (Lst &quot;C&quot; (Val &quot;D&quot;)))
b0 = numbering a0
b1 = numbering a1

newtype Numbering a = Numbering (Integer -&gt; (a, Integer))

instance Monad Numbering where
    return a
        = Numbering (\s -&gt; (a, s))
    Numbering ls0 &gt;&gt;= fls1
        = Numbering $ \s0 -&gt;
            let
                (a1, s1) = ls0 s0
                Numbering ls1 = fls1 a1
            in
                ls1 s1

mnumbering :: Lst a -&gt; Lst Integer
mnumbering t =
    let
        Numbering nt = mnumb t
        in fst (nt 0)

mnumb :: Lst a -&gt; Numbering (Lst Integer)
mnumb (Val a) = do
    n &lt;- getNumber
    return (Val n)
mnumb (Lst a ls) = do
    na &lt;- getNumber
    nls &lt;- mnumb ls
    return (Lst na nls)

getNumber :: Numbering Integer
getNumber = Numbering (\ n -&gt; (n+1, n+1))

c0 = mnumbering a0
c1 = mnumbering a1

\end{code}
</pre>
<pre>Main&gt; a0
Val &quot;A&quot;
Main&gt; a1
Lst &quot;A&quot; (Lst &quot;B&quot; (Lst &quot;C&quot; (Val &quot;D&quot;)))
Main&gt; b0
Val 1
Main&gt; b1
Lst 1 (Lst 2 (Lst 3 (Val 4)))
Main&gt; c0
Val 1
Main&gt; c1
Lst 1 (Lst 2 (Lst 3 (Val 4)))
</pre>


            ]]></content:encoded>
        </item>
    

</rdf:RDF>
<!-- cached -->