Haskell 可以像 Racket 的比赛一样在 S-Expression 上进行比赛吗?

发布于 2024-11-13 06:16:34 字数 383 浏览 3 评论 0原文

我三天前刚刚开始学习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 技术交流群。

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

发布评论

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

评论(1

傾旎 2024-11-20 06:16:34

Classic Haskell 不提供通用模式匹配。它确实提供标准模式匹配和防护。因此,您可以编写诸如

foo :: [Int] -> ...
foo [1,2,3] = <some expression>
foo [1,2,x] = <some expression that can use x>
foo (x:xs) = <some expression where x is the first element, and xs is the rest>
foo (x:x':xs) = <some expression where x is the first element, x' the second, and xs is the rest>

bar :: (Int,String) -> ...
bar (1,"hello") =
bar (someInt,someString) =
bar (someInt,_) = 
bar _ = 

交替:

bar :: (Int, String) -> ...
bar x = case x of
         (1,"hello") -> ...
         _ -> ...

交替:

bar :: (Int, String) -> ...
bar (someInt,someString) 
         | someInt == 1 && someString == "hello" = ...
         | someInt == 2 && someString == "hello" = ...
         | otherwise = ...

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

foo :: [Int] -> ...
foo [1,2,3] = <some expression>
foo [1,2,x] = <some expression that can use x>
foo (x:xs) = <some expression where x is the first element, and xs is the rest>
foo (x:x':xs) = <some expression where x is the first element, x' the second, and xs is the rest>

bar :: (Int,String) -> ...
bar (1,"hello") =
bar (someInt,someString) =
bar (someInt,_) = 
bar _ = 

alternately:

bar :: (Int, String) -> ...
bar x = case x of
         (1,"hello") -> ...
         _ -> ...

alternately:

bar :: (Int, String) -> ...
bar (someInt,someString) 
         | someInt == 1 && someString == "hello" = ...
         | someInt == 2 && someString == "hello" = ...
         | otherwise = ...

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文