What's the reason of 'let rec' for impure functional language OCaml?

前端 未结 6 1764
梦谈多话
梦谈多话 2021-02-02 06:54

In the book Real World OCaml, the authors put why OCaml uses let rec for defining recursive functions.

OCaml distinguishes between nonrecurs

6条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-02 07:51

    I think this has nothing to do with being purely functional, it is just a design decision that in Haskell you are not allowed to do

    let a = 0;;
    let a = a + 1;;
    

    whereas you can do it in Caml.

    In Haskell this code won't work because let a = a + 1 is interpreted as a recursive definition and will not terminate. In Haskell you don't have to specify that a definition is recursive simply because you can't create a non-recursive one (so the keyword rec is everywhere but is not written).

提交回复
热议问题