如何将逻辑表示法转换为Haskell语法

编程入门 行业动态 更新时间:2024-10-11 17:24:49
本文介绍了如何将逻辑表示法转换为Haskell语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我最近在uni上找到了Haskell,我正在通过一系列练习来工作,下面是我无法理解的一个片段:

I've recently picked up Haskell at uni and I'm working my way through a set of exercises, here's a snippet of one that I can't make sense of:

考虑以下简单的前缀计算器语言的语法:

"Consider the following grammar for a simple, prefix calculator language:"

num ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 int ::= num | num int expr ::= int | - expr | + expr expr | * expr expr

我很困惑如何将它转换成Haskell语法(我是一个完整的新手在Haskell和函数式编程,请温柔)

I'm confused as how to translate this into Haskell syntax (I'm a complete beginner in both Haskell and functional programming, please be gentle)

我怀疑 num , int 和 expr 都是假设可以使用 data 声明的类型/值>或键入,并且它们对计算器施加约束。然而,我无法理解:我如何声明类型或数据(不是变量)值,即0-9?另外,如何在声明中放置 - 或 + 等符号?

I suspect that num, int and expr are all, supposedly, types/values that can be declared using data or type and that they impose constraints on the calculator. However I can't make sense of either: How do I declare type or data(not a variable) for fixed values, namely 0-9? Also, how can I put symbols like - or + in a declaration?

推荐答案

不要混淆表示AST的语法中的字符串。比较字符串

Don't confuse a string in the grammar for the AST that represents it. Compare the string

"+ + 3 4 5"

这是你给出的文法中的一个字符串

which is a string in the grammar you've been given with

Plus (Plus (Literal 3) (Literal 4)) (Literal 5)

这是一个合理的可以解析为 String 的AST的Haskell值。

which would be a sensible Haskell value for the AST that String could get parsed to.

更多推荐

如何将逻辑表示法转换为Haskell语法

本文发布于:2023-11-30 19:27:20,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:转换为   如何将   语法   逻辑   Haskell

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!