smlnj

Print int list in sml

时光毁灭记忆、已成空白 提交于 2019-12-06 15:46:12
Does there any function exist that directly prints the int list? I have to print int list for debugging purposes. I know that I can achieve this by writing my own functions but I want to know that is there any other method available? SML/NJ doesn't have as many features for pretty printing as some other implementations of SML but its PRINTCONTROL signature gives some flexibility. For example, with the default settings you have this: But if in the REPL you evaluate Control.Print.printLength := 500; and Control.Print.linewidth := 80; then: In Poly/ML there is a special function PolyML.print that

in smlnj how do you convert “string option” to “string”?

♀尐吖头ヾ 提交于 2019-12-05 23:08:12
Please help I have no idea how what a string option does. is it possible to convert string option to a string? As already pointed out you could use pattern matching to get the desired result. So, something like this: fun foo(NONE) = "" | foo(SOME a) = a; But you could spare the trouble and use Option.valOf function from SML library instead by just doing: Option.valOf(SOME "my string"); (Or just valOf(SOME "my string"); as newacct pointed out in the comments.) The "option" type in ML is like, say, Nullable in the .NET world. It is a discriminated union with two values, None and Some of 'a (for

How can I customize the SML/NJ interactive loop?

房东的猫 提交于 2019-12-05 20:13:23
I'm new to Standard ML and I'm trying to get my head around the SML/NJ runtime environment. I want to adapt it to my needs. Specifically, I want to: Use IntInf by default Prevent it from truncating strings and IntInf to 70 characters. Here's what I've found in my 8+ hours reading documentation and experimenting. I can overload IntInf on top of int with the command open IntInf; I can control how many characters in a string are displayed with the variable Control.Print.stringDepth. For example, this will let it display 1000 characters before truncating: Control.Print.stringDepth := 1000; How do

What do “continuations” mean in functional programming?(Specfically SML)

梦想的初衷 提交于 2019-12-05 14:08:32
I have read a lot about continuations and a very common definition I saw is, it returns the control state. I am taking a functional programming course taught in SML. Our professor defined continuations to be: "What keeps track of what we still have to do" ; "Gives us control of the call stack" A lot of his examples revolve around trees. Before this chapter, we did tail recursion. I understand that tail recursion lets go of the stack to hold the recursively called functions by having an additional argument to "build" up the answer. Reversing a list would be built in a new accumulator where we

Find if Duplicates Exist SML NJ

送分小仙女□ 提交于 2019-12-05 14:08:24
I want to write a single function that searches a list and finds if there are any duplicates values in this list. The function should return a boolean. Here is where I'm at, but this is not working... fun myFunc [] = true myFunc(x::xs) = if(x=myFunc(xs)) then false else myFunc(xs); [1,2,2,3,4,5,6] should return true [1,2,3,4,5,6,7] should return false [1,2,3,4,5,6,1] should return true thanks! As @Marcin said in the comment, an easy and efficient way is to use set for checking duplication. SML/NJ have many set structures available in Utility Library . Regarding your function, you cannot

Polymorphic function as return value and value restriction in SML

别来无恙 提交于 2019-12-05 13:10:36
Basically, I want to have a function to return a polymorphic function, some thing like this: fun foo () = fn x => x So the foo function takes in a value of type unit and returns a polymorphic identity function and the compiler is happy with that, it gives me: val foo = fn : unit -> 'a -> 'a but once I actually call the foo function, the return value is not what I expected val it = fn : ?.X1 -> ?.X2 Can't generalize because of value restriction it says, any help? thanks in advance For technical reasons, you are not allowed to generalize (i.e., make polymorphic) the results of a function call.

When to use semicolons in SML?

[亡魂溺海] 提交于 2019-12-05 12:19:38
问题 I know that semicolons are used as terminators in REPL. But I'm confused about when to use them in a source file. For example it is not necessary after val x = 1 . But if I omit it after use "foo.sml" , the compiler will complain about it. Then, what are the rules on using semicolons? 回答1: Semicolons are used for a number of syntactic entities in SML. They are normally used to create sequences of, e.g., expressions or declarations. Here's a link to the SML grammar: http://www.mpi-sws.org/

Passing command-line arguments to an SML script

微笑、不失礼 提交于 2019-12-05 07:03:47
How do I go about passing command-line arguments to an SML script? I'm aware that there is a CommandLine.arguments() function of the right type ( unit -> string list ), but invoking the interpreter like so: $ sml script_name.sml an_argument another_one doesn't give me anything. Pointers? seanmcl Try this. (* arg.sml *) val args = CommandLine.arguments() fun sum l = foldr op+ 0 (map (valOf o Int.fromString) l) val _ = print ("size: " ^ Int.toString (length args) ^ "\n") val _ = print ("sum: " ^ Int.toString (sum args) ^ "\n") val _ = OS.Process.exit(OS.Process.success) The exit is important,

BigInt for Standard ML/NJ

ぃ、小莉子 提交于 2019-12-04 09:03:16
Is there a Java BigInt equivalent for Standard ML? The normal int type throws an exception when it overflows. Yes, see the IntInf structure. The official SML'97 standard basis library introduces a zoo of structures like Int, IntInf, Int32, Int64, LargeInt etc. To actually use them in practice to make things work as expected, and make them work efficiently, you need to look closely at the SML implementation at hand. One family of implementations imitates the memory layout of C and Java, so Int32 will be really a 32bit machine word (but with overflow checking), and Int64 a 64bit machine word.

Suppress “val it” output in Standard ML

倖福魔咒の 提交于 2019-12-04 00:48:13
问题 I'm writing a "script" in Standard ML (SML/NJ) that sets up the interactive environment to my liking. The last thing the script does is print out a message indicating everything went smoothly. Essentially, the last line is this: print "SML is ready.\n"; When I run the script, all goes well but the SML interpreter displays the return value from the print function. SML is ready. val it = () : unit - Since I'm merely printing something to the screen, how can I suppress the "val it = () : unit"