简体   繁体   中英

For loop in Ocaml raises syntax error at end of the script

I'm really not used to functional languages... so I'm really struggling to learn and use Ocaml to a class project in my University... I'd really use some help here... Thats my issue: I'm trying to work on a mini compiler using Ocaml, so I'm trying to make an icrement like i++ in C++, by adding 1 on the element on my int list "lista_valores", that has the same index of the variable name in string list "lista_variaveis". But for some reason, Ocaml keeps on raising syntax error at the end of my code, after adding a For loop to make the variable increment. Take a look:

    let rec print_list = function 
[] -> ()
| e::l -> print_int e ; print_string " " ; print_list l



let pega_indice = fun s lista resposta ->
    let tamanho_lista = List.length lista in
    let tamanho_string = String.length s in
    let char = ref "0" in
    let existe = ref "0" in
    let condicao = ref 0 in
    try
    for i = 0 to (tamanho_lista-1) do
        let elemento = (List.nth lista i) in
        let tamanho_elemento = (String.length elemento) in
        if tamanho_elemento = tamanho_string then
      for j = 0 to (tamanho_elemento - 1) do            
        char := String.make 1 (String.get s j);
        existe := String.make 1 (String.get elemento j);
            condicao := if char = existe then
            (!condicao + 1)
            else
            0;      
      done;
      resposta := if !condicao = tamanho_elemento
      then i
        else -1;
        if !resposta = i then raise Exit;
    done;
    false
  with Exit -> true;;

let lista_variaveis = ref ["var1"; "var2"];;
let append_item lst a = lst @ [a];;
let lista_valores = ref [1; 5];;
let lista_novos_valores = [];;
let tamanho_listas = List.length !lista_valores;;
let resposta = ref 12314;;
let elemento = 0;;
print_endline("Digite a variavel:");;
let input = read_line();;
pega_indice input !lista_variaveis resposta;;
print_endline("indice da variavel:");;
print_endline(string_of_int !resposta);;
print_endline("incrementando...");;

for i = 0 to (tamanho_listas-1) do 
    begin
    if (i = !resposta) then lista_novos_valores@[(List.nth !lista_valores i) +1] else lista_novos_valores@[List.nth !lista_valores i];
    end;

print_list lista_novos_valores;;

The print screen I took of the error: Error raised at the end image 在此处输入图片说明

Thanks in advace for everyone interested in trying to help!

在你的代码你到底有do没有匹配done

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM