빠른 종류의 같은 <= and > 운영자 :함수 인수
-- 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]
내가 left
및 right
기능 거의 동일 참조하십시오. 그러므로 나는 ... 그것을 짧은를 다시 작성하는 뭐 그런 원하는 :
-- 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.
λ:
내가 오류 메시지를 읽을 수 있지만 내가 돈 그 이유를 아직 이해하지 못했습니다 ...
연산자 ('<=' and '>')와'f'를 역 인용 부호 ('\'')로 둘러싸면'qsort' 함수가 작동해야합니다. 역 인용 부호는 중위 연산자와 같은 이진 함수를 사용할 수 있도록합니다. – Jubobs
네, 괜찮습니다. 고마워요. 그런데 백틱을 사용해야하는 이유는 무엇입니까? '<='와'<'연산자는 삽입 연산자 (infix operators)이므로 중절 삽입 폼이이 경우에 효과가있을 것으로 예상했습니다. –
GHC는 "특수 문자"([this] (http://stackoverflow.com/questions/10548170/what-characters-are-permitted-for-haskell-operators) 참조)로 정의 된 함수 만 사용합니다. 태만. 예를 들어, GHCi에서'let (/./) = (+)'을 시도해보십시오. 오류없이 '1 /./ 5'와 같은 것을 쓸 수 있습니다. – Jubobs