24 June, 2006

[Haskell] Exercise 2.1

Haskell: The Craft of Functional Programmingのサイトから、教科書用のコードのダウンロードページへ行き、Pictures.hs をダウンロードした。

% ghci
___ ___ _
/ _ \ /\ /\/ __(_)
/ /_\// /_/ / / | | GHC Interactive, version 6.4, for Haskell 98.
/ /_\\/ __ / /___| | http://www.haskell.org/ghc/
\____/\/ /_/\____/|_| Type :? for help.

Loading package base-1.0 ... linking ... done.
Prelude> :load Pictures
Compiling Pictures ( Pictures.hs, interpreted )
Ok, modules loaded: Pictures.
*Pictures> printPicture horse
.......##...
.....##..#..
...##.....#.
..#.......#.
..#...#...#.
..#...###.#.
.#....#..##.
..#...#.....
...#...#....
....#..#....
.....#.#....
......##....
*Pictures>

これをimportするようなUsePictures.hsを書く。

% cat UsePictures.hs
--
-- UsePictures.hs
--
module UsePictures where
import Pictures

-- see p.8 and p.9
blackHorse :: Picture
blackHorse = invertColour horse

rotateHorse :: Picture
rotateHorse = flipH (flipV horse)

% ghci
___ ___ _
/ _ \ /\ /\/ __(_)
/ /_\// /_/ / / | | GHC Interactive, version 6.4, for Haskell 98.
/ /_\\/ __ / /___| | http://www.haskell.org/ghc/
\____/\/ /_/\____/|_| Type :? for help.

Loading package base-1.0 ... linking ... done.
Prelude> :load UsePictures
Compiling Pictures ( ./Pictures.hs, interpreted )
Compiling UsePictures ( UsePictures.hs, interpreted )
Ok, modules loaded: UsePictures, Pictures.
*UsePictures> printPicture blackHorse
#######..###
#####..##.##
###..#####.#
##.#######.#
##.###.###.#
##.###...#.#
#.####.##..#
##.###.#####
###.###.####
####.##.####
#####.#.####
######..####
*UsePictures> printPicture rotateHorse
....##......
....#.#.....
....#..#....
....#...#...
.....#...#..
.##..#....#.
.#.###...#..
.#...#...#..
.#.......#..
.#.....##...
..#..##.....
...##.......
*UsePictures>

No comments: