haskell - 我可以将字符串切成头尾后用作整个字符串吗(:as) and using recursion on it in Haskell?

标签 haskell

我试图在我拥有的函数中使用字符串中的每个字符(仅使用一个字符),但我也尝试在同一个递归函数中使用相同的字符串作为一个整体,以将其与中的单个字符进行比较另一个字符串(使用elem)。有没有办法可以使用该字符串的头和尾以及整个字符串,以便每次递归后字符串都不会被切断?

代码:

checkTrue :: TrueChar -> Char -> [Char] -> TruthValue
checkTrue a b c
            | a == IsTrue b                             = AbsoluteTrue
            | (a == IsFalse b) && (b `elem` c)          = PartialTrue
            | otherwise                                 = NoneTrue

checkTruths :: [TrueChar] -> [Char] -> [TruthValue]
checkTruths [][] = []
checkTruths (a:as) (b:bs) = checkTrue a b (removeAbsoluteTrue (a:as) (b:bs)): checkTruths as bs 
{-  This is the line, 
i wanted to use b as a string and also as b:bs. is this possible? -}
checkTruths _ _ = [NoneTrue]

最佳答案

您想要一个 as 模式,如 documented在 Haskell 2010 报告的第 3.17.1 节中。

Patterns of the form var@pat are called as-patterns, and allow one to use var as a name for the value being matched by pat. For example,

case e of { xs@(x:rest) -> if x==0 then rest else xs }

is equivalent to:

let { xs = e } in
  case xs of { (x:rest) -> if x==0 then rest else xs }

在你的函数中,你会写

checkTruths alla@(a:as) allb@(b:bs) = checkTrue a b (removeAbsoluteTrue alla allb): checkTruths as bs

关于haskell - 我可以将字符串切成头尾后用作整个字符串吗(:as) and using recursion on it in Haskell?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70998734/

相关文章:

haskell - 如何阅读haskell中的语法 `Typ{..}`?

haskell - 如何将 stack 项目转换为 cabal 项目?

haskell - 管道和创建基于管道的库

haskell - 如果 Haskell 总是评估为 True,为什么它有 isIEEE?

haskell - 只做 GHC 类型检查?

haskell - Monad 转换器 monad 重复

haskell - 鉴于 STG 生成自定义 ABI,Haskell 链接如何工作

Haskell:使用规则列表过滤列表

haskell - 如何处理 Applicative 的副作用?

haskell - 在 Haskell 中查找字符的 Unicode 脚本