Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/8.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
Function 使用运算符(/x)映射函数时出现故障_Function_Haskell_Mapping - Fatal编程技术网

Function 使用运算符(/x)映射函数时出现故障

Function 使用运算符(/x)映射函数时出现故障,function,haskell,mapping,Function,Haskell,Mapping,我有一个函数的结果,给定一个int列表,减去列表中所有数字的一个int,然后我想把新列表除以x,在这里是12。如果我做了第一段编码,它会给我一个错误,但如果我做了第二段,它是可能的。我怎么做,为什么它会给我一个错误 let xs = [23,32,1,3] map (/12) xs map(/12) [23,32,1,3] potenciasPor12 xs = map (/12) xs 这就是我得到的错误 <interactive>:176:1: No instance fo

我有一个函数的结果,给定一个int列表,减去列表中所有数字的一个int,然后我想把新列表除以x,在这里是12。如果我做了第一段编码,它会给我一个错误,但如果我做了第二段,它是可能的。我怎么做,为什么它会给我一个错误

let xs = [23,32,1,3]
map (/12) xs

map(/12) [23,32,1,3]

potenciasPor12 xs = map (/12) xs
这就是我得到的错误

<interactive>:176:1:
No instance for (Fractional Int)
  arising from a use of ‘potenciasPor12’
In the expression: potenciasPor12 xs
In an equation for ‘it’: it = potenciasPor12 xs
:176:1:
没有(分数整数)的实例
因使用“PotentiaSpor12”而产生
在表达式中:PotentiAspor12 xs
在“it”的方程式中:it=PotentialsPor12 xs

如果设置了单态限制(在较新的GCHi中默认为关闭,但在编译代码中为打开),则
xs
将默认为
[Int]
,而不是使用
(/)
运算符的更一般的类型
Num a=>[a]

(至少在GHCi 8.4.1中,它似乎默认为
Integer
,而不是
Int

始终提供显式类型签名,以确保:

% ghci -XMonomorphismRestriction
GHCi, version 8.4.1: http://www.haskell.org/ghc/  :? for help
Prelude> let xs = [23,32,1,3] :: Num a => [a]
Prelude> :t xs
xs :: Num a => [a]
Prelude> map (/12) xs
[1.9166666666666667,2.6666666666666665,8.333333333333333e-2,0.25]

你好,芮-欢迎!一个好主意是还包括您得到的错误消息。如果您指定
xs
[Int]
,这确实是不可能的,因为
(/)
是在
分数
上定义的。基本上我有
效价aspor12(funcao(xs))
其中
funcao
基本上是
映射(3-)xs
我现在有一个错误:
:176:1:在表达式中使用'PotentialsPor12 xs'在'it'的等式中没有(分数Int)的实例:it=PotentialsPor12 xs
听起来像是单态限制:
xs
默认为
[Int]
编译器不是抱怨提供给(/12)的更一般的
Num a=>[a]
类型参数,如果您在ghci中键入
:t map(/12)xs
:t(/)
,它应该告诉您的列表是问题所在。在这种情况下,由于您试图在Int列表上映射
(/12)
,如果您没有指定列表的类型,那么编译器的推理不知何故会将列表从
Num a=>[a]
默认为
[Int]
,在这种情况下,我只需指定创建列表时列表包含的类型,基本上
让xs=[1,2,3,4…50]::(Num a,Enum a)=>[a]
% ghci -XMonomorphismRestriction
GHCi, version 8.4.1: http://www.haskell.org/ghc/  :? for help
Prelude> let xs = [23,32,1,3] :: Num a => [a]
Prelude> :t xs
xs :: Num a => [a]
Prelude> map (/12) xs
[1.9166666666666667,2.6666666666666665,8.333333333333333e-2,0.25]