简体   繁体   中英

Why isn't this printing any new lines?

I wrote a function in F# for parsing a list of integer lists. I tested it by creating a function that was supposed to print out all the values in each list, starting a new line with the end of each list. The values were all correct but there were no new lines being printed. I tested it by taking input from the command prompt at first and then parsing it.

Here are my parsing functions:

let rec parseIntList (str : string) : int list =
  let capture      = Regex.Match(str, "\d+(;|])")
  if capture       = Match.Empty then [] else
    let init : int = capture.Index
    let fin : int  = init + capture.Length
    (int str.[init..(fin - 2)]) :: (parseIntList str.[fin..])

let rec parse2dIntList (str : string) : int list list =
  let capture = Regex.Match(str, "\[.{3,}](;|])")
  if capture  = Match.Empty then [] else
    let init : int = capture.Index
    let fin : int  = init + capture.Length
    (parseIntList str.[init..(fin - 2)]) :: (parse2dIntList str.[fin..])

Which weren't changed at all when I was working on this.

Here are my printing functions:

let rec printList (list : int list) : unit =
  for i in list do
    printf "%i " i
  printfn "%s" "" //printfn "" and printf "\n" didn't work either

let rec print2dList (list : int list list) : unit =
  match list with
    | []        -> ()
    | (x :: xs) -> printList x; print2dList xs

let _ = while true do

          let input  = System.Console.ReadLine()
          let output = parse2dIntList input
          if output = [] then printfn "Please give valid input"
          else print2dList output

The code above produced the correct values but didn't print new lines. However, when I tested it like this,

let _ = while true do
    print2dList [[1;2;3];[4;5;6];[7;8;9]]

It printed the correct values with new lines, many times. The immediate conclusion would be that my parsing function is parsing a 2d list to a 1d list. But I'm pretty confident that because of the way I've set the functions up, there would be a type error if that were true.

Are my regex incorrect? I tried escaping the closing square brackets, ie using \\] instead of ] , but it just messed everything up.

If you run parse2dIntList [[1;2;3];[4;5;6];[7;8;9]] you'll see that it's already not working, it returns a flat list.

So you can remove all the printing stuff from your question because it is clear that the problem is in the parsing. Including the printing functions in the debugging process just add more noise and makes it more complicated.

Now if you use an online regex tester, you'll see that the pattern \\[.{3,}](;|]) is not capturing sublists.

Try adjusting it, for instance if you use something like .+?(?=\\])\\]. it will work:

let rec parse2dIntList (str : string) : int list list =
  let capture = Regex.Match(str, ".+?(?=\])\].")
  if capture  = Match.Empty then [] else
    let init : int = capture.Index
    let fin : int  = init + capture.Length
    (parseIntList str.[init..(fin - 2)]) :: (parse2dIntList str.[fin..])

Test

parse2dIntList "[[1;2;3];[4;5;6];[7;8;9]]" ;;
val it : int list list = [[1; 2; 3]; [4; 5; 6]; [7; 8; 9]]

And of course, if you try all your printing stuff it will work now.

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