在存储路径的同时搜索树
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你怎么知道只有一场比赛?
尝试这样的事情:(
未经测试。)这假设您想要第一个匹配项(如果有多个匹配项)。
(您将需要使用“参数”的正确拼写。)
就我个人而言,如果可以的话,我会将参数按相反的顺序排列:
How do you know there's only one match?
Try something like this:
(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: