Write word in succession until the end of the cycle

Asked

Viewed 31 times

0

let palavra="1234567";;
let tamanho=String.length palavra;;
for i = 0 to 6 do
  print_string String.sub palavra i (tamanho-i);
  print_string "\n";
done;;

I tried to execute the code, but it won’t return what was expected. How do I print successive words:

1234567

234567

34567

4567

1 answer

0

Only parentheses are missing.

# print_string String.sub palavra i (tamanho-i);;
Error: This function has type string -> unit
       It is applied to too many arguments; maybe you forgot a `;'.

You need parentheses around the argument of print_string.

let palavra="1234567";;
let tamanho=String.length palavra;;
for i = 0 to 6 do
  print_string (String.sub palavra i (tamanho-i));
  print_string "\n";
done;;

Instead of print_string "\n", is easier to use print_endline. To make code more general, use length as the cycle terminal.

for i = 0 to tamanho do
  print_endline (String.sub palavra i (tamanho-i));
done;;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.