ml 数据类型(带有原始函数)如何制作?
我有这个数据类型
datatype e = X | Const of int | P of e*e | S of e*e | M of e*e | D of e*e;
和这个过程
val rec evl = fn (Const k)=>(fn x=>k)| X=> (fn x=>x)| P(e1,e2)=> (fn x=> (evl e1 x)+(evl e2 x))| S(e1,e2)=> (fn x=> (evl e1 x)-(evl e2 x))| M(e1,e2)=> (fn x=> (evl e1 x)*(evl e2 x))| D(e1,e2)=> (fn x=> (evl e1 x)/(evl e2 x));
如何扩展这个数据类型和 evl 过程:
-val addsub = evl( A( X(1),X(2),X(3), S( X(4),X(5) ) )) ; addsub(4,5,2,9,8) 返回它 = 12 (4+5+2+(9-8))
P = +, S = -, M = * , D = / 不仅仅是 X(5),我需要 X(n)...?
i have this datatype
datatype e = X | Const of int | P of e*e | S of e*e | M of e*e | D of e*e;
and this procedure
val rec evl = fn (Const k)=>(fn x=>k)| X=> (fn x=>x)| P(e1,e2)=> (fn x=> (evl e1 x)+(evl e2 x))| S(e1,e2)=> (fn x=> (evl e1 x)-(evl e2 x))| M(e1,e2)=> (fn x=> (evl e1 x)*(evl e2 x))| D(e1,e2)=> (fn x=> (evl e1 x)/(evl e2 x));
how to expand this datatype and evl procedure to do:
-val addsub = evl( A( X(1),X(2),X(3), S( X(4),X(5) ) )) ;
addsub(4,5,2,9,8) return it = 12 (4+5+2+(9-8))
P = +, S = -, M = * , D = /
and not just for X(5), I need for X(n)...?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
关于您的数据类型和函数的一些注释:
算术语境中的含义
表达。
让你的代码很难
理解。
S(减)和 D(除)不可交换,对参数列表执行这些操作是一个坏主意。我演示了如何使用 P(加)和 M(乘)来实现此目的:
例如:
evl2 (P [Const 3, Const 2, M [Const 3, Const 2, Const 1]])
将返回11
。如果你还想用S和D来做,你可以从上面的代码片段来推断。
Some notes about your datatype and function:
meaning in context of arithmetic
expression.
makes your code so hard to
understand.
S (subtract) and D (divide) are not commutative, it is a bad idea to do those operations on a list of arguments. I demonstrate how to do so with P (Plus) and M (Multiply):
For example:
evl2 (P [Const 3, Const 2, M [Const 3, Const 2, Const 1]])
would return11
.If you still want to do it with S and D, you can infer from the above code fragment.