论coordenadas = criaCoordenada(num_col, width, height)
我得到了错误的3个变量调用:无法比拟的预期类型
Couldn't match expected type `Integer' with actual type `Float' In the expression: num_col In the first argument of `criaCoordenada', namely `(num_col, width, height)'
type Point = (Float,Float)
type Rect = (Point,Float,Float)
writeRect :: (String,Rect) -> String
writeRect (style,((x,y),w,h)) =
printf "<rect x='%.3f' y='%.3f' width='%.2f' height='%.2f' style='%s' />\n" x y w h style
writeRects :: Float -> Float -> [(String,Rect)] -> String
writeRects w h rs =
printf "<svg width='%.3f' height='%.2f' xmlns='http://www.w3.org/2000/svg'>\n" w h
++ (concatMap writeRect rs) ++ "</svg>"
criaPosicoes :: (Integer, Integer) -> [Float]
criaPosicoes (num_col, x) = [fromIntegral x | x <-[0,x..(x*num_col)] ]
criaCoordenada :: (Integer, Integer, Integer) -> [Point]
criaCoordenada (col, w, h) =
let
lx = criaPosicoes (col, w)
ly = criaPosicoes (col, h)
in
[(x,y) | y <- ly, x <- lx]
main :: IO()
main = do
let
(num_lin, num_col) = (5, 5) -- Número de linha/coluna do retângulo maior.
(width, height) = (41,19) -- Width/Height do retângulo colorido.
(w, h) = ((width * num_col), (height * num_col)) -- Width/Height do SVG.
coordenadas = criaCoordenada(num_col, width, height) --Error here
style = "fill:hsl(20, 20%, 30%);stroke:rgb(156,156,139)"
rects = [ (style, (( 0, 0), width, height))
, (style, ((41, 0), width, height))
, (style, ((82, 0), width, height))
, (style, ((123, 0), width, height))
, (style, ((164, 0), width, height))
]
writeFile "colors.svg" $ writeRects w h rects
我知道错误意味着什么,但我不知道为什么...
从我可以看到你甚至没有使用'coordenadas',但似乎Haskell已经推断'num_col'应该是一个'Float'而不是'Integer'。尝试做'(num_lin,num_col)=(5,5)::(Integer,Integer)',看看它是否给你一个新的错误。 – bheklilr
@bheklilr是的,现在当我尝试使用'num_col'时,它说我不能使用它,因为它期望'Float'并且它是一个Integer ... oo – PlayHardGoPro
@PlayHardGoPro如果我假设'type Point =( Float,Float)'并且只设置'writeRects = undefined',因为你没有给它类型,这段代码编译得很好,没有我添加任何类型的签名。我会指出你的'main'函数中有一个轻微的缩进错误,'writeFile'函数应该缩进较少,所以它不属于'let'绑定。 – bheklilr