smlnj listdir problems

喜夏-厌秋 提交于 2019-12-25 17:06:23

问题


I am a newbie learning sml and the question I am given involves IO functions that I do not understand. Here are the 2 questions that I really need help with to get me started, please provide me with code and some explanation, I will be able to use trial and error with the code given for the other questions.

Q2) readlist(filename) which reads a list of filenames (each of which were produced by listdir in (Q1) and combines them into one large list. (reads from the text file in Q1 and then assigns the contents into 1 big list containing all the information)

Thing is, I only learned from the lecturer in school on the introduction section, there isn't even a system input or output example shown, not even the "use file" function is taught. if anyone that knows sml sees this, please help. Thanks to anyone who took the effort to help me.

Thanks for the reply, current I am using SMLNJ to try and do this. Basically, Q1 requires me to list the directory's files of the "directoryname" provided into a text file in "filename". The Q2 requires me to read from the "filename" text file and then place the contents into one large list.

BTW, if you people only kept seeing this post, please try and ask questions also. Currently i am stuck trying to read from the txt file and appending it to a list, I am able to do it for a single line but am now trying to do it for the whole file:

fun readlist(infile : string) =
let val ins = TextIO.openIn infile

fun listing() = 
TextIO.inputLine ins;

in listing()
end;
TextIO.closeIn;

回答1:


It is very hard for me to make out what questions you are trying to ask.

The functions you ask about are not part of the Standard Basis Library for ML. If you are supposed to write them, you are going to have a hard time without some kind of Posix module. You can tell your instructor I didn't care for this assignment.

Moscow ML contains a listDir function which is admirably simple:

- load "Mosml";
> val it = () : unit
- Mosml.listDir ".";
> val it = ["natural-semantics.djvu", "natural-semantics.pdf"] : string list
- 

To get more help, please be a little clearer what you are asking.


EDIT: Since it's a homework question I shouldn't just give you the answer, but some useful functions includeopenDir, readDir, and closeDir from the OS.Filesys structure. These will tell you what's in the directory. Then to read and write files you'll want TextIO.

You'll find the Standard Basis Library documentation indispensible.




回答2:


You sure i didn't teach u? u owe me one chicken pie.



来源:https://stackoverflow.com/questions/672065/smlnj-listdir-problems

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!