< =和>运算符作为函数参数

时间:2015-01-05 11:54:42

标签: haskell

快速排序:

-- First variant:
qsort :: (Ord a) => [a] -> [a]
qsort [] = []
qsort (x:xs) = left x ++ [x] ++ right x 
  where left  n = qsort [m | m <- xs, m <= n]
        right n = qsort [m | m <- xs, m  > n]

-- λ: qsort [10,2,5,3,1,6,7,4,2,3,4,8,9]
-- [1,2,2,3,3,4,4,5,6,7,8,9,10]

我看到leftright函数几乎相同。所以我想把它改写得更短......就像那样:

-- Second variant:
qsort' :: (Ord a) => [a] -> [a]
qsort' [] = []
qsort' (x:xs) = (srt <=) ++ [x] ++ (srt >)
  where srt f = qsort' [m | m <- xs, m f x]

但是当我尝试将其加载到ghci

时,我收到错误
λ: :load temp
[1 of 1] Compiling Main             ( temp.hs, interpreted )

temp.hs:34:18:
    Couldn't match expected type `[a]'
                with actual type `(t0 -> [a]) -> Bool'
    Relevant bindings include
      srt :: forall t. t -> [a] (bound at temp.hs:35:9)
      xs :: [a] (bound at temp.hs:34:11)
      x :: a (bound at temp.hs:34:9)
      qsort' :: [a] -> [a] (bound at temp.hs:33:1)
    In the first argument of `(++)', namely `(srt <=)'
    In the expression: (srt <=) ++ [x] ++ (srt >)
    In an equation for qsort':
        qsort' (x : xs)
          = (srt <=) ++ [x] ++ (srt >)
          where
              srt f = qsort' [m | m <- xs, m f x]

temp.hs:34:37:
    Couldn't match expected type `[a]'
                with actual type `(t1 -> [a]) -> Bool'
    Relevant bindings include
      srt :: forall t. t -> [a] (bound at temp.hs:35:9)
      xs :: [a] (bound at temp.hs:34:11)
      x :: a (bound at temp.hs:34:9)
      qsort' :: [a] -> [a] (bound at temp.hs:33:1)
    In the second argument of `(++)', namely `(srt >)'
    In the second argument of `(++)', namely `[x] ++ (srt >)'
    In the expression: (srt <=) ++ [x] ++ (srt >)

temp.hs:35:38:
    Could not deduce (a ~ (t -> a -> Bool))
    from the context (Ord a)
      bound by the type signature for qsort' :: Ord a => [a] -> [a]
      at temp.hs:32:11-31
      `a' is a rigid type variable bound by
          the type signature for qsort' :: Ord a => [a] -> [a]
          at temp.hs:32:11
    Relevant bindings include
      m :: a (bound at temp.hs:35:29)
      f :: t (bound at temp.hs:35:13)
      srt :: t -> [a] (bound at temp.hs:35:9)
      xs :: [a] (bound at temp.hs:34:11)
      x :: a (bound at temp.hs:34:9)
      qsort' :: [a] -> [a] (bound at temp.hs:33:1)
    The function `m' is applied to two arguments,
    but its type `a' has none
    In the expression: m f x
    In a stmt of a list comprehension: m f x
Failed, modules loaded: none.
λ:

我看了错误信息,但我还不明白原因......

1 个答案:

答案 0 :(得分:9)

您不应该使用f作为中缀。您可以通过将f放在前面并表示括号(<=)

之间的函数来解决此问题
-- third variant:
qsort' :: (Ord a) => [a] -> [a]
qsort' [] = []
qsort' (x:xs) = (srt (<=)) ++ [x] ++ (srt (>))
  where srt f = qsort' [m | m <- xs, f m x]

这主要是因为您基本上想要做的是在fm致电 x。现在,默认的lambda-calculus始终首先计算左侧列出的函数。

Haskell只为操作符提供一些语法糖:当你写a+b时,你基本上写的是(+) a b(幕后)。这就是Haskell最喜欢的,但编译器因此为程序员提供了一些方便的功能。由于编写a*b+c*d比编写(+) ((*) a b) ((*) c d)更容易,但第二个实际上是如何在lambda-calculus中编写这样的东西。

为了将运算符视为函数,您可以在括号之间编写它们,因此要获得<=的函数变量,请编写(<=)

修改

正如@Jubobs所说,你也可以使用中缀,但因此需要你使用反引号:

-- fourth variant:
qsort' :: (Ord a) => [a] -> [a]
qsort' [] = []
qsort' (x:xs) = (srt (<=)) ++ [x] ++ (srt (>))
  where srt f = qsort' [m | m <- xs, m `f` x]

问题主要在于您需要通过f <{> 1}}和<=函数来传递,{{1} }和>是。从技术上讲,这个故事有点复杂,但我想在学习基础知识时就足够了。

通过使用反引号,Haskell读取:

(<=)

为:

(>)

(请注意,这并非完全正确,因为运营商还有优先级x `f` y f x y 更紧密,但这些更多的是&#34;详情&# 34;过程)。

将括号放在运算符上会产生相反的效果:

*

+

x o y 运营商。