2011-10-21 69 views
1

這裏是模塊 - Number1.hs不在範圍:數據構造IsTriangle

module Number1(isTriangle) where 

isTriangle x y z = if x*x+y*y >= z*z then True 
       else False 

這是主程序Main1.hs

import System.Environment 
import Number1 

main = do 
    args<-getArgs 
    let a = args !! 0 
    let b = args !! 1 
    let c = args !! 2 
    if (IsTriangle a b c) then return(True) 
    else return(False) 

這個錯誤,我得到的時候ghc --make Main1.hs

+1

附: '如果出現錯誤,則返回True'否則False'與'something'相同 – user102008

回答

3

當你在Main1.hs中調用isTriangle時,你用大寫'I'來調用它。

確保您的大小寫與Haskell匹配區分大小寫,並確保函數以小寫字符開頭,因爲這是強制性的。

編輯 - 圍捕其他錯誤

Main1.hs:

import System.Environment 
import Number1 

main :: IO() 
main = do 
     args<-getArgs 
     {- Ideally you should check that there are at least 3 arguments 
     before trying to read them, but that wasn't part of your 
     question. -} 
     let a = read (args !! 0) -- read converts [Char] to a number 
     let b = read (args !! 1) 
     let c = read (args !! 2) 
     if (isTriangle a b c) then putStrLn "True" 
      else putStrLn "False" 

Number1.hs:

module Number1(isTriangle) where 

{- It's always best to specify the type of a function so that both you and 
    the compiler understand what you're trying to achieve. It'll help you 
    no end. -} 
isTriangle  :: Int -> Int -> Int -> Bool 
isTriangle x y z = if x*x+y*y >= z*z then True 
         else False 
+0

現在我有一個錯誤 - '沒有實例'。 –

+0

你傳入的變量不是數字,它們是字符列表。我將粘貼正確的代碼到我原來的答案中。 –

+0

謝謝。但它什麼都沒有返回。對於考試'> Main1 3 4 5' - 沒有結果 –

相關問題