2016-11-22 79 views
0

我想寫一個簡單的函數,它接受三個Int值並返回這三個中最小和最大整數之和。Haskell中的表達式中的語法錯誤(意外的'}')

我的代碼:

summinmax3 :: Int -> Int -> Int -> Int 
summinmax3 x y z = 
    if (x > y && z < y) 
    then (x + z) 
    else if (y > x && z < x) 
    then (y + x) 
    else if (z > x && y < X) 
    then (y + z) 

的代碼返回錯誤syntax error in expression (unexpected '}'), possibly due to bad layout

任何幫助,將不勝感激

+0

你使用哪種編譯器/解釋器?這就是說,每個'then'都需要一個'else'。另外,Haskell區分大小寫。 – Zeta

回答

3

你錯過else。每個if需要thenelse,否則將不會確定返回值,例如,如果x甚至不在這裏,會發生什麼?

add3IfEven x = if even x then x + 3 

然而,你的編譯器(擁抱)不使用你的實際代碼,而是將其轉換爲與花括號別的東西:

{if … then … else … } 

因爲你錯過了這最後else,將}是意外的。所以請務必添加正確的else的情況。順便說一句,你可以簡單地用maximum [x + y, x + z, y + z]解決這個練習。

+0

我認爲期望的結果是'最小[x,y,z] +最大[x,y,z]'。 – chepner