MLにおける結合と連続


あなたが以前にさびたコードで働いたならば、あなたはこのようなコードに遭遇したかもしれません
let a = {
  let x = 5;
  let y = 4;
  let x = 7;
  x + y
}
Cベースの言語を使用して過去の経験を考慮すると、いくつかの質問が心に湧きます.
  • なぜ同じ変数を複数回定義するのは合法的ですか?
  • なぜブロックは最後の式に評価されますか?
  • 答えを得るために、それは遠くにいとこを見ることができます錆.

    入力する


    NB: You can refer to https://learnxinyminutes.com/docs/ocaml/ for a quick crash course on OCaml's syntax


    ocamlの同じコードは次のように書かれます:
    let a =
      let x = 5 in
      let y = 4 in
      let x = 7 in
      x + y
    
    これは以下の構文的糖度と見なされます.
    (* bind is a Higher-order function that takes a value and
     * a function, and returns the result of applying that value
     * to the function
     *)
    let bind x fn = fn x;;
    
    bind 5 (fun x ->
      bind 6 (fun y ->
        bind 7 (fun x ->
          x + y
        )
      )
    );;
    
    (* Same code but without indentation.
     * Notice how bind has replaced the assignment operator.
     *)
    bind 5 (fun x ->
    bind 6 (fun y ->
    bind 7 (fun x ->
    x + y)));;
    
    我々の最初のOCMLスニペットの各行が我々の第2の断片の入れ子になった機能に対応するので、我々はなぜそれが複数回同じ変数を定義するのが合法であるかについて見ることができます.また、なぜブロックは、そのボディ内の最後の値に評価する式だけであることを示します.

    一石で鳥を殺す


    代入演算子をカスタマイズ可能なバインド関数に置き換えるには、Rust ' s try演算子とAsync/Wait構文を含む、コア言語で実装されるべき年を要したすべての種類を行うことができます.
    これがどのように行われるかのすべての詳細をあなたに教えていない間、あなたがウサギ穴がどれくらい深いかに興味があるならば、私は若干の資源をあなたに残します.
  • https://www2.lib.uchicago.edu/keith/ocaml-class/definitions.html
  • https://fsharpforfunandprofit.com/posts/computation-expressions-continuations/#continuations-and-let
  • https://jobjo.github.io/2019/04/24/ocaml-has-some-new-shiny-syntax.html
  • https://www.cs.cornell.edu/courses/cs3110/2015fa/l/17-async/rec.html