如何将逻辑表示法转换为Haskell语法 [英] How to translate logical notation to Haskell syntax

查看:96
本文介绍了如何将逻辑表示法转换为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语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆