Recursive function that returns all values in list (In OCaml)?

Your function is equivalent to : let rec elements list = match list with | -> | h :: t -> elements t This happens because a ; be evaluates a (and discards the result) and then evaluates and returns be Obviously, this is in turn equivalent to: let elements (list : 'a list) = This is not a very useful function Before you try solving this, however, please understand that Objective Caml functions can only return one value Returning more than one value is impossible There are ways to work around this limitation. One solution is to pack all the values you wish to return into a single value: a tuple or a list, usually. So, if you need to return an arbitrary number of elements, you would pack them together into a list and have the calling code process that list: let my_function () = 1 ; 2; 3; 4 in (* Return four values *) List.

Iter print_int (my_function ()) (* Print four values *) Another less frequent solution is to provide a function and call it on every result: let my_function action = action 1 ; action 2 ; action 3 ; action 4 in my_function print_int This is less flexible, but arguably faster, than returning a list : lists can be filtered, sorted, stored.

Your function is equivalent to : let rec elements list = match list with | -> | h :: t -> elements t This happens because a ; be evaluates a (and discards the result) and then evaluates and returns b. Obviously, this is in turn equivalent to: let elements (list : 'a list) = This is not a very useful function. Before you try solving this, however, please understand that Objective Caml functions can only return one value.

Returning more than one value is impossible. There are ways to work around this limitation. One solution is to pack all the values you wish to return into a single value: a tuple or a list, usually.So, if you need to return an arbitrary number of elements, you would pack them together into a list and have the calling code process that list: let my_function () = 1 ; 2; 3; 4 in (* Return four values *) List.

Iter print_int (my_function ()) (* Print four values *) Another less frequent solution is to provide a function and call it on every result: let my_function action = action 1 ; action 2 ; action 3 ; action 4 in my_function print_int This is less flexible, but arguably faster, than returning a list : lists can be filtered, sorted, stored...

Thank you for the response. I have been thinking about doing this in two steps with two functions, like you described, but my problem is with retrieving each element one by one. I should be a little bit more URL1 main function is a subset function in which I am given the task of determining whether list a is a subset of list b.

I am required to write this function in a way that is polymorphic, but right now I can only figure out how to do it with a list of ints. – Atticus Jan 13 at 11:03 The problem is because I wrote a function to return the first element of the list: let head list = match list with h::t -> h | -> 0. Since I have to account for the empty list, I return 0, and OCaml determines that this function takes a list of ints.

Since the subset function needs to be polymorphic, I cannot know ahead of time what to return for the empty set, be it 0, "null", or whatever. This therefore limits me to lists of ints. – Atticus Jan 13 at 11:07 in this case (what to return for the empty list), simply raise an error : (failwith "error") – gasche Jan 13 at 11:12 2 I think you're going completely in the wrong direction on this matter.

If you have trouble writing a subset function, I would advise you to ask a question about that - you would get answers that are cleaner and more adapted to your actual problem (such as let subset a be = List. For_all (fun I -> List. Mem I b) a) as well as constructive comments on the code you already wrote.

– Victor Nicollet Jan 13 at 11:24.

Your question is kind of confusing - you want a function that returns all the values in a list. Well the easiest way of returning a variable number of values is using a list! Are you perhaps trying to emulate Python generators?

OCaml doesn't have anything similar to yield, but instead usually accomplishes the same by "passing" a function to the value (using iter, fold or map). What you have currently written is equivalent to this in Python: def elements(list): if(len(list) == 0): return else: list0 return elements(list1:) If you are trying to do this: def elements(list): if(len(list) > 0): yield list0 # this part is pretty silly but elements returns a generator for e in elements(list1:): yield e for x in elements(1,2,3,4,5): dosomething(x) The equivalent in OCaml would be like this: List. Iter dosomething 1;2;3;4;5 If you are trying to determine if list a is a subset of list be (as I've gathered from your comments), then you can take advantage of List.

Mem and List. For_all: List. For_all (fun x -> List.

Mem x b) a fun x -> List. Mem x be defines a function that returns true if the value x is equal to any element in (is a member of) b. List.

For_all takes a function that returns a bool (in our case, the membership function we just defined) and a list. It applies that function to each element in the list. If that function returns true for every value in the list, then for_all returns true.So what we have done is: for all elements in a, check if they are a member of b.

If you are interested in how to write these functions yourself, then I suggest reading the source of list. Ml, which (assuming *nix) is probably located in /usr/local/lib/ocaml or /usr/lib/ocaml.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions