使用一个可能只有3个以上的变量

时间:2012-05-24 16:46:16

标签: haskell syntax ghc

嗨,我自己无法弄清楚这一点,我也找不到在线的例子

我正在尝试使用一个可能或一个警卫,我发现的例子只有两个变量, 当我编辑或跟随两个以上的例子我得到一个错误继承人我正在尝试做什么

--maybe example
    Fun :: Double -> Double -> Double -> Maybe Double
    Fun a b c
        | a >= -1.0 || a <= 1.0 || b >= -1.0 || b <=1.0 || c /=0 || c >= -1.0 = Nothing
        | otherwise = Just max(  ((c^ (-c)) +   (a^(-c)-1.0)  ^   (a+ (-1.0/a)))     0.0)

错误与gchi

 The function `Just' is applied to two arguments,
    but its type `a0 -> Maybe a0' has only one

而hlint给出

No suggestions

尝试使用警卫而不是我得到一个不同的错误

--guard example
    Fun :: Double -> Double -> Double -> Maybe Double
    Fun a b c
        | a >= -1.0 || a <= 1.0 || b >= -1.0 || b <=1.0 || c /=0 || c >= -1.0 = error "Value out of range "
        | otherwise = max(  ((c^ (-c)) +   (a^(-c)-1.0)  ^   (a+ (-1.0/a)))     0.0)

和heres ghc和hlints错误

  test.hs:10:1:
        Invalid type signature: Fun :: Double
                                       -> Double -> Double -> Maybe Double
        Should be of form <variable> :: <type>
    $ hlint test.hs
 Error message:
  Left-hand side of type signature is not a variable: Fun
Code:
    Fun :: Double -> Double -> Double -> Maybe Double
  > Fun a b c

2 个答案:

答案 0 :(得分:7)

您必须使用小写字母编写函数。这意味着写下这个:

fun :: Double -> Double -> Double -> Maybe Double
fun a b c ...

函数永远不能以大写字母开头,因为大写标识符是为模块保留的(例如Data.List)和类型构造函数和数据构造函数(如IntMaybeJust)和其他一些事情。

Just构造函数的应用程序也存在问题。如果你这样写:

Just max(something)

......它意味着同样的事情:

Just max something

即。你给了Just两个论点。你需要通过调整你的parens来解决这个问题:

Just (max something)

答案 1 :(得分:3)

语法错误,主要是:

f :: Double -> Double -> Double -> Maybe Double
f a b c
    |  a >= -1
    || a <=  1
    || b >= -1
    || b <=  1
    || c /=  0
    || c >= -1
     = Nothing

    | otherwise
     = Just $ max ((c ** (-c)) + (a ** (-c)-1) ** (a+ (-1/a))) 0

请注意使用更通用的exponention运算符(**)$来避免Just包装器中max上的parens。