代码之家  ›  专栏  ›  技术社区  ›  jian

haskell将另一个函数用作输入参数

  •  -1
  • jian  · 技术社区  · 2 年前

    你好,收到了。hs

    waxOn = x
        where 
            z = 7
            y = z + 18
            k = y ^ 2
            x = k * 5
    
    triple  = a
        where 
            a = a * 3 
    

    然后加载。

    ghci> :load hi_copy.hs
    [1 of 1] Compiling Main             ( hi_copy.hs, interpreted )
    Ok, one module loaded.
    

    然后跑 triple waxOn

    <interactive>:122:1: error:
        • Couldn't match expected type ‘Integer -> t’
                      with actual type ‘Integer’
        • The function ‘triple’ is applied to one value argument,
            but its type ‘Integer’ has none
          In the expression: triple waxOn
          In an equation for ‘it’: it = triple waxOn
        • Relevant bindings include it :: t (bound at <interactive>:122:1)
    

    3 * waxOn 会有用的。 但现在我不知道该怎么做 三重瓦克森 工作 梅塔:到目前为止,哈斯克尔还没有学会打字。也许已经有其他好的答案了。但我不明白别人的好答案。

    1 回复  |  直到 2 年前
        1
  •  1
  •   willeM_ Van Onsem    2 年前

    你的 triple 不接受任何参数。它只返回一个值,这个值将是无限递归的结果,其中 a ((((…) * 3) * 3) * 3) .

    您应该将参数作为输入,因此:

    triple :: Num a => a -> a
    triple a = a * 3
    

    或更短:

    triple :: Num a => a -> a
    triple = (3 *)