为什么我的测试文件不会导入我的数据结构?哈斯克尔

Mel*_*own 1 import haskell data-structures hunit

我目前正在为我的(非常简单的)二十一点游戏编写单元测试,我的测试文件(Tests.hs)似乎没有导入我在文件中声明的数据结构(我正在进行单元测试(HelpFunctions.hs)).我可以访问此文件中的函数/方法,但不能访问数据结构.有人可以帮我找到问题吗?

这是我的测试文件的顶部:

module Tests(performTests) where
import Test.HUnit
import HelpFunctions

cardList = [(Hearts, Ace)]
(...)
Run Code Online (Sandbox Code Playgroud)

这是我要编写测试文件的顶部

module HelpFunctions(Suit, Value, blackjack, cardDeck, shuffleOne, 
                     shuffleCards, getValue, addHand, dealCard, bust, 
                     getHighest
                    ) where

import System.Random
import Control.Monad(when)

{- Suit is one of the four suits or color of a playing card 
   ie Hearts, Clubs, Diamonds or Spades
   INVARIANT: Must be one of the specified.
 -}
data Suit = Hearts | Clubs | Diamonds | Spades deriving (Show)

{- Value is the numeric value of a playing card according to the rules of blackjack. 
   INVARIANT: Must be one of the specified.
 -}
data Value = Two | Three | Four | Five | Six | Seven | Eight | 
             Nine | Ten | Jack | Queen | King | Ace 
             deriving (Eq, Show)
(...)
Run Code Online (Sandbox Code Playgroud)

在编译测试文件时,我得到了错误

    Tests.hs:6:14: error: Data constructor not in scope: Hearts
  |
6 | cardList = [(Hearts, Ace)]   |              ^^^^^^

Tests.hs:6:22: error: Data constructor not in scope: Ace
  |
6 | cardList = [(Hearts, Ace)]   |                      ^^^
Run Code Online (Sandbox Code Playgroud)

我有另一个文件导入HelpFunctions和它的数据结构,并且没有问题.

mel*_*ene 5

你的问题在这里:

module HelpFunctions(Suit, Value, ...
Run Code Online (Sandbox Code Playgroud)

该行说HelpFunctions出口的种类SuitValue,但不是他们的数据构造(即类型是抽象的).

你要

module HelpFunctions(Suit(..), Value(..), ...
Run Code Online (Sandbox Code Playgroud)

您可以显式列出所有构造函数,但..速记符号表示"此类型的所有数据构造函数".


参考:Haskell 2010语言报告,5.2导出列表:

  1. 由声明或声明声明的代数数据类型T可以用以下三种方式之一命名:datanewtype

    • 表单T命名类型,但不命名构造函数或字段名称.在没有构造函数的情况下导出类型的能力允许构造抽象数据类型(参见第5.8节).
    • 形式T(c 1,...,c n),命名类型及其部分或全部构造函数和字段名称.
    • 缩写形式T(..)命名当前在范围内的类型及其所有构造函数和字段名称(无论是否合格).