在存储路径的同时搜索树

发布于 2024-12-09 11:04:53 字数 689 浏览 0 评论 0原文

type Pattern = [PatternPart]

data PatternPart =
    MatchTuple [PatternPart] |
    Named String |
    MatchAny    

data ArguementIndex =
    InTuple Int ArguementIndex | -- arguement is in tuple at index Int, then at index...
    ArgIndex Int

testPattern = [Any, MatchTuple[Any, Named "hello"]]

getArgIndex :: Pattern -> String -> Maybe ArguementIndex

我需要编写一个函数 getArgIndex 来搜索 testPattern 中的 "hello" 并返回 InTuple 1 (ArgIndex 1)

getArgIndex [Any, Any, MatchTuple [Named "hi"]] "hi" = Just (InTuple 2 (ArgIndex 0))

另一个例子,

我无法想出一种优雅的方法来做到这一点。

请指教。

type Pattern = [PatternPart]

data PatternPart =
    MatchTuple [PatternPart] |
    Named String |
    MatchAny    

data ArguementIndex =
    InTuple Int ArguementIndex | -- arguement is in tuple at index Int, then at index...
    ArgIndex Int

testPattern = [Any, MatchTuple[Any, Named "hello"]]

getArgIndex :: Pattern -> String -> Maybe ArguementIndex

I need to write a function getArgIndex to search testPattern for "hello" and return InTuple 1 (ArgIndex 1)

getArgIndex [Any, Any, MatchTuple [Named "hi"]] "hi" = Just (InTuple 2 (ArgIndex 0))

Another example

I cant come up with an elegant way to do this.

Please advise.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

紧拥背影 2024-12-16 11:04:53

你怎么知道只有一场比赛?

尝试这样的事情:(

import Data.Maybe (listToMaybe)

getArgIndex :: Pattern -> String -> Maybe ArguementIndex
getArgIndex haystack needle = listToMaybe (getArgIndices haystack needle)

getArgIndices :: Pattern -> String -> [ArguementIndex]
getArgIndices haystack needle
   = concat $ zipWith f [0..] haystack
  where f i (MatchTuple haystack')        = map (InTuple i) $ getArgIndices haystack' needle
        f i (Named item) | item == needle = [ArgIndex i]
        f i _                             = []

未经测试。)这假设您想要第一个匹配项(如果有多个匹配项)。

(您将需要使用“参数”的正确拼写。)

就我个人而言,如果可以的话,我会将参数按相反的顺序排列:

getArgIndices' :: String -> Pattern -> [ArguementIndex]
getArgIndices' needle = g
  where g = concat . zipWith f [0..]
        f i (MatchTuple haystack)         = map (InTuple i) $ g haystack
        f i (Named item) | item == needle = [ArgIndex i]
        f i _                             = []

How do you know there's only one match?

Try something like this:

import Data.Maybe (listToMaybe)

getArgIndex :: Pattern -> String -> Maybe ArguementIndex
getArgIndex haystack needle = listToMaybe (getArgIndices haystack needle)

getArgIndices :: Pattern -> String -> [ArguementIndex]
getArgIndices haystack needle
   = concat $ zipWith f [0..] haystack
  where f i (MatchTuple haystack')        = map (InTuple i) $ getArgIndices haystack' needle
        f i (Named item) | item == needle = [ArgIndex i]
        f i _                             = []

(Untested.) This assumes you want the first match if there is more than one.

(You will want to use the proper spelling of "argument".)

Personally, I would put the arguments in the opposite order, if I could:

getArgIndices' :: String -> Pattern -> [ArguementIndex]
getArgIndices' needle = g
  where g = concat . zipWith f [0..]
        f i (MatchTuple haystack)         = map (InTuple i) $ g haystack
        f i (Named item) | item == needle = [ArgIndex i]
        f i _                             = []
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文