我有以下数据结构来模拟Haskell中的替代版本编号方法:
data VersionNumber = VC (Maybe Int) -- VersionCompound: x (Nothing), 1 (Just 1), 2 (Just 2), 3 (Just 3), 4, 5, ...
| VN VersionNumber (Maybe Int) -- VersionNumber: x.x, x.0, x.1, x.2, ... , 1.0, 1.1, 1.2, ... 1.x.x, 2.x.x, 3.x.x, ...
deriving (Show)
versionCompoundToString :: (Maybe Int)-> String
versionCompoundToString (Just n) = (show n)
versionCompoundToString Nothing = "x"
versionNumberToString :: VersionNumber -> String
versionNumberToString (VN vn vc) = (versionNumberToString vn) ++ "." ++ (versionCompoundToString vc)
versionNumberToString (VC vc) = (versionCompoundToString vc)
鉴于上述定义,以下结构是合法的:
*VersionNumber> versionNumberToString (VN (VN (VC (Just 1)) Nothing) Nothing)
"1.x.x"
*VersionNumber> versionNumberToString (VN (VC (Just 2)) Nothing)
"2.x"
*VersionNumber> versionNumberToString (VN (VN (VN (VC Nothing) Nothing) (Just 3)) Nothing)
"x.x.3.x"
我正在尝试编写可以检测不同版本编号模式的函数 . 例如,以下函数 isReleaseBranch
应为 1.x
, x.2.x
, x.x.3.x
, x.x.x.x.5.x
等版本号返回 True
. 同样,以下值应返回相应的结果: x.x.4.x
- > True
, x.x
- > False
, 1.2.x
- > False
. 到目前为止我能想到的只是遵循模式匹配实现:
isReleaseBranch (VN (VC (Just _)) Nothing) = True
isReleaseBranch _ = False
我不知道如何模式匹配版本号左侧的任意 x 的嵌套模式: (VN (VN (VN (VC Nothing) Nothing) (Just 3)) Nothing)
( x.x.3.x
) (VN (VN (VN (VC Nothing) Nothing) (Just 3)) Nothing)
( x.x.3.x
)或 VN (VN (VC Nothing) Nothing) Nothing
( x.x.x
) VN (VN (VN (VN (VC Nothing) Nothing) Nothing) (Just 8)) Nothing
( x.x.x.8.x
)等 . 是否有在Haskell中实现这种嵌套模式匹配的任何方法?