Home > Back-end >  How can I calculate the length of a list containing lists in OCAML
How can I calculate the length of a list containing lists in OCAML

Time:04-14

i am a beginner in ocaml and I am stuck in my project.

I would like to count the number of elements of a list contained in a list.

Then test if the list contains odd or even lists.

let listoflists = [[1;2] ; [3;4;5;6] ; [7;8;9]] 

output

l1 = even
l2 = even 
l3 = odd

The problem is that :

 List.tl listoflists 

Gives the length of the rest of the list so 2

-> how can I calculate the length of the lists one by one ?

-> Or how could I get the lists and put them one by one in a variable ?

for the odd/even function, I have already done it !

Tell me if I'm not clear and thank you for your help .

CodePudding user response:

Unfortunately it's not really possible to help you very much because your question is unclear. Since this is obviously a homework problem I'll just make a few comments.

  1. Since you talk about putting values in variables you seem to have some programming experience. But you should know that OCaml code tends to work with immutable variables and values, which means you have to look at things differently. You can have variables, but they will usually be represented as function parameters (which indeed take different values at different times).

  2. If you have no experience at all with OCaml it is probably worth working through a tutorial. The OCaml.org website recommends the first 6 chapters of the OCaml manual here. In the long run this will probably get you up to speed faster than asking questions here.

  3. You ask how to do a calculation on each list in a list of lists. But you don't say what the answer is supposed to look like. If you want separate answers, one for each sublist, the function to use is List.map. If instead you want one cumulative answer calculated from all the sublists, you want a fold function (like List.fold_left).

  4. You say that List.tl calculates the length of a list, or at least that's what you seem to be saying. But of course that's not the case, List.tl returns all but the first element of a list. The length of a list is calculated by List.length.

  5. As the commenter points out, your calculations of even and odd seem inverted. You are calling the even subblists odd and vice versa.

If you give a clearer definition of your problem and particularly the desired output you will get better help here.

CodePudding user response:

  1. Use List.iter f xs to apply function f to each element of the list xs.

  2. Use List.length to compute the length of each list.

  3. Even numbers are integrally divisible by two, so if you divide an even number by two the remainder will be zero. Use the mod operator to get the remainder of the division. Alternatively, you can rely on the fact that in the binary representation the odd numbers always end with 1 so you can use land (logical and) to test the least significant bit.

  4. If you need to refer to the position of the list element, use List.iteri f xs. The List.iteri function will apply f to two arguments, the first will be the position of the element (starting from 0) and the second will be the element itself.

  • Related