ocaml

Tail recursive function to find depth of a tree in Ocaml

做~自己de王妃 提交于 2019-11-27 00:42:27
I have a type tree defined as follows type 'a tree = Leaf of 'a | Node of 'a * 'a tree * 'a tree ;; I have a function to find the depth of the tree as follows let rec depth = function | Leaf x -> 0 | Node(_,left,right) -> 1 + (max (depth left) (depth right)) ;; This function is not tail recursive. Is there a way for me to write this function in tail recursive way? You can trivially do this by turning the function into CPS (Continuation Passing Style). The idea is that instead of calling depth left , and then computing things based on this result, you call depth left (fun dleft -> ...) , where

Does != have meaning in OCaml?

我的未来我决定 提交于 2019-11-27 00:16:12
问题 It seems to be an equivalency comparison for some types, but not strings. # 3 != 3;; - : bool = false # 3 != 2;; - : bool = true This is as expected. # "odp" = "odp";; - : bool = true # "odp" != "odp";; - : bool = true # "odp" <> "odp";; - : bool = false Why does "odp" != "odp" evaluate to true ? What is it actually doing? Shouldn't it generate a type error? 回答1: you have experienced the difference between structural and physical equality. <> is to = (structural equality) as != is to ==

Unary minus and floating point number in OCaml

青春壹個敷衍的年華 提交于 2019-11-26 22:08:31
问题 I wanted to have a vector of complex numbers in my program, so I wrote this: [|pt 0. 0.; pt -4. 1.; pt -7. -2.; pt 4. 5.; pt 1. 1.|] Here pt is a function of type float -> float -> Complex.t . But ocaml refused to compile this saying: Characters 12-14: [|pt 0. 0.; pt -4. 1.; pt -7. -2.; pt 4. 5.; pt 1. 1.|];; ^^ Error: This expression has type float -> float -> Complex.t but an expression was expected of type int What I wanted to do here is (obviously) include the complex number whose real

tail recursion vs. forward recursion

半世苍凉 提交于 2019-11-26 19:25:08
问题 Can someone give me the difference between these two kinds recursions and example (specifically in OCaml)? 回答1: A tail recursive function is a function where the only recursive call is the last one in the function. A non-tail recursive function is a function where that is not the case. A backward recursion is a recursion where in each recursive call the value of the parameter is less than in the previous step. A forward recursion is a recursion where it grows bigger with each step. Those are

Ocaml Unbound Graphics Module

纵饮孤独 提交于 2019-11-26 17:13:53
问题 Running open Graphics;; in ocaml returns an error, saying it is an unbound module. Running it in terminal (ocaml) returns the same thing. Does this mean my Graphics Module was somehow not installed with the ocaml package? If so, how can I install the module? On Fedora. 回答1: Graphics module is not ready by default. You need to load it manually. In toplevel: $ ocaml OCaml version blahblah # #load "graphics.cma";; # open Graphics;; or you can specify it at the command line: $ ocaml graphics.cma

Why does OCaml sometimes require eta expansion?

狂风中的少年 提交于 2019-11-26 16:41:35
问题 If I have the following OCaml function: let myFun = CCVector.map ((+) 1);; It works fine in Utop, and Merlin doesn't mark it as a compilation error. When I try to compile it, however, I get the following error: Error: The type of this expression, (int, '_a) CCVector.t -> (int, '_b) CCVector.t , contains type variables that cannot be generalized If I eta-expand it however then it compiles fine: let myFun foo = CCVector.map ((+) 1) foo;; So I was wondering why it doesn't compile in eta-reduced

F# changes to OCaml [closed]

▼魔方 西西 提交于 2019-11-26 11:48:01
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 3 years ago . F# is derived from OCaml, but what major items are missing or added? Specifically I\'m curious as to whether the resources available for learning OCaml are also useful to someone who wants to learn F#. 回答1: The main differences are that F# does not support: functors OCaml-style objects polymorphic variants the

Why are functions in Ocaml/F# not recursive by default?

北城余情 提交于 2019-11-26 10:21:11
问题 Why is it that functions in F# and Ocaml (and possibly other languages) are not by default recursive? In other words, why did the language designers decide it was a good idea to explicitly make you type rec in a declaration like: let rec foo ... = ... and not give the function recursive capability by default? Why the need for an explicit rec construct? 回答1: The French and British descendants of the original ML made different choices and their choices have been inherited through the decades to

In Functional Programming, what is a functor?

▼魔方 西西 提交于 2019-11-26 10:04:36
问题 I\'ve come across the term \'Functor\' a few times while reading various articles on functional programming, but the authors typically assume the reader already understands the term. Looking around on the web has provided either excessively technical descriptions (see the Wikipedia article) or incredibly vague descriptions (see the section on Functors at this ocaml-tutorial website). Can someone kindly define the term, explain its use, and perhaps provide an example of how Functors are

Tail recursive function to find depth of a tree in Ocaml

巧了我就是萌 提交于 2019-11-26 09:27:26
问题 I have a type tree defined as follows type \'a tree = Leaf of \'a | Node of \'a * \'a tree * \'a tree ;; I have a function to find the depth of the tree as follows let rec depth = function | Leaf x -> 0 | Node(_,left,right) -> 1 + (max (depth left) (depth right)) ;; This function is not tail recursive. Is there a way for me to write this function in tail recursive way? 回答1: You can trivially do this by turning the function into CPS (Continuation Passing Style). The idea is that instead of