在写入最大功能时输入错误 [英] Type error while writing max function

查看:84
本文介绍了在写入最大功能时输入错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  max':: Int  - > Int  - > Int 
max'ab = if a> = b then a else b

这个函数是正确的,但如果我写的是

  let a = 3,
let b = 3

以及如果我写了

  ghci的> a == b =>真正的

所以它比较它们,为什么它不能在我的函数中比较

  ghci> max'a b 

错误是为什么?或者什么是写它的正确方法?



对不起,我是初学者,如果我的问题很愚蠢的原谅我,并编辑它,如果有需要,谢谢

 < interactive>:19:6:
实际类型为'Integer'的预期类型'Int' '
在max'的第一个参数中,即'a'
在表达式中:max'ab
在'it'的等式中:it = max'ab

< interactive>:19:8:
无法与实际类型为'Integer'的预期类型'Int'匹配
在max'的第二个参数中,即'b'
在表达式中:max'ab
在'it'的等式中:it = max'ab


解决方案

我想你在 ghci 解释器中这样做。然后,看一下(:t )显示表达式的类型和 a :: t 表示 a 有类型 t ):

 前奏>让a = 3 
Prelude> :ta
a :: Integer

ghci 解释器尽早提交并给出 a 类型 Integer ,但它应该给出任何数字类型(因此<$ c

现在,你的函数接收 Int code> s作为参数,但是既然 a b 整数

您可以删除限制类型签名,也可以定义 a b Int s。除非有一些要求使用 Int - 仅限类型签名,否则我会选择第一个选项。为此,您需要在定义的末尾添加 :: Int >

 前奏>让b = 42 :: Int 
Prelude> :tb
b :: Int

如果您想删除签名重新编码,只有一行:

  max'ab = if a> = b then a else b 

$ b现在,如果你要检查它的类型:

 

前奏> :t max'
max':: Ord a => a - > a - > a

这意味着你有一个通用函数,它可以用于任何可以订购的类型。 / p>

另一个选择是使用扩展名开始 ghci ghci -XNoMonomorphismRestriction 。在这种情况下:

  Prelude>让a = 3 
Prelude> :t a
a :: Num a =>一个

可以直接在你的函数上工作。



没有这个扩展的 ghci 提交给整数的原因是单态限制


max' :: Int -> Int -> Int
max' a b = if a >= b then a else b 

you see that the function is correct but if i write

let a = 3,
let b = 3

and also if i write

ghci> a == b => True

so it compares them then why it doesn't compare in my function

ghci> max' a b 

error occurs why? or what is the right way to write it?

Sorry I am beginner if my question is silly forgive me for that and edit it if there is a need for that Thanks

<interactive>:19:6:
    Couldn't match expected type `Int' with actual type `Integer'
    In the first argument of max', namely `a'
    In the expression: max' a b
    In an equation for `it': it = max' a b

<interactive>:19:8:
    Couldn't match expected type `Int' with actual type `Integer'
    In the second argument of max', namely `b'
    In the expression: max' a b
    In an equation for `it': it = max' a b

解决方案

I guess you are doing this in the ghci interpreter. Then, have a look at (:t displays the type of an expression and a line of the form a :: t means a has type t):

Prelude> let a = 3
Prelude> :t a
a :: Integer

The ghci interpreter commits early and gives a the type Integer though it should give any numeric type (thus a :: Num t => t).

Now, your function receives Ints as arguments but since a and b are Integers you get that error message.

You can either remove the restrictive type signature or you can define a and b to be Ints. I'd go with the first option, unless there is some requirement to go with Int-only type signature. To do so you need to add ::Int at the end of the definition:

Prelude> let b = 42 :: Int
Prelude> :t b
b :: Int

If you want to remove the signature recode your function to have only one line:

max' a b = if a >= b then a else b 

Now, if you're to inspect its type:

Prelude> :t max'
max' :: Ord a => a -> a -> a

Which means you've got a generic function which works for any type which can be ordered.

An alternative is to start ghci using an extension: ghci -XNoMonomorphismRestriction. In this case:

Prelude> let a = 3
Prelude> :t a
a :: Num a => a

which will work directly on your function.

The reason why ghci without this extension commits to Integer is the Monomorphism restriction

这篇关于在写入最大功能时输入错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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