Haskell 可以像 Racket 的比赛一样在 S-Expression 上进行比赛吗?
我三天前刚刚开始学习Haskell,目标是为Haskell中的一些自定义语义提供解释器。我有解释器的 Racket 实现,Racket 中 S-Expression 上的 match
匹配非常方便。说到Haskell,我不太确定是否有类似的东西存在? 或者我必须编写一些数据类型并将 S 表达式解析为定义的数据类型,然后在 Haskell 中使用一些匹配机制?
我想要匹配的东西(在球拍中),例如,如果有一个输入(来自文件或立场输入),例如:(lambda (v1 v2) (+ v1 v2))
,那么在 Racket 中,我可以这样写模式: (lambda (,v ...),body)
。然后做我想做的事。 在 Haskell 中,我可以做类似的事情吗?
I just started to learn Haskell three days ago, aiming for a interpreter for some custom-ed semantics in Haskell. I have the Racket implementation of the interpreter, the match
matching on the S-Expression in Racket is pretty handy. When it comes to Haskell, I am not quite sure whether there is something similar exists?
or I have to write some data types and parse the S-Expressions to the data types defined and then use some matching mechanism in Haskell?
The thing I want to match (in racket), for example, if there is an input (from a file or stand input) like: (lambda (v1 v2) (+ v1 v2))
, then in Racket, I can write the pattern like:(lambda (,v ...) ,body)
. and then do what I want later on.
In Haskell, can I do something similar?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Classic Haskell 不提供通用模式匹配。它确实提供标准模式匹配和防护。因此,您可以编写诸如
交替:
交替:
GHC 还提供扩展来更无缝地集成防护和模式匹配。请参阅“视图模式”和“模式防护”部分: http://www.haskell.org/ghc/docs/latest/html/users_guide/syntax-extns.html
Classic Haskell doesn't provide generalized pattern matching. It does provide both standard pattern matching, and guards. So you can write things like
alternately:
alternately:
GHC also provides extensions to integrate guards and pattern matching more seamlessly. See the sections on "View Patterns" and "Pattern Guards": http://www.haskell.org/ghc/docs/latest/html/users_guide/syntax-extns.html