Haskell,再次.非法类型? [英] Haskell, again. Illegal types?

查看:119
本文介绍了Haskell,再次.非法类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将只发布代码和编译错误.

I'll just post the code and the compiling error.

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

推荐答案

我不知道这段代码应该达到什么目的,因此很难在正确的方向上进行纠正.在问题中,您应该已经说明了目标,以便提供一些背景信息.

I have no idea about what this code is supposed to achieve, so it's hard to correct it in the right direction. In the question you should have stated your goal so to provide some context.

我只会指出代码中的一些错误.

I'll just point out a few errors in the code as it is.

motion :: [Char] -> [Int, Int] -> [Int, Int]

[Int, Int]不是类型.如果您想要一个整数列表,只需使用[Int]即可;如果是一对正好两个整数,请使用(Int, Int).

[Int, Int] is not a type. If you want a list of integers, just use [Int]; if a pair of exactly two integers, use (Int, Int).

motion [] c ≃ c

应该是=. (是怎么结束的?)

The should be =. (How did end up there?)

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

首先,缩进很重要. else的缩进应大于motion ....

First, indentation matters. The elses should be indented more than motion ....

第二,您不能在纯函数中使用putStr,除非返回IO类型,否则不允许出现副作用.

Second, you can't putStr in a pure function -- no side effects are allowed unless you return an IO type.

第三个tail c + 1没有意义:列表的尾部是列表,而不是数字,因此您不能+1那样.

Third tail c + 1 makes no sense: the tail of a list is a list, and not a number, so you can't +1 that.

第四,我将使用error "instrução invalida"的错误情况.

Fourth, the the error case I'd use error "instrução invalida".

第五,有时您不带参数地递归调用motion.

Fifth, sometimes you recursively call motion without an argument.

此外,在

mov = do putStr motion

您无法打印函数:为此提供一些参数.

you can't print a function: provide some arguments for that.

这篇关于Haskell,再次.非法类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆