1

Queue.hs の一部:

module Queue ( Queue, emptyQueue, isEmptyQueue, enQueue, deQueue ) where

data Queue a = EmptyQueue | Single a | Q a ( Queue a ) a deriving (Show, Eq)

emptyQueue :: Queue a
emptyQueue = EmptyQueue

enQueue :: a -> Queue a -> Queue a
enQueue x EmptyQueue = Single x
enQueue x (Single q) = Q q (EmptyQueue) x
enQueue x (Q qf q qb) = Q qf (enQueue qb q) x

私は印刷物を使用していました。それらが1つずつ修正されているかどうかを確認してください。ここで、HUint を使用してそれらをテストしたいと思います。

Main.hs:

module Main where
import Queue 
import Test.HUnit

test1 = TestCase $ assertEqual "" (Single 'a'::Queue Char) $ enQueue 'a' emptyQueue
tests = TestList [TestLabel "test1" test1]

main = do runTestTT tests

これが私が得たものです:

*Queue> :r
[2 of 2] Compiling Main             ( Main.hs, interpreted )

Main.hs:5:36: Not in scope: data constructor ‘Single’
Failed, modules loaded: Queue.

どうすれば修正できますか?

4

1 に答える 1

1

データ型は抽象的で不透明であるように思われるQueueため、すべてのコンストラクターを公開することはお勧めできません。ただし、実装を 2 つのサブモジュールに分割できます。

module Queue.Internal where --expose all internal stuff

data Queue = <...>


module Queue (Queue, emptyQueue, <...>) where --expose user API only

import Queue.Internal


module Queue.Tests where

import Queue.Internal --to get access to „guts“

test = <...>


module Main where

import Queue --to just use the type

一部のライブラリ タイプ ( などByteString) は、この方法で作成されます。

于 2015-02-13T11:04:30.427 に答える