在Haskell中如何检查字符串是否小于另一个字符串?

时间:2009-05-04 10:38:42

标签: haskell string compare

我有两个字符串作为Haskell函数的参数。

如果s1短于s2,或者s1长度相同且s2的字典小于{{1},则{p> s1小于s2 }}

如何在Haskell中实现它?

6 个答案:

答案 0 :(得分:9)

我会使用以下内容:

smaller :: String -> String -> Bool
smaller s1 s2 | len1 /= len2         = (len1 < len2)
              | otherwise            = (s1 < s2)
              where (len1, len2) = (length s1, length s2)

以下是Hugs中的示例运行:

Main> smaller "b" "aa"
True
Main> smaller "aa" "b"
False
Main> smaller "this" "that"
False
Main> smaller "that" "this"
True

答案 1 :(得分:7)

一次性解决方案:

lengthcompare :: Ord a => [a] -> [a] -> Ordering
lengthcompare = lc EQ
 where
  lc lx [] [] = lx
  lc _ [] _ = LT
  lc _ _ [] = GT
  lc EQ (v:vs) (w:ws) = lc (compare v w) vs ws
  lc lx (_:vs) (_:ws) = lc lx vs ws

smaller :: Ord a => [a] -> [a] -> Bool
smaller s1 s2 = lengthcompare s1 s2 == LT

答案 2 :(得分:5)

试试这个:

compare s1 s2

(返回LT,EQ或GT)。

答案 3 :(得分:4)

Tom Lokhorst上面的mappend版本的缩短版本:

import Data.Monoid (mappend)
import Data.Ord (comparing)

compareStrings :: String -> String -> Ordering
compareStrings = comparing length `mappend` comparing id

另一种方式,利用元组的排序:

import Data.Ord (comparing)
import Control.Arrow ((&&&))

compareStrings :: String -> String -> Ordering
compareStrings = comparing (length &&& id)

答案 4 :(得分:2)

StringOrd的一个实例,因此您可以使用所有这些方法按字典顺序比较字符串。正如安德鲁所说,这基本上是compare,但也包括比较运算符,(<)等。

smaller :: Ord a => a -> a -> Bool
smaller a b = a < b

这适用于实现Ord所有类型(实际上只是(<)的粗略包装),包括String

答案 5 :(得分:2)

普通字符串比较仅适用于词典排序,而不适用于字符串的长度。

所以你必须编写自己的函数来检查长度:

smaller :: String -> String -> Bool
smaller s1 s2 | length s1 < length s2 = True
              | length s1 > length s2 = False 
              | otherwise             = s1 < s2

或者更一般:

compareStrings :: String -> String -> Ordering
compareStrings s1 s2 | length s1 < length s2 = LT
                     | length s1 > length s2 = GT
                     | otherwise             = compare s1 s2

示例:

ghci> compare "ab" "z"
LT
ghci> compareStrings "ab" "z"
GT

我们上周在大学里玩弄了Monoids,我们想出了这个可爱的替代Ord实例:

instance Ord a => Ord [a] where
  compare = comparing length
              `mappend` comparing head `mappend` comparing tail

但如果你不太明白这一点,我建议你坚持第一个定义; - )