不在范围内:数据构造函数 IsTriangle

发布于 2024-12-11 10:17:35 字数 466 浏览 1 评论 0原文

这是模块 - Number1.hs

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 时出现此错误

Here is the module - Number1.hs

module Number1(isTriangle) where

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

This is the main program 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)

This error I get when ghc --make Main1.hs

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

许你一世情深 2024-12-18 10:17:35

当您在 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

When you call isTriangle in Main1.hs, you call it with a capital 'I'.

Make sure your capitalisation matches as Haskell is case sensitive, and make sure functions start with a lower case character as this is mandatory.

Edit - rounding up other errors

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文