Haskell passing list as a argument doesn't work

时间:2018-04-18 18:12:46

标签: haskell

I'm trying to pass a list with ordonable elements to a function

bsearch :: (Ord a) => [a] -> a -> Int -> Int -> Int
bsearch xs nr l h = if (nr < (xs !! ((l + h) `quot` 2) ))
                      then 0
                      else 1

But I get the following problem for when I call bsearch([1,2,3,4], 3 , 0::Int , 4::Int) from GHCi

<interactive>:280:8: error:
    * Couldn't match expected type `[a]'
                  with actual type `([Integer], Integer, Int, Int)'
    * In the first argument of `bsearch', namely
        `([1, 2, 3, 4], 3, 0 :: Int, 4 :: Int)'
      In the expression: bsearch ([1, 2, 3, 4], 3, 0 :: Int, 4 :: Int)
      In an equation for `it':
          it = bsearch ([1, 2, 3, ....], 3, 0 :: Int, 4 :: Int)
    * Relevant bindings include
        it :: a -> Int -> Int -> Int (bound at <interactive>:280:1)

I'm losing my mind here I dont understand why I did wrong.

1 个答案:

答案 0 :(得分:2)

调用Haskell函数时,请不要使用通常的括号 - 逗号表示法:

-- Correct:
function arg1 arg2 arg3

-- Incorrect:
function(arg1, arg2, arg3)

不正确的表单被解释为将元组(一组值,如(1,5,True))作为函数的一个参数传递。