在没有case语句的情况下,在SML DataType中解压缩数据 [英] 'Unpacking' the data in an SML DataType without a case statement

查看:131
本文介绍了在没有case语句的情况下,在SML DataType中解压缩数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个SML程序,它表示一个由表达式组成的表达式的语言:

 数据类型Value = int的IntVal 
|价值清单的列表价值

datatype Exp =价值
|的Const Exp * Exp
|的加号减去Exp * Exp
| Exp的时间* Exp

我还在写一个将表达式转换为值的eval函数。如果表达式是Plus表达式(例如 Plus(Const(IntVal 1),Const(IntVal 1)),它表示 1 + 1 ),我只想取出存储在IntVal中的整数,并将它们加在一起并返回。



但是据我所知,为了得到IntVal数据类型中的整数,我只能看到一个看起来多余的case语句,只有一个case: fun eval e =
(*评估不同的类型(*如果它是一个常量,那么只需返回值*)
Const v => v
(*如果是加号,我们要将两个值*)加在一起
| Plus(x,y)=>
(*只有一个案例似乎多余的案例陈述*)
案例(eval x,eval y)
(IntVal xVal,IntVal yVal)=> IntVal(xVal + yVal)

有没有简单的方法可以简化它?我想要做这样的事情,当然这不是有效的SML:

  fun eval e = 
$ e
Const v => v
| Plus(x,y)=> IntVal(eval x + eval x)


解决方案

你的 eval 函数返回一个 int ,你还没有弄清楚如何得到一个 int 来自 Value ,它使用 ListVal 构造函数 - 只需提供与您的预期定义涵盖的案例相对应的模式。

  fun eval(Const(IntVal v))= v 
| eval(Plus(e1,e2))= eval(e1)+ eval(e2)
| eval(Minus(e1,e2))= eval(e1) - eval(e2)
| eval(Times(e1,e2))= eval(e1)* eval(e2);

SML / NJ给出警告:match nonexhaustive - 但是如果它符合你的意图,那么你可以忽略这个警告。



上面的代码返回一个 int 。如果你想返回看起来像例如 IntVal 3 然后你可以定义3个函数,它们对 IntVals 并返回 IntVals 对应于它们的总和,差异和产品,并在上述定义的右边使用这些函数。


I have an SML program which represents a language with Expressions that are comprised of Values:

datatype Value = IntVal of int
               | ListVal of Value list

datatype Exp = Const of Value
             | Plus of Exp * Exp
             | Minus of Exp * Exp
             | Times of Exp * Exp

I'm also writing an eval function that converts an expression into a value. If the expression is a Plus expression (e.g. Plus (Const (IntVal 1), Const (IntVal 1)) which represents 1+1), I just want to take out the integer stored in the IntVal and just add them together and return that.

But as far as I can tell, I have to have a seemingly redundant case statement with only one case just to get at the integer inside the IntVal data type:

(*Evaluates an Exp and returns a Value*)
fun eval e =
  (*Evaluate different types of Exp*)
  case e of
      (*If it's a constant, then just return the Value*)
      Const v => v
      (*If it's a Plus, we want to add together the two Values*)
    | Plus (x,y) =>
         (*Case statement with only one case that seems redundant*)
         case (eval x, eval y) of
            (IntVal xVal, IntVal yVal) => IntVal (xVal + yVal)

Is there no easy way to do simplify this? I'd like to do something like this, which of course isn't valid SML:

fun eval e =
  case e of
      Const v => v
    | Plus (x,y) => IntVal (eval x + eval x)

解决方案

If you want your eval function to return an int and you haven't figured out how to get an int from a Value which uses the ListVal constructor -- it is enough to just supply patterns which correspond to the cases that your intended definition covers.

fun eval (Const (IntVal v)) = v
|   eval (Plus (e1,e2)) = eval(e1) + eval(e2)
|   eval (Minus (e1,e2)) = eval(e1) - eval(e2)
|   eval (Times (e1,e2)) = eval(e1) * eval(e2);

SML/NJ gives Warning: match nonexhaustive - but if it matches your intention then you can ignore the warning.

The above code returns an int. If you want to return values which look like e.g. IntVal 3 then you could define 3 functions which take pairs of IntVals and return IntVals corresponding to their sums, differences, and products and use these functions on the right hand sides of the above definition.

这篇关于在没有case语句的情况下,在SML DataType中解压缩数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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