【问题标题】:Beginner SML Syntax初学者 SML 语法
【发布时间】:2012-12-31 04:37:27
【问题描述】:

我对 SML 很陌生。我目前正在做一个检查手机是否平衡的项目。

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

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

然后我有一个权重函数来检查手机的重量:

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

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

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

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

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

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

【问题讨论】:

    标签: syntax sml


    【解决方案1】:

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

    但是,如果正确修复,代码中没有错误。

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

    if b then 
      true
    else
      false
    

    那你应该马上想到this picture,换成b这个表达式,显然是一样的。

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

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

    【讨论】:

      【解决方案2】:

      顺便说一句,这是一种非常低效的方法来确定移动设备是否 是平衡的,因为子树的权重是反复计算的 再次。想想一个函数 weight_of_balanced_mobile 要么返回 如果手机不平衡,则为 NONE,如果平衡,则为 SOME w。

      fun weight_of_balanced_mobile (Object w) = SOME w
        | weight_of_balanced_mobile (Wire (l,r)) =
             case weight_of_balanced_mobile l
               of NONE => NONE
                | SOME u => case weight_of_balanced_mobile r
                              of NONE => NONE
                               | SOME v => if u = v then SOME (u+v) else NONE;
      
       fun is_balanced mobile =
              case weight_of_balanced_mobile mobile
                of NONE => false
                 | SOME _ => true;
      

      这里的问题是你的“平衡”函数只返回一点 信息,而为了有效地计算,我们需要更多的信息。一世 已经开始将布尔值视为危险信号。

      另一种构造计算的方法,以便您获得更多信息 (不仅仅是平衡的东西,而是它有多重)是通过 一个延续。我们将制作一个需要手机和“什么”的功能 如果这个手机是平衡的' 的论点。哦,让我们花点时间 '如果不是那么好使用的值。

      (* val check_balance : mobile -> 'a -> (int -> 'a) -> 'a *)
      fun check_balance (Object w) _ f = f w
        | check_balance (Wire (l,r)) d f =
             check_balance l d (fn u =>
                check_balance r d (fn v =>
                   if u = v then f (u+v) else d));
      
      fun is_balanced mobile = check_balance mobile false (fn _ => true);
      

      如果你看的刚刚好,这和之前的代码是一样的 翻过来了。

      【讨论】:

        【解决方案3】:

        and 更改为andalso 可以解决syntax error found at AND 错误。

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

        但是你得到了这个:

        stdIn:5.8-5.29 Error: operand of andalso is not of type bool [tycon mismatch]
          operand: mobile
          in expression:
            (balanced l) andalso r
        

        这是因为weight 函数的类型是val weight = fn : mobile -> int,它不满足andalso 的布尔约束,因为它返回一个int

        【讨论】:

        • 另外,if b then true else false 只是b 的复杂表达方式。
        猜你喜欢
        • 2023-03-26
        • 1970-01-01
        • 1970-01-01
        • 2012-02-11
        • 2015-02-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-05-23
        相关资源
        最近更新 更多