Haskell 又是哈斯克尔。非法类型?
我将发布代码和编译错误Haskell 又是哈斯克尔。非法类型?,haskell,types,Haskell,Types,我将发布代码和编译错误 module Mov where import Data.List import System.IO mov = do putStr motion motion :: [Char] -> [Int, Int] -> [Int, Int] motion [] c ≃ c motion (i : it) c = if i == 'L' || i == 'l' then putStr ((head c) +1, tail c) : motion it else
module Mov where
import Data.List
import System.IO
mov = do putStr motion
motion :: [Char] -> [Int, Int] -> [Int, Int]
motion [] c ≃ c
motion (i : it) c = if i == 'L' || i == 'l' then putStr ((head c) +1, tail c) : motion it
else if i == 'U' || i == 'u' then putStr (head c, ((tail c) + 1)) : motion
else if i == 'R' || i == 'r' then putStr (((head c) + 1), tail c) : motion
else if i == 'D' || i == 'd' then putStr ((head c), ((tail c) - 1)) : motion it
else "instrução invalida" putStr c
以及编译错误:
mov.hs:10:19:
Illegal type: ‘'[Int, Int]’ Perhaps you intended to use DataKinds
mov.hs:10:33:
Illegal type: ‘'[Int, Int]’ Perhaps you intended to use DataKinds
我不知道这段代码应该实现什么,所以很难在正确的方向上纠正它。在问题中,你应该说明你的目标,以便提供一些背景 我只想指出代码中的一些错误
motion :: [Char] -> [Int, Int] -> [Int, Int]
[Int,Int]
不是类型。如果需要整数列表,只需使用[Int]
;如果一对整数正好是两个整数,请使用(Int,Int)
≃代码>应为=
。(你是怎么做到的≃代码>在那里结束?)
首先,缩进很重要。else
s的缩进应大于motion…
其次,在纯函数中不能putStr
——除非返回IO
类型,否则不允许有任何副作用
第三个尾部c+1
毫无意义:列表的尾部是一个列表,而不是一个数字,因此不能+1
那样
第四,我将使用的错误案例是error“instrução invalida”
第五,有时候你递归地调用运动
,没有参数
而且,
mov = do putStr motion
你不能打印函数:提供一些参数。这段代码在很多方面都是错误的-请开始阅读一些基本的haskell语法-比如[Int,Int]
不存在-也许你的意思是(Int,Int)
也putStr
需要一个字符串,因为我尝试了一个元组(Int,Int)我也遇到了同样类型的编译错误。a)请保持礼貌,b)如果你在论坛上写一个有意义的问题——你的帖子中没有你已经研究或工作过的信息on@baitillus:你的代码(和态度)在很多方面都是错误的,我真的不知道从哪里开始。。。但是在任何情况下:它说[Int,Int]
是非法类型,这就是编译错误的全部原因-类型[Int,Int]
是无意义的;当然,你没有提供关于这段代码打算做什么的见解,也没有提供你可能认为这一错误意味着什么的见解,这将有助于了解你的想法和经验水平等,帮助他人帮助你。mov=putStr。运动
也有意义。@TheodoreLiefGannon不幸的是,运动
需要两个参数,所以它不起作用:-(啊,错过了。只提供了一个参数仍然有效:mov xs=putStr.motion xs
。当然总是有mov=(putStr.)。运动
但是……是的,ew.)所有这些嵌套的“如果”真的需要守卫。@user3237465,我忽略了一个事实,即替代方案是相同字母的不同情况。我会打碎箱子,用模式匹配。
motion (i : it) c = if i == 'L' || i == 'l' then putStr ((head c) +1, tail c) : motion it
else if i == 'U' || i == 'u' then putStr (head c, ((tail c) + 1)) : motion
else if i == 'R' || i == 'r' then putStr (((head c) + 1), tail c) : motion
else if i == 'D' || i == 'd' then putStr ((head c), ((tail c) - 1)) : motion it
else "instrução invalida" putStr c
mov = do putStr motion