问题
I'm completely new to SML and I have no idea how to work with anything related to it. I am supposed to use the SMLNJ compiler and I'm currently coding using Notepad++. But how do I compile the program exactly? Do I copy and paste the code in the SMLNJ command line thing? Or is there an environment for SMLNJ I can actually code in and compile my code? PLEASE HELP!
回答1:
If by "compile" you mean "compile to a stand-alone executable" -- don't worry about that when first learning the language as a full answer is somewhat involved. In principle it is possible, though there seems to be practical issues (as this excellent answer details).
On the other hand, SML/NJ has a command-line based REPL (Read-Evaluate-Print-Loop) which actually is an compiler. It is an incremental compiler -- meaning that it compiles newly defined functions in the context of currently defined functions. At the very first you will be experimenting with short snippets typed directly into the REPL. Sooner rather than later you will want to write the code in something like NotePad++ . Just write the file, save it with a .sml extension, then in the REPL type
use "filename";
and it loads and compiles your definitions.
A trick that I sometimes use is the following. On the top of my files I have a comment like this:
(* val _ = (use "C:/Programs/sets.sml", OS.Process.system "cls"); *)
Here sets.sml
just happens to be an SML file I have on my machine. When I get done editing the file I save it and then copy the insides of the comment (beginning with val
and extending to the semicolon) to the clipboard and then paste it into an open SML REPL (using the edit menu for the command window which pops up when you hit the icon in the upper left hand corner of the command window). This loads the definitions and presents you with a cleared REPL for experimenting with your definitions.
Notepad++ seems to lack a syntax definition file for SML (although it has one for F#, which might be close enough). Personally, I do most of my SML using Textpad. This isn't open source but is reasonably priced nagware which is roughly comparable to Notepad++. From their website you can download a useful SML syntax-definition file which does a nice job of logically highlighting your code. I even created a Textpad tool (which for unknown reasons only works sporadically) for automatically saving the file, opening SML, and invoking use
on the file name.
来源:https://stackoverflow.com/questions/36125649/how-to-compile-sml-using-smlnj-while-the-code-is-in-notepad