haskell - 我无法在 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,解释)失败,没有加载模块。
解决方案
中已经有一个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
推荐阅读
- ios - 在 SwiftUI 中使用分段式选择器在两个页面之间滑动
- asp.net-core - 创建 ASP.Net Core 3.1 身份用户和角色管理
- javascript - 执行文件太多次?
- php - 尝试连接到 AWS RDS 时连接被拒绝
- android - Change order of tabs in TabLayout
- javascript - XMLHTTPRequest onreadystatechange not working
- javascript - 继承没有绝对位置的填充宽度?是否可以?
- javascript - 为什么我收到“找不到模块 'jsonLogic'”错误?
- google-cloud-platform - GCP AI 平台训练不能使用全部 GPU 配额
- python - 行子集中的熊猫填充