sml

SML and functional coding style

你。 提交于 2019-12-07 03:37:26
问题 I'm start to learn Standard ML with Programming languages course. In the first homework, I try to write a function is_older that takes two dates and evaluates to true or false . It evaluates to true if the first argument is a date that comes before the second argument (If the two dates are the same, the result is false .). So I write the following code: fun is_older(first: int * int * int, second: int * int * int) = if(#1 first = #1 second andalso #2 first = #2 second andalso #3 first = #3

Passing command-line arguments to an SML script

*爱你&永不变心* 提交于 2019-12-07 02:51:55
问题 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? 回答1: 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: " ^

Nested case statements in SML

一笑奈何 提交于 2019-12-06 17:18:45
问题 This is more of a stylistic question than anything else. Given the following piece of code: case e1 of (* datatype type_of_e1 = p1 | p2 *) p1 => case e11 of (* datatype type_of_e11 = NONE | SOME int *) NONE => expr11 | SOME v => expr12 v | p2 => case e21 of (* datatype type_of_e21 = NONE | SOME string *) NONE => expr21 | SOME v => expr22 v Is there a way to resolve the types of rules don't agree error caused by trying to pattern match e11 to p2 , other than enclosing p1 's expression in

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

Merge infinite lists in SML

霸气de小男生 提交于 2019-12-06 09:19:40
问题 In SML I have created three infinite lists namely fibonacci , evenfib and oddfib . Now what I want to do is create a fourth list which will contain the first 10 numbers of evenfib and the first 10 numbers of oddfib and merge them into pairs of one evenfib and one oddfib using the zip function and create a fourth list. I have written a zip function as follows but it doesn't work. fun fib a b = CONS(a, fn () => fib b (a + b)); fun odd n = if ( n mod 2 = 1) then true else false; fun even n = if

SML syntactical restrictions within recursive bindings?

强颜欢笑 提交于 2019-12-06 07:18:47
There seems to be syntactical restrictions within SML's recursive bindings, which I'm unable to understand. What are these restrictions I'm not encountering in the second case (see source below) and I'm encountering when using a custom operator in the first case? Below is the case with which I encountered the issue. It fails when I want to use a custom operator, as explained in comments. Of the major SML implementations I'm testing SML sources with, only Poly/ML accepts it as valid, and all of MLton, ML Kit and HaMLet rejects it. Error messages are rather confusing to me. The clearest one to

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

SML How to check variable type?

此生再无相见时 提交于 2019-12-05 19:52:36
问题 Is there any way to check/test the type of a variable? I want to use it like this: if x = int then foo else if x = real then bar else if x = string then ... else ..... 回答1: ML languages are statically typed, so it's not possible for something to have different types at different times. x can't sometimes have type int and at other times have the type string . If you need behavior like this, the normal way to go about it is to wrap the value in a container that encodes type information, like:

SML - Creating dictionary that maps keys to values

╄→尐↘猪︶ㄣ 提交于 2019-12-05 18:36:58
I need to create a dictionary in sml, but I am having extreme difficulty with an insert function. type dict = string -> int option As an example, here is the empty dictionary: val empty : dict = fn key => NONE Here is my implementation of an insert function: fun insert (key,value) d = fn d => fn key => value But this is of the wrong type, what I need is insert : (string*int) -> dict -> dict. I've searched everything from lazy functions to implementing dictionaries. Any help or direction would be greatly appreciateds! If you are still confused on what I am trying to implement, I drafted up what