2015-09-19 58 views
0

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 

我知道错误意味着什么,但我不知道为什么...

+0

从我可以看到你甚至没有使用'coordenadas',但似乎Haskell已经推断'num_col'应该是一个'Float'而不是'Integer'。尝试做'(num_lin,num_col)=(5,5)::(Integer,Integer)',看看它是否给你一个新的错误。 – bheklilr

+0

@bheklilr是的,现在当我尝试使用'num_col'时,它说我不能使用它,因为它期望'Float'并且它是一个Integer ... oo – PlayHardGoPro

+0

@PlayHardGoPro如果我假设'type Point =( Float,Float)'并且只设置'writeRects = undefined',因为你没有给它类型,这段代码编译得很好,没有我添加任何类型的签名。我会指出你的'main'函数中有一个轻微的缩进错误,'writeFile'函数应该缩进较少,所以它不属于'let'绑定。 – bheklilr

回答

3

编译器推断whFloat,因为writeRects说他们是。因此,width,num_colheight由于(w, h) = ((width * num_col), (height * num_col))而被推断为Float。这导致criaCoordenada(num_col, width, height)中的类型冲突,因为Floats被传递给criaCoordenada,其表示它期望Integers。

+0

omg e.e我该如何解决这个问题?我的意思是,编译器认为它是一个浮点数,因为我做了一个数学... WoW。我从计算中移除了'width'&'height'并写下了真实的数字值。错误仍然相同 – PlayHardGoPro

+1

你可以尝试'(w,h)=((width * num_col),(height * num_col))::(Integer,Integer)'和'writeFile“colors.svg”$ writeRects(fromInteger w )(来自Integer h)rects'?我的哈斯克尔有点生疏... – WhiteViking

相关问题