Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Haskell 类型签名[…;]缺少附带的绑定_Haskell - Fatal编程技术网

Haskell 类型签名[…;]缺少附带的绑定

Haskell 类型签名[…;]缺少附带的绑定,haskell,Haskell,为什么在下面的程序中会出现“绑定”错误 wheels :: Int cars :: Int carpark wheels cars | wheels == odd = error wheels "is not even" | cars <= 0 = error cars "is an invalid number" | (2*cars) >= wheels = error "the number of wheels is

为什么在下面的程序中会出现“绑定”错误

wheels :: Int

cars :: Int

carpark wheels cars   
  | wheels   == odd      = error wheels "is not even"
  | cars     <= 0        = error cars "is an invalid number"
  | (2*cars) >= wheels   = error "the number of wheels is invalid"
  | wheels   >= (4*cars) = error "the number of wheels is invalid"
  | otherwise            = "There are " (wheels-(2 * cars)) `div` 2 "cars and " (cars - ((wheels - (2 * cars)) div 2)) "motorcycles on the parking lot"
wheels::Int
汽车::Int
停车场轮车
|轮子==奇数=错误轮子“不是偶数”
|cars=车轮=错误“车轮数量无效”
|车轮>=(4辆车)=错误“车轮数量无效”
|否则=“停车场上有”(车轮-(2辆车))`div`2“车和”(汽车-((车轮-(2辆车))div 2))“摩托车”
这就是错误:

aufgabe1.lhs:6:3: The type signature for ‘wheels’ lacks an accompanying binding aufgabe1.lhs:7:3: The type signature for ‘cars’ lacks an accompanying binding aufgabe1.lhs:6:3: “wheels”的类型签名缺少附带的绑定 aufgabe1.lhs:7:3: “cars”的类型签名缺少附带的绑定 我怎样才能摆脱它?

缺少哪些绑定? 您的程序有很多问题,但让我们先关注“绑定”问题。您可能已经习惯于使用Pascal或C,其中必须在参数处指定参数的类型:

string停车场(内部车轮、内部车辆);
然而,Haskell不是这样工作的。如果你写信

wheels :: Int
在文档中,您告诉编译器值
wheels
的类型为
Int
。编译器现在希望在某个地方有一个定义。缺少其绑定的此定义。
轮的类型已知,但不知道
应绑定到什么

如果你要补充

wheels = 1 * 2 + 12312
编译器不会再抱怨那个特定的绑定了

实际的问题是什么? 如上所述,您希望指定
停车场的参数类型,对吗?但是,这表明您指定了停车场的类型:

carpark :: Int -> Int -> String
carpark wheels cars 
   | -- omitted
这将消除“缺少绑定”错误

少了什么? 好的,在此之后,您仍然会有一个非编译软件,例如
error wheels“是非偶数”
是无效的。查看
错误
的类型:

error :: String -> a
由于
wheels
不是
字符串,因此无法编译。相反,您必须
显示
控制盘

error (show wheels ++ " is not even")
请注意,
error(show wheels)“不是偶数”
会很高兴地编译,但不会给出您实际查找的错误消息,因此要注意括号和字符串连接

练习
  • 编写一个函数
    whatNumber
    ,如果数字是奇数,则返回“isodd”;如果数字是偶数,则返回“Is偶数”,例如

    whatNumber 2 == "Is Even"
    
  • 编写一个函数
    whatNumberId
    ,如果数字是奇数,则返回
    “isodd”
    ,如果数字是偶数,则返回
    “is偶数”
    ,其中
    应该是数字,例如

    whatNumberId 123 == "123 is odd"
    

这两个练习都应该有助于你完成最初的任务。

泽塔的答案是完整的,但我只想提出另一点,不幸的是,这不适合作为评论

wheels :: Int
cars :: Int
carpark wheels cars...
Haskell中的代码尝试与C中的以下代码片段有些类似:

int wheels;
int cars;
char* carpark(wheels, cars) { ... }
类比在哪里?在这两种情况下,
车轮
(或
汽车
)的第一次和第二次出现都是指不同的对象。第一个是全局变量,第二个是函数的形式参数

停车场内
车轮
表示函数的形式参数;由于名称隐藏,全局
控制盘
不可用。请注意,在C99及更高版本中,此代码是非法的,因为未给出
carpark
的形式参数类型。全局
车轮
的类型为
int
这一事实并不意味着什么,因为
车轮
停车场中的
车轮
无关

这些标识符范围的一般概念也适用于Haskell。声明
wheels::Int
在其作用域(可能是模块作用域)声明一个值,
carpark wheels cars
在嵌套作用域中对另一个值使用相同的名称。您无法访问
停车场
车身中的外部
车轮
,因为它是隐藏的;如果您编写了
停车场w c..
,则您可以同时访问外部
车轮
和本地
w


最后,在上面的C代码片段中,缺少
wheels
cars
的类型是一个错误,而在Haskell中,我们通常将推导形式参数的类型留给编译器
carpark
本身是一个值,因此它有一个类型。如果我们将类型声明为
carpark::Int->Int->String
,编译器会推断,在以
carpark wheels cars…
开头的定义中,
wheels
这里不能是
Int

类型,而不应该为函数参数编写单独的类型签名。您只需要
carPark::Int->Int->String
,这是
carPark
的类型签名。小心-您需要“toString”方法
show
Int
转换为
String
,并使用连接运算符
++
组合这些String我很确定我见过一种语言,您可以先编写参数及其类型,然后编写实际函数。但我记不起它的名字了。