I am writing sml programs which run on SML/NJ and MLton (not interactive). When I use print statements in the the sml file, SML/NJ always adds
val it = () : unit
to the output, which clutters up the output. MLton does not do this.
Is there a way to remove this output? I have tried CM_VERBOSE=false, which did not help.
Running SML/NJ v110.73.
Without examples of the code that produces this, it is a bit hard to help, however it seems that your "issues" are somewhat related to this question.
In summary, remember to bind all result values to something, such that the it variable don't get assigned to the result:
val _ = print "fooo"
Related
I am new to the SML language and I want to do this:
to ask a person "what is your full name?"
to get answer from user's keyboard
and to write "your full name is"+name (his or her name that answered)
This answer has three parts. The first part answers your only question. The second part answers a question you seem to be fishing for without asking it, and the third part addresses how to find answers to questions by your own means.
How to read string from user keyboard in SML language?
You use TextIO.inputLine TextIO.stdIn:
- val wat = TextIO.inputLine TextIO.stdIn;
Hello, World!
val wat = SOME "Hello, World!\n" : string option
Notice that this actually doesn't work in my Poly/ML REPL (aka "the top-level" or "the prompt"), but it does work in both of my SML/NJ and Moscow ML REPLs, but it will probably work from within an .sml file that you compile or run.
Notice also that you'll get the linebreak as well. Maybe you don't want that.
Although you didn't ask, you can print a string in much the same way:
- TextIO.output (TextIO.stdOut, Option.valOf wat);
Hello, World!
val it = () : unit
The catch here is that when you read a line from the user, you might not get anything, which results in the value NONE rather than an empty string (what you'd expect in Python) or an exception (what you'd expect in Java). And when you get something, to be able differentiate between getting an empty response and not getting a response, you get SOME "...".
If you don't care about this distinction, you can also make life easier and build some helper functions:
(* helper functions *)
fun getLine () =
Option.getOpt (TextIO.inputLine TextIO.stdIn, "")
fun putLine s =
TextIO.output (TextIO.stdOut, s)
(* examples of use *)
val wat = getLine ()
val _ = putLine (wat ^ "!!!")
When you get around to want to ask similar questions, you can find some of these answers yourself by typing open TextIO; Enter in your REPL. This tells you what functions are available inside the TextIO module, but not necessarily what they do. So what you can do is also look up the documentation by googling around:
https://smlfamily.github.io/Basis/text-io.html
I am using the deepdiff function to find the difference between 2 dictionaries, which gives the output as: A = {'dictionary_item_added': set(["root['mismatched_element']"])}. How to print just 'mismatched_element'?
Try this:
set_item = A['dictionary_item_added'].pop()
print set_item[set_item.find("['")+2 : set_item.find("']")]
The first line gets the element from the set, the second removes the [] and everything around them, and prints.
This code does the specific task you asked for, but it's hard to generalize the solution without a more generalized question..
I currently have a lot of print statements in SML code, and I'm traversing a very large tree, so it takes a while for all the print statements to be printed, but right now I don't want to see any print statements and just want to see it run as fast as possible. But I don't want to comment all the prints because I later need them again to debug something else.
So I just want to be able to temporarily disable them for this run of the code.
I'm using the SML/NJ compiler.
As the first line in your code put the definition
fun print x = ();
Then -- your code will still work but the prints will do nothing.
Delete that line when you want to re-enable print.
I'd like to print a string, either const char* or std::string, using lldb so that it is human readable. Most importantly, \n's would be printed as a newline. Does anyone know how to do this? I tried the advice given for gdb in this post, however it doesn't seem to work with lldb.
Edit: I'm aware that you can issue the print myString command to print the string, however it doesn't format newline characters (at least not by default):
Most of the time you want to see the literal contents of your strings, so the default lldb behavior for print is correct. However, it would be useful to have a format option to "render" the output in the same way the standard libraries would do a string. That's basically what the gdb "printf" command is. Please file a bug with the lldb.llvm.org bug reporter asking for this.
Just like with gdb, you can get the standard library to render the text for you:
(lldb) expr (void) printf("Some text\nMore text\nEven more text\n")
Some text
More text
Even more text
(lldb)
I cast it to void in this case because I didn't care about the return value, and it makes it harder to see the text.
As was pointed out in the post you referred to, if your output is not going to a terminal somewhere, that isn't helpful, so some explicit "render the output" option would be a good idea as well. But that should only happen if you attach to, rather than run, your program in the debugger.
Is it possible, and if so, how would you make error output include the last couple lines of code? I know about GNU g++'s support for __LINE__ and __FUNC__ but those only give the line number as an int and the function name as a string - I'd like to dump the function that failed's code or at least the last several commands executed by the program.
I was thinking maybe write a m4 script that would parse through the code base and inject string structures of the functions they're put in (excluding themselves, of course).
Any other/better ideas?
Do not change your code at all. Use __LINE__ and __FUNC__ and post process the error output. That is, run your program as my-prog > $(tty) 2>&1 | post-process where post-process has access to your code base and can generate the desired text. You might want to modify the code slightly by adding tags to make it easier for post-process to find the function name and line number.