Haskellで乱数を使う

モナドを習得するためWinGHCiでHaskellを写経中だが、アクションのサンプルで使う乱数用のモジュールのインポートが失敗する。

Could not find module 'System.Random'

どうやらSystem.Randomモジュールが実装されていないようだ。
ググってみると、ghc7.2.1からSystem.Randomはバンドルされなくなっている、らしい。

というわけでWinGHCiでSystem.Randomパッケージをインストールしてみる。

まずはパッケージをここから入手しよう。
当方はrandom-1.1.tar.gzを取得した。
展開すると以下の通り。

haskell-system-random
SetupプログラムがHaskellのソースで梱包されている。
コンパイルしてみよう。

random-1.1>ghc Setup.hs
[1 of 1] Compiling Main             ( Setup.hs, Setup.o )
Linking Setup.exe ...

実行してみる。

random-1.1>Setup.exe
no command given (try --help)

怒られた。
「ヘルプを見ろ」と言っているので、見てみる。

random-1.1>Setup --help

This Setup program uses the Haskell Cabal Infrastructure.
See http://www.haskell.org/cabal/ for more information.

Usage: Setup [GLOBAL FLAGS] [COMMAND [FLAGS]]

Commands:
  configure     Prepare to build the package.
  build         Compile all/specific components.
  repl          Open an interpreter session for the given component.
  install       Copy the files into the install locations. Run register.
  copy          Copy the files into the install locations.
  haddock       Generate Haddock HTML documentation.
  clean         Clean up after a build.
  sdist         Generate a source distribution file (.tar.gz).
  hscolour      Generate HsColour colourised code, in HTML format.
  register      Register this package with the compiler.
  unregister    Unregister this package with the compiler.
  test          Run all/specific tests in the test suite.
  bench         Run all/specific benchmarks.
  help          Help about commands.

For more information about a command use
  Setup COMMAND --help

Typical steps for installing Cabal packages:
  Setup configure
  Setup build
  Setup install

Global flags:
 -h --help            Show this help text
 -V --version         Print version information
    --numeric-version Print just the version number

Cabal packagesをインストールするにはconfigure、build、installの順で実行して見ろと言っている。
やってみる。

random-1.1>Setup configure
Configuring random-1.1...

random-1.1>Setup build
Building random-1.1...
Preprocessing library random-1.1...
[1 of 1] Compiling System.Random    ( System\Random.hs, dist\build\System\Random.o )

System\Random.hs:43:1: Warning: Tab character
・
・(Warning多数)
・
System\Random.hs:528:1: Warning: Tab character
In-place registering random-1.1...

random-1.1>Setup install
Installing library in C:\Program
Files\Haskell\x86_64-windows-ghc-7.10.3\random-1.1-7Qv4dDT5WBF9U3tMWkytIb
Registering random-1.1...

完了。

winghci-system-random
System.Randomをインポートできるようになった。

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です