sml

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/

File seeking with SML Basis

和自甴很熟 提交于 2019-12-05 11:10:55
Is there a way, using the SML Basis library, to open a file at a specific position? That is, use an operating system call to change the position, rather than scan through the file and throw away the data. This is tricky. Unfortunately, seeking isn't directly supported. Moreover, file positions are only transparent for binary files, i.e., those that you have opened with the BinIO structure [1]. For this structure, the corresponding type BinIO.StreamIO.pos is defined to be Position.int , which is some integer type. However, in an SML system that supports the complete I/O stack from the standard

SML and functional coding style

五迷三道 提交于 2019-12-05 09:35:38
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 second) then false else if (#1 first < #1 second) then true else if (#1 first = #1 second andalso #2

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,

Nested case statements in SML

强颜欢笑 提交于 2019-12-04 23:00:44
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 parenthesis? The p2 pattern has another case statement, to avoid the 'just switch the patterns' answer ;-).

Case Statements and Pattern Matching

孤街浪徒 提交于 2019-12-04 21:31:24
问题 I'm coding in SML for an assignment and I've done a few practice problems and I feel like I'm missing something- I feel like I'm using too many case statements. Here's what I'm doing and the problem statements for what I'm having trouble with.: Write a function all_except_option, which takes a string and a string list. Return NONE if the string is not in the list, else return SOME lst where lst is like the argument list except the string is not in it. fun all_except_option(str : string, lst :

Merge infinite lists in SML

末鹿安然 提交于 2019-12-04 16:10:32
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 (n mod 2 = 0) then true else false; val fibs = fib 0 1; fun evenfibs l = FILTER even l; fun oddfibs l =