0
votes

And thanks for your help. I'm currently working on an assigment, and I've been stuck with a faulty recursive call. I have a simple CAML-Light function which is supposed to take a list and a size (number), and return a list with a chunk of that list, of that size, and a list with the rest. Really simple actually, it's a translation from a past PLT-Scheme project.

However, I'm having trouble with the recursive call. I simply don't know why it's not working. The function goes like this:

let rec cortar texto longitud resultado = match texto, longitud with
            |  _::t,0 -> resultado::(t::[])
            |  c::t,x-> cortar t (longitud-1) (consderecha resultado c);;

Longitud is the integer which defines the size. I'm afraid I'm doing something terribly wrong, as it appears to loop infinitely because "longitud" never reaches 0. I'm (as you may guess) new to ML and it's dialects, so any help would be really appreciated.

Thanks!

EDIT: Solved it, actually not really... Turns out I was calling the function with commas on the list, rather than semicolons. Yep. So thanks!!

1

1 Answers

1
votes

It's hard to answer because I see so many problems with the code.

You say your function takes a list and a size, but the function you're defining here takes three parameters. It appears that the resultado parameter is an accumulated result. It's a little untidy to add this parameter to your outer function. Generally you'd want to add it to an internal function so the outer function matches your description (i.e., it takes two parameters).

Both of your patterns in the match statement assume that texto is non-null. This means your pattern isn't exhaustive, i.e., the function will fail if texto is null either initially or in a recursive call.

Your function uses a function named consderecha that's not defined anywhere. I'm going to assume it does a reverse cons, i.e., it adds a value to the end of a list. (This is not a good way to handle lists. Adding a value to the end of a list is slow.)

However, if you put aside all these issues, I don't see any problem with your recursion. When I try your function on a case where it's defined, it almost works. (It seems to be missing a value from the middle of the list.)

# cortar [1;2;3;4;5] 2 [];;
- : int list list = [[1; 2]; [4; 5]]

Possibly rather than not stopping, your function isn't starting. Maybe you forgot the third parameter?