简体   繁体   English

理解 ML 编程中的“let”和“in”

[英]Understanding "let" & "in" in ML programming

My teacher recently went over a function in ML that uses "let" & "in" but the body of the function is confusing to me as I dont understand how they work together to produce the result.我的老师最近在 ML 中查看了 function,它使用“let”和“in”,但 function 的主体让我感到困惑,因为我不明白它们如何协同工作以产生结果。 The function takes a list of vegetables in your garden and replaces an original vegetable with a given substitute, so that list will print out the substitute in every location where the original element is in.This is the function function 获取您花园中的蔬菜列表,并用给定的替代品替换原始蔬菜,因此该列表将在原始元素所在的每个位置打印出替代品。这是 function

image to function code图片转function码

fun replaceVegetable(orig, subt, Garden([]) = Garden([])

  | replaceVegetable(orig, subt, Garden([first::rest]) =

      let val Garden(newRest) = 

          replaceVegetable(orig, subst, Garden(rest));

      in Garden((if first = orig then subst else first)::newRest)

      end

  | replaceVegetable(orig, subst, x) = x;

Im not worried about the last pattern "replaceVegetable(orig, subst, x) = x;", im mainly concerned about understanding the second pattern.我不担心最后一个模式“replaceVegetable(orig, subst, x) = x;”,我主要关心理解第二个模式。 I think I understand that Garden(newRest) is a local variable to the function and that whatever replaceVegetable(orig, subst, Garden(rest)) produces will be stored in that local variable.我想我明白 Garden(newRest) 是 function 的局部变量,无论 replaceVegetable(orig, subst, Garden(rest)) 产生什么都将存储在该局部变量中。 I dont exactly know what happens on "in Garden((if first = orig then subst else first)::newRest)" is this applying recursion so it can run through the list I give it to see where it has to replace the original with the substitute?我不完全知道在“in Garden((if first = orig then subst else first)::newRest)”上会发生什么,这是在应用递归,这样它就可以遍历我给它的列表,看看它必须在哪里替换原来的替补? If so, I can't exactly see how its doing that as the function as a whole is confusing for me to look at.如果是这样,我无法确切地看到它是如何做到的,因为 function 作为一个整体让我感到困惑。

let , in , and end go together; let , in , 和end go 在一起; in Garden((if first... is not a "unit of language" and doesn't mean anything. in Garden((if first...不是“语言单位”,也没有任何意义。

In the simpler form,在更简单的形式中,

let val x = y in e end

means "in the expression 'e', 'x' has the same value as 'y'.表示“在表达式‘e’中,‘x’与‘y’具有相同的值。

If the function took just a plain list, it might be easier to understand:如果 function 只是一个简单的列表,可能更容易理解:

fun replaceVegetable(orig, subst, []) = []
  | replaceVegetable(orig, subst, first::rest) =
    let val newRest  = 
            replaceVegetable(orig, subst, rest)
    in (if first = orig then subst else first)::newRest
    end
  | replaceVegetable(orig, subst, x) = x;

The second case here is exactly the same as这里的第二种情况与

| replaceVegetable(orig, subst, first::rest) =
      (if first = orig then subst else first)::(replaceVegetable(orig, subst, rest))

Your function also has a pattern-matching binding instead of a plain variable binding.您的 function 还具有模式匹配绑定,而不是普通变量绑定。

let val Garden newRest = replaceVegetable(...)
in ...
end

matches on the result of the recursion and binds the "wrapped" list to newRest .匹配递归的结果并将“包装”列表绑定到newRest
It means exactly the same as意思完全一样

case replaceVegetable (...) of
     Garden newRest => ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM