0

I am trying to convert the following variable:

- final "in1.txt";
val it = [|[#"S",#".",#".",#"."],[#".",#".",#".",#"."],[#"W",#".",#"X",#"W"],
[#".",#".",#"X",#"E"]|] : char list array

from 'char list array' to 'char array array' in SMLNJ. The only reason I want to do this is because I need to be able to randomly iterate through this data, to perform a Dijkstra-like algorithm for a school project (if there 's a more efficient way to make this data iteratable, I am all ears). Is there a way to do this? The function that reads the input file and returns the above is this (I found it in Stack Overflow):

fun linelist file =
    let
        open Char
        open String
        open List
        val instr = TextIO.openIn file
        val str   = TextIO.inputAll instr
    in
        tokens isSpace str
        before
        TextIO.closeIn instr
    end

fun final file =
    let
        fun getsudo file = map explode (linelist file)
    in
        Array.fromList (getsudo file)
    end

and the input files that need to be processed are like the one that follows:

S...
....
W.XW
..XE
Zehanort
  • 458
  • 2
  • 10

1 Answers1

0

You might want to try a different way to read this (space delivery) map (to help Lakis -- yes I am a classmate of yours).

fun parse file =
        let
            fun next_String input = (TextIO.inputAll input) 
            val stream = TextIO.openIn file
            val a = next_String stream
            val lista = explode(a)
        in
            lista
        end  

Parse is a function that gets all the contents from a text file and saves them in string a. Then, the function explode (function of String Signature of the SML NJ) creates a list, called lista. The elements of the list are the characters of the string a in the same order.

Then, you can create another function that saves the contents of the list to an array. Each row of the array will contain the characters of the list until #"\n" comes up.

g_url
  • 1