[英]Trouble mapping functions with the operator (/x)
So i have the result of a function that given a list of ints subtractes an int to all of the numbers in the list and then i want to divide the new list by x in this case 12. If i do the first paragraph of coding it gives me an error but if i do the second one it is possible. 所以我得到一个函数的结果,给出一个int的列表减去列表中所有数字的int,然后我想在这种情况下将新列表除以x 12.如果我做第一段编码它给我一个错误,但如果我做第二个,它是可能的。 How do i do this and why does it give me an error?
我该怎么做?为什么它会给我一个错误?
let xs = [23,32,1,3]
map (/12) xs
map(/12) [23,32,1,3]
potenciasPor12 xs = map (/12) xs
This is the error i'm getting 这是我得到的错误
<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
If the monomorphism restriction is set (it is off by default in newer GCHi, but on in compiled code), then xs
will default to [Int]
rather than the more general type Num a => [a]
which would work the the (/)
operator. 如果设置了单态限制(默认情况下它在较新的GCHi中是关闭的,但在编译的代码中是关闭的),那么
xs
将默认为[Int]
而不是更通用的类型Num a => [a]
这将起作用(/)
运算符。
(In GHCi 8.4.1, at least, it appears to default to Integer
instead of Int
.) (在GHCi 8.4.1中,至少,它似乎默认为
Integer
而不是Int
。)
% ghci
GHCi, version 8.4.1: http://www.haskell.org/ghc/ :? for help
Prelude> let xs = [1,2]
Prelude> :t xs
xs :: Num a => [a]
Prelude> :set -XMonomorphismRestriction
Prelude> let ys = [1,2]
Prelude> :t ys
ys :: [Integer]
Always provide explicit type signatures to be sure: 始终提供显式类型签名以确保:
% 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]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.