sml

building a lexical analyser using ml-lex

半腔热情 提交于 2019-12-04 11:52:30
I need to create a new instance of a lexer tied to the standard input stream. However, when I type in val lexer = makeLexer( fn n => inputLine( stdIn ) ); I get an error that I don't understand: stdIn:1.5-11.13 Error: operator and operand don't agree [tycon mismatch] operator domain: int -> string operand: int -> string option in expression: ( makeLexer is a function name present in my source code) inputLine returns a string option , and my guess is a string is expected. What you want to do is either have makeLexer take a string option , like so: fun makeLexer NONE = <whatever you want to do

SML: What's the difference between using abstype and using a signature to hide the implementation of a structure?

冷暖自知 提交于 2019-12-04 10:13:55
问题 I've done a little work in SML in the past, but I'm now starting to get to the more interesting parts. Using the abstype...with...end construct, I can make things but keep their implementation details hidden. I can also create a signature of the thing I want to make, and use the :> operator to make a structure adhering to that signature that keeps the implementation details hidden. Aren't signatures/structures just a more general version of abstypes? What can I do with abstypes that I can't

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.

Lazy datatypes in Objective C

放肆的年华 提交于 2019-12-04 07:50:55
In SML, the following is possible for modelling lazy programming, // Have a datatype to wrap a computation datatype 'a susp = Susp of (unit -> 'a) // A function to hold the computation fun delay(f ) = Susp(f) I know that closures can be written using Blocks , int multiplier = 7; int (^myBlock)(int) = ^(int num) { return num * multiplier; }; So I think I can use it as a function argument. The next step would be how to use functions with no real arguments ( unit value e.g. in SML fn () => ) and creating lazy datatypes as the one above. Is this possible or should I be pursuing a different more

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"

When to use semicolons in SML?

冷暖自知 提交于 2019-12-03 23:36:56
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? 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/~rossberg/sml.html In your case, you are interested in the semicolon for declarations (the dec class). Note that

How do I install a working version of Standard ML on Mac?

我是研究僧i 提交于 2019-12-03 22:08:11
问题 I'm using Mac OSX 10.7.5 and I can't seem to get download a working Standard ML compiler with a REPL available. Is this supposed to be so difficult? Is there a best ML that I should be downloading. I've tried SML/NJ and MLton to no avail. 回答1: I did the following: --download appropriate(for your operating system) .dmg file from http://www.smlnj.org/dist/working/110.75/ --in your ~/.bash_profile: export PATH="$PATH:/usr/local/smlnj-110.75/bin" --run your bash_profile by doing source .bash

Could I ask for physical analogies or metaphors for recursion?

杀马特。学长 韩版系。学妹 提交于 2019-12-03 18:03:59
问题 I am suddenly in a recursive language class (sml) and recursion is not yet physically sensible for me. I'm thinking about the way a floor of square tiles is sometimes a model or metaphor for integer multiplication, or Cuisenaire Rods are a model or analogue for addition and subtraction. Does anyone have any such models you could share? 回答1: Imagine you're a real life magician, and can make a copy of yourself. You create your double a step closer to the goal and give him (or her) the same

What is a good data structure to represent an undirected graph?

旧时模样 提交于 2019-12-03 15:49:15
I need to construct an undirected graph. I don't need it to do anything too fancy, but ideally it would work like this: structure UDG = UndirectedGraph val g = UDG.empty val g = UDG.addEdges(g, n1, [n2, n4, n7]) (* n1 is connected to n2, n4, and n7 *) val g = UDG.addEdge(g, n2, n3) UDG.connected(g, n2) (* returns [n1, n3] *) Is there a good data structure in SML/NJ to model these relationships? Should I just roll my own? Updates I've gone ahead and tried rolling my own, but I get a type mismatch error when I try to test it. My experience with SML structures and functors is pretty basic, so I

Curried anonymous function in SML

六月ゝ 毕业季﹏ 提交于 2019-12-03 15:11:46
问题 I have the function below and it works: (fn x => x * 2) 2; but this one doesn't work: (fn x y => x + y ) 2 3; Can anyone tell me why? Or give me some hint to get it to work? 回答1: (fn x => fn y => x+y) 2 3; works. fn simply doesn't have the same syntactic sugar to define curried functions that fun has. 回答2: In Standard ML, a function can have only one argument , so use (fn (x,y) => x + y) (2,3) and the type is fn: int * int -> int in this time (x,y) and (2,3) is a list structure, 回答3: The