I am new to SML(and functional programming in general) and if anyone could help me with two things I would be glad.
Firstly, I want to read from a file that has the form of a NxM grid where N,M are unknown and store them into an array, either 1d or 2d . What is a good way to do so? What I have done so far is read every line as a string and create a string list and then convert every line into a char list.And that brings me to the second question.
Given a char list list A how to create a char list B that is the concatenation of every element in A.I thought foldr could apply here but
foldr concat A
gives me a tycon mismatch error.
Thanks in advance!
Your first question is a bit too vague since a good answer would depend on the structure of the file, which you haven't given. In any event, it seems that you have an answer that would work as soon as your second question is answered.
concat
has type string list -> string
. You have the tycon mismatch since a char list
is not a string
.
Instead, the List structure has its own function named concat
. It is a polymorphic function of type 'a list list -> 'a list
meaning that it can take an arbitrary list of lists and flatten it into a single list. Thus the answer to your second question is simply:
List.concat A