简体   繁体   中英

OCaml Stackoverflow Error when printing list

I am trying to print a list in OCaml. My list consists of elements of a structure I defined called Elem. Somewhere I am getting a stack overflow error and I cannot figure it out.

let rec pretty_print tr =
    let rec trc_list_to_str trc out = match trc with
      | [] -> out
      | t::ts -> trc_list_to_str ts (out ^ pretty_print t)
    in "[" ^ pretty_print tr ^ "]"

  and print_single_trace (st: Ast.Elem.t) =
    PrettyPrinting.print_identifier st.label ^
    PrettyPrinting.print_literal st.payload

In your current code, the trc_list_to_str function is unused, the pretty_print_trace function can thus be read as

let rec pretty_print_trace tr =
  in "<Trace>\n" ^ pretty_print_trace tr ^ "\n</Trace>\n"

Therefore, the pretty_print_trace functions calls itself infinitely without ever looking at its argument trying to compute <Trace><Trace><Trace><Trace> … × ∞ </Trace></Trace></Trace></Trace>

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