初学者 SML 语法 [英] Beginner SML Syntax

查看:47
本文介绍了初学者 SML 语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 SML 很陌生.我目前正在开展一个项目,该项目正在检查手机是否平衡.

I am very new to SML. I am currently working on a project that is checking to see if a mobile is balanced.

我的数据类型 mobile 定义如下:

My datatype mobile is defined as follows:

datatype mobile = Object of int 
                | Wire   of mobile * mobile

然后我有一个重量功能来检查手机的重量:

Then I have a weight function to check the weight of a mobile:

fun weight (Object w)   = w 
  | weight (Wire (l,r)) = weight l + weight r

我现在正在尝试检查手机是否平衡.我有以下内容:

I am now trying to check if the mobile is balanced. I have the following:

fun balanced (Object w)   = true 
  | balanced (Wire (l,r)) = if weight l = weight r and balanced l and balanced r then true else false

但是,我不断收到错误消息:

However, I keep getting an error:

stdIn:18.19-18.31 Error: syntax error: deleting  AND ID
stdIn:18.34 Error: syntax error found at AND

有人能告诉我我做错了什么吗?

Could someone please tell me what I am doing wrong?

推荐答案

正如 Brian 所指出的,我们在 SML 中使用 andalsoorelse.

As Brian points out, we use andalso and orelse in SML.

但是,当正确修复时,代码中没有错误.

However there are no errors in the code, when this is fixed correctly.

正如 Andreas Rossberg 所指出的,当你写出一个形式的表达式时

Also as pointed out by Andreas Rossberg, when ever you write an expression of the form

if b then 
  true
else
  false

那你应该立刻想到这张图片,并与表达式 b,因为它显然是一样的.

then you should immediately think of this picture, and exchange it with the expression b, as it is obviously the same.

鉴于此,您的 balanced 函数最终看起来像这样

Given this, your balanced function end up looking something like this

fun balanced (Object w)   = true
  | balanced (Wire (l,r)) = weight l = weight r andalso
                            balanced l andalso balanced r

这篇关于初学者 SML 语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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