Chapter 02

プログラムの概要

Nobuo Yamashita
Nobuo Yamashita
2021.12.01に更新
このチャプターの目次

Toy.toy (toy01)

toy はソースコードを対話的プログラムに変換する関数とします。
toy :: SourceCode -> Interactive

toy :: SourceCode -> Interactive
toy prog = undefined

対話的プログラムは、コンソールからの入力列 [Input] からコンソールへの出力列 [Output] への変換関数です。

type SourceCode  = String
type Interactive = [Input] -> [Output]
type Input  = String
type Output = String

対話プログラムの起動はプレリュード関数 interact :: (String -> String) -> IO () で行います。そこで、対話プログラム InteractiveString -> String に変換する関数 drive :: Interactive -> (String -> String) も用意しておきます。

drive :: Interactive -> (String -> String)
drive f = unlines . f . lines

その日のプログラミングは、プログラムが、stack build が通る状態でおえるようにしておくとことにします。すこし冗長ですが、ページの最後にモジュールファイル全体を再掲するようにします。

コード

ここまでの`Toy`モジュール全体
src/Toy.hs
module Toy where

type SourceCode  = String
type Interactive = [Input] -> [Output]
type Input  = String
type Output = String

drive :: Interactive -> (String -> String)
drive f = unlines . f . lines

toy :: SourceCode -> Interactive
toy prog = undefined