2014-03-01 69 views
0

這是我的代碼哈斯克爾錯誤解析錯誤(可能是不正確的縮進)

font a = let x= ord a in 
     if x>=0 || x<=31 || x>=126 then ["*****","*****","*****","*****","*****","*****","*****"] 
     else 
      auxfont (fontBitmap!!(x-32)) 
      where 
      auxfont b = let y = map trns (map rInt (map show b)) in 
         convertir y [] 
      trns z = modA [] 1 z 
      modA o l k 
        | l < 8 = modA (o++[(k `mod` 2)]) (l+1) (k `div` 2) 
        | otherwise o   
      convertir (e1:e2:e3:e4:e5) f 
        | e1==[] = f 
        | otherwise convertir [tail(e1),tail(e2),tail(e3),tail(e4),tail(e5)] (f++[(psr(head(e1)))++(psr(head(e2)))++(psr(head(e3)))++(psr(head(e4)))++(psr(head(e5)))]) 
      psr 0 = " " 
      psr 1 = "*" 

,我不得不和這個錯誤在convertir:

[1 of 2] Compiling Pixels   (Pixels.hs, interpreted) 

Pixels.hs:122:13: parse error (possibly incorrect indentation) 
Failed, modules loaded: none. 
+5

在'otherwise'之後需要'='。 – augustss

+1

兩人之後。 –

回答

2

爲什麼錯誤

每(正常)後衛的形式爲

| boolean expression = value 

您錯過了otherwise個案。它的工作原理是這樣,因爲otherwise被定義爲

otherwise = True 

,所以它不是像else關鍵字,它只是一個人類可讀的「永遠」,並且由於守衛試圖從高端到低端,這是一個包羅萬象 - 所有這些都不是真的。

一些修改

font a = let x= ord a in 
     if x>=0 || x<=31 || x>=126 then ["*****","*****","*****","*****","*****","*****","*****"] 
     else 
      auxfont (fontBitmap!!(x-32)) 
      where 
      auxfont b = let y = map trns (map rInt (map show b)) in 
         convertir y [] 
      trns z = modA [] 1 z 
      modA o l k 
        | l < 8 = modA (o++[(k `mod` 2)]) (l+1) (k `div` 2) 

這裏:

   | otherwise = o  -- added = 
     convertir (e1:e2:e3:e4:e5) f 
       | e1==[] = f 

這裏:

   | otherwise = convertir [tail(e1),tail(e2),tail(e3),tail(e4),tail(e5)] (f++[(psr(head(e1)))++(psr(head(e2)))++(psr(head(e3)))++(psr(head(e4)))++(psr(head(e5)))]) 
     psr 0 = " " 
     psr 1 = "*" 

一些縮寫

順便說一句,

  • ["*****","*****","*****","*****","*****","*****","*****"]replicate 7 "*****"
  • map trns (map rInt (map show b))map (trns.fInt.show) b
  • [tail(e1),tail(e2),tail(e3),tail(e4)]map tail [e1,e2,e3,e4,e5]
  • ,但我認爲你有一個類型的錯誤與:e5,因爲它是在模式列表列表(e1:e2:e3:e4:e5),但你已經用它的元素等tail(e5)
  • 另外[(psr(head(e1)))++(psr(head(e2)))++(psr(head(e3)))++(psr(head(e4)))++(psr(head(e5)))]map (psr.head) [e1,e2,e3,e4,e5]