首页 > 解决方案 > 我无法在 Haskell 中运行模块

问题描述

Module Transpose (transpose) where
import Data.List 
transpose :: [[a]] -> [[a]]
map sum $ transpose [[0,3,5,9],[10,0,0,9],[8,5,1,1]]

前奏> :reload

dosya.hs:32:30: 错误:输入“where”时解析错误 | 32 | 模块转置(transpose)^^^^^ [1 of 1] 编译主程序(dosya.hs,解释)失败,没有加载模块。

标签: haskell

解决方案


中已经有一个transpose函数Data.List,但您似乎不想使用它。相反,您正在尝试定义一个Transpose导出您自己的transpose函数的模块,您可以将其导入其他模块,对吗?

如果是这样,您的尝试有几个问题:

-- "module" must be lowercase
Module Transpose (transpose) where

-- this import may interfere with your own definition of `transpose`
import Data.List 

-- this type signature has no associated definition
transpose :: [[a]] -> [[a]]

-- you can't include this code in the middle of the module;
-- it needs to be part of a function definition
map sum $ transpose [[0,3,5,9],[10,0,0,9],[8,5,1,1]]

您可能想要的是一个Transpose.hs包含以下内容的文件:

-- contents of Transpose.hs
module Transpose (transpose) where

import Data.List hiding (transpose) -- don't use Data.List definition

transpose :: [[a]] -> [[a]]
transpose = error "to be implemented"

然后是另一个在函数Test.hs中导入和测试它的文件:main

-- contents of Test.hs
import Transpose

main = do
    print $ map sum $ transpose [[0,3,5,9],[10,0,0,0],[8,5,1,1]]

有了这两个文件,您应该能够从 GHCi加载Test和运行:main

λ> :l Test
[2 of 2] Compiling Main             ( Test.hs, interpreted ) [flags changed]
Ok, modules loaded: Main, Transpose (/scratch/buhr/stack/global-project/.stack-work/odir/Transpose.o).
Collecting type info for 1 module(s) ... 
λ> main
*** Exception: to be implemented
CallStack (from HasCallStack):
  error, called at ./Transpose.hs:7:13 in main:Transpose

或编译并运行它:

$ ghc Test.hs
[1 of 2] Compiling Transpose        ( Transpose.hs, Transpose.o )
[2 of 2] Compiling Main             ( Test.hs, Test.o )
Linking Test ...
$ ./Test
Test: to be implemented
CallStack (from HasCallStack):
  error, called at ./Transpose.hs:7:13 in main:Transpose

推荐阅读