# Is this implementation truely tail-recursive?

Filed under:
|
|
|
##### tail-recursion

Hello everyone!

I've come up with the following code to compute in a tail-recursive way the result of an expression such as `3 4 * 1 + cos 8 *` (aka `8*cos(1+(3*4))`)

The code is in OCaml. I'm using a `list ref`to emulate a stack.

``````type token =
Num of float
| Fun of (float->float)
| Op of (float->float->float);;

let pop l = let top = (List.hd !l) in l := List.tl (!l); top;;
let push x l = l := (x::!l);;
let empty l = (l = []);;
let pile = ref [];;

let eval data =
let stack = ref data in

let rec _eval cont =
match (pop stack) with
| Num(n) -> cont n;
| Fun(f) -> _eval (fun x -> cont (f x));
| Op(op) -> _eval (fun x -> cont (op x (_eval (fun y->y))));
in _eval (fun x->x)
;;

eval [Fun(fun x -> x**2.); Op(fun x y -> x+.y); Num(1.); Num(3.)];;
``````

I've used continuations to ensure tail-recursion, but since my stack implements some sort of a tree, and therefore provides quite a bad interface to what should be handled as a disjoint union type, the call to my function to evaluate the left branch with an identity continuation somehow irks a little.

Yet it's working perfectly, but I have the feeling than in calling the `_eval (fun y->y)` bit, there must be something wrong happening, since it doesn't seem that this call can replace the previous one in the stack structure... Am I misunderstanding something here?

I mean, I understand that with only the first call to `_eval` there wouldn't be any problem optimizing the calls, but here it seems to me that evaluation the `_eval (fun y->y)` will require to be stacked up, and therefore will fill the stack, possibly leading to an overflow...

Thanks!

© Stack Overflow or respective owner

• #### Search Engine Optimization - The Importance of Page Optimization in Search Engine Optimization

as seen on Ezine Articles - Search for 'Ezine Articles'
In order for your website to rank well, your internal linking structure is critical to your success. This is covered some of the theory for this in various articles and blogs about Page Structure of a website, which said how you should map out the physical linking structure, but in this guide I will… >>> More

• #### SEO Optimization - How to Master the SEO Optimization Process in Four Easy Steps

as seen on Ezine Articles - Search for 'Ezine Articles'
Search engine optimization is a critical part of any internet marketing campaign but can often be intimidating to new marketers. In this article you will learn the 4 basic components of SEO. >>> More

• #### Keywords Optimization For Website Optimization

as seen on Ezine Articles - Search for 'Ezine Articles'
Saying that you need to do website optimization sounds like saying you need to get healthy. To get healthy we do 2 things: diet management and exercise. Lets start with diet management. Keywords are like food for your WebPages. This article explains the role of keywords in website optimization. >>> More

• #### Importance of On-Page Optimization in Search Engine Optimization (SEO)

as seen on Ezine Articles - Search for 'Ezine Articles'
On-page optimization is crucial for any website. Read the importance of on-page optimization and how it would be helpful for getting high ranking in search engines. >>> More

• #### The Expert Secret to Search Engine Optimization - Effective Website Optimization

as seen on Ezine Articles - Search for 'Ezine Articles'
Throwing keywords into a program that shows you how popular they are and then using those keywords without doing a little bit of preliminary research and answering some very important questions can just spell disaster. There are three questions that are extremely important to ask yourself before just… >>> More

• #### F# and OCaml

as seen on Stack Overflow - Search for 'Stack Overflow'
I hear that F# is derived from OCaml. How true is this statement? That is to say, are the resources available for learning OCaml useful to someone who wants to learn F#? What are the major differences between the two languages (aside from the fact that F# is .NET)? >>> More

• #### Good projects to learn OCaml and F#

as seen on Stack Overflow - Search for 'Stack Overflow'
After learning the basic syntax, reading some non-trivial code is a fast way to learn a language. We can also learn how to design a library/software during reading others' code. I have following lists. A Chess program in OCaml by Tomek Czajka. Hal Daumé has written several machine learning libraries… >>> More

• #### Higher-order type constructors and functors in Ocaml

as seen on Stack Overflow - Search for 'Stack Overflow'
Can the following polymorphic functions let id x = x;; let compose f g x = f (g x);; let rec fix f = f (fix f);; (*laziness aside*) be written for types/type constructors or modules/functors? I tried type 'x id = Id of 'x;; type 'f 'g 'x compose = Compose of ('f ('g 'x));; type 'f fix = Fix… >>> More

• #### Tail-recursive merge sort in OCaml

as seen on Stack Overflow - Search for 'Stack Overflow'
Hello world! I’m trying to implement a tail-recursive list-sorting function in OCaml, and I’ve come up with the following code: let tailrec_merge_sort l = let split l = let rec _split source left right = match source with | [] -> (left, right) | head :: tail ->… >>> More

• #### OCaml beginner needs help: What is wrong?

as seen on Stack Overflow - Search for 'Stack Overflow'
Code: let rec get_val (x, n) = match x with [] -> -1 | if (n=0) then (h::_) -> h else (_::t) -> get_val(t, n-1) ;; Error message: Characters 55-57: | if (n=0) then (h::_) - h ^^ Error: Syntax error >>> More