0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

IFPHの12章のプログラムの動かし方

Posted at

IFPHのサポートページに公開されている、12章のプログラムをHaskell Platform (ghc 7.6.3 , OS: Windows)でそのままでは動かなかったので、修正したところを公開します。

動かし方

テキスト中の例は、Example.lhsに記述されているので、それをロードして実行するのが手っ取り早いと思います。自分で、独自の計算をしたいときも、Example.lhsに習って書けば良いと思います。

ghci> :l Examples
ghci> ex6

修正箇所

MonadZeroが記述されていますが、すでに廃止されているようで、うまく動きません。MonadZeroのzeroplusを合わせたのが、Control.Monadで定義されているMonadPlusに当てはめれそうなので、そのように変更します。

MyParseLib.lhs
> {- plusをmplusに変更
> module MyParseLib
>    (Parser, plus, orelse, item, many, some, manywith, somewith,
>     sat, char, string, digit, lower, upper, letter, alphanum,
>     ident, space, token, symbol, applyParser) where
> -}
> module MyParseLib
>    (Parser, mplus, orelse, item, many, some, manywith, somewith,
>     sat, char, string, digit, lower, upper, letter, alphanum,
>     ident, space, token, symbol, applyParser) where
> import Control.Monad

> {-
> instance MonadZero Parser where
>   zero = MkP f
>          where f s = []
>  -}
> {-
> plus :: Parser a -> Parser a -> Parser a
> p `plus` q = MkP f
>              where f s = papply p s ++ papply q s
> -}

> instance MonadPlus Parser where
>    mzero                      =  MkP (\inp -> [])
>    p `mplus` q = MkP f
>              where f s = papply p s ++ papply q s
>

後は、Functorのメソッドがmapになっているのをfmapに、isAlphanumisAlphaNumにします。それから、isAlphaNumData.Charに定義されているので、それもインポートすれば、MyParseLib.lhsのコンパイルは通るようになります。

MyParseLib.lhs
> import Data.Char

> instance Functor Parser where
>   -- map f p = MkP g
>   fmap f p = MkP g
>             where g s = [(f x, s') | (x,s') <- papply p s]

> alphanum :: Parser Char
> -- alphanum = sat isAlphaum
> alphanum = sat isAlphaNum

後、コンパイルが通らなかったところはUtilities.lhspartitionsです。以下のように変更すると、コンパイルが通るようになります。

Utilities.lhs
> partitions :: Int -> [a] -> [[[a]]]
> partitions 0 []     = [[]]
> partitions n []     = []
> partitions 0 _      = []
> partitions n (x:xs) = [[x]:yss | yss <- partitions (n-1) xs] ++
>      [(x:ys):yss | ys:yss <- partitions n xs]

修正したファイルはここに置いておきます。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?