任何人都可以解释一下,为什么读取一个数字将其添加到另一个数字是有效的,尽管只读一个数字是无效的?
Prelude> read "5" + 3 8 Prelude> read "5" :33:1: No instance for (Read a0) arising from a use of `read' The type variable `a0' is ambiguous Possible fix: add a type signature that fixes these type variable(s) Note: there are several potential instances: instance Read () -- Defined in `GHC.Read' instance (Read a, Read b) => Read (a, b) -- Defined in `GHC.Read' instance (Read a, Read b, Read c) => Read (a, b, c) -- Defined in `GHC.Read' ...plus 25 others In the expression: read "5" In an equation for `it': it = read "5"
为什么“5”不明确?
答案 0 :(得分:7)
"5"
本身并不含糊,更多的是Haskell不知道你想要的类型read
。 read
是一个定义为:
read :: Read a => String -> a
您可以定义支持Read
类的多种类型。例如,Int
是Read
的实例,也是Float
的实例,您可以定义自己的类型Read
的实例。例如,您可以定义自己的类型:
data WeirdNumbers = Five | Twelve
然后定义instance Read WeirdNumbers
"5"
,Five
"5"
,Prelude> read "5" :: Int
5
Prelude> read "5" :: Float
5.0
,然后read "5" + 3
映射几种类型。
您可以通过告诉Haskell您想要阅读的类型来解决问题。像:
3
(+) :: Num a => a -> a -> a
之所以有用,是因为您提供的是+
和read
。所以Haskell的理由“Well 3是一个整数,而def tiles(arr, nrows, ncols):
"""
If arr is a 2D array, the returned list contains nrowsXncols numpy arrays
with each array preserving the "physical" layout of arr.
When the array shape (rows, cols) are not divisible by (nrows, ncols) then
some of the array dimensions can change according to numpy.array_split.
"""
rows, cols, channel = arr.shape
col_arr = np.array_split(range(cols), ncols)
row_arr = np.array_split(range(rows), nrows)
return [arr[r[0]: r[-1]+1, c[0]: c[-1]+1]
for r, c in product(row_arr, col_arr)]
def pre_process_images(data, dimensions=(28, 28)):
images = data['image']
features = []
count = 1
nrows = dimensions[0]
ncols = dimensions[1]
sift = cv2.xfeatures2d.SIFT_create(1)
for arr in images:
image_feature = []
cut_image = tiles(arr, nrows, ncols)
for small_image in cut_image:
(kps, descs) = sift.detectAndCompute(im, None)
image_feature.append(descs.flatten())
features.append(image_feature)
print count
count += 1
data['sift_features'] = features
return data
要求左右操作数是相同类型的,我知道我必须使用if (!isValidGridPos()) {
Debug.Log("GAME OVER");
Destroy(gameObject);
}
这样它才能读取整数。“