sml

How to get a string from TextIO in sml/ml?

大兔子大兔子 提交于 2019-12-11 04:37:19
问题 I'm trying to read text from a file in SML. Eventually, I want a list of individual words; however, I'm struggling at how to convert between a TextIO.elem to a string . For example, if I write the following code it returns a TextIO.elem but I don't know how to convert it to a string so that I can concat it with another string TextIO.input1 inStream 回答1: TextIO.elem is just a synonym for char , so you can use the str function to convert it to a string. But as I replied to elsewhere, I suggest

SML type inference by hand

淺唱寂寞╮ 提交于 2019-12-11 04:14:41
问题 Hello I am getting ready for my finals and there is always ml type inference on the exams . i.e. we are asked to write the type of a function like this one : fun ugh x y z = x z (y z); val ugh = fn : ('a -> 'b -> 'c) -> ('a -> 'b) -> 'a -> 'c or fun doh x y z = z (x y ) (y + 1); val doh = fn : (int -> 'a) -> int -> ('a -> int -> 'b) -> 'b However all the ways I am trying to infer the type I always get it wrong . Although there are examples online there are no examples for functions like that

Structuring a Library in SML

喜欢而已 提交于 2019-12-11 03:54:48
问题 I'm currently building a testing library in Standard ML (using Poly/ML as the interpreter). I have the following directory structure: project/a.sml project/src/b.sml project/src/c.sml ... Where a.sml is just a bunch of calls to use use "src/b.sml" use "src/c.sml" ... b.sml , c.sml etc. are all structure definitions like this structure ComponentX struct ... end which form nice, logically separated components of the library. I sometimes also create one module in one file, and then introduce a

Concatenating strings with foldr in SML

痴心易碎 提交于 2019-12-11 03:03:54
问题 I'm trying to declare a function, string list -> string, that with the input for instance ["Chicago","city","USA"] should return "Chicago city USA" . What I did so far was this: fun gather ts = foldr op ^ "" ts; This seems to be somewhat along the lines, however the problem is, I would like to include the spaces between the words, as this function would return "ChigagocityUSA" . 回答1: Yes, the problem is that ^ is a function that for two strings "foo" and "bar" returns "foobar", although you

Correct functional implementation on Binomial Heap

牧云@^-^@ 提交于 2019-12-11 02:17:07
问题 I am reading Binomial Heap in Purely Functional Data Structures. The implementation of insTree function confused me quite much. Here are the set of codes datatype Tree = Node of int * Elem.T * Tree list fun link (t1 as Node (r, x1, c1), t2 as Node (_, x2, c2)) = if Elem.leq (x1, x2) then Node (r+1, x1, t2::c1) else Node (r+1, x2, t1::c2) fun rank (Node (r, x, c)) = r fun insTree (t, []) = [t] | insTree (t, ts as t' :: ts') = if rank t < rank t' then t::ts else insTree (link (t, t'), ts') My

Structural comparison in Standard ML

自古美人都是妖i 提交于 2019-12-11 02:15:59
问题 I can't seem to find reference on why does this not work: - (2000,1)<(2000,1); stdIn:18.1-18.18 Error: operator and operand don't agree [overload] operator domain: 'Z * 'Z operand: (int * int) * (int * int) in expression: (2000,1) < (2000,1) Does Standard ML support structural comparison? 回答1: The short answer: Only for equality. The strictly less than operator (<) in the top level environment is as the other comparison operators a bit "special". They are "special" in the way that they are

Standard ML Syntax

一曲冷凌霜 提交于 2019-12-11 02:08:40
问题 I'm new to Standard ML and trying to write the following code fun whilestat test stmt1 = (fn x => if (test x) then (stmt1 x;whilestat test stmt1 ) else (x) ); The issue is that it gives me the following error w.sml:21.6-22.82 Error: right-hand-side of clause doesn't agree with function result type [circularity] expression: ('Z -> 'Y) -> 'Z -> 'Z result type: ('Z -> 'Y) -> 'Z in declaration: whilestat2 = (fn arg => (fn <pat> => <exp>)) uncaught exception Error raised at: ../compiler/TopLevel

Conque SML backspace

非 Y 不嫁゛ 提交于 2019-12-11 01:17:56
问题 I have a problem with SML while running it in vim with conque. If I run the interpreter from the terminal then everything is ok. If I will run it using Conque it will be ok except for the fact that if I use the cursor it will write "^[[C" or "^[[A" or so on. ("^H" for backspace). What can I do? 回答1: You are probably talking about SML/NJ, in which case the REPL famously lacks readline support (why? I really don't know). I'm guessing those funny keystrokes you're seeing generated are the result

How to compile and execute a stand-alone SML-NJ executable

我只是一个虾纸丫 提交于 2019-12-11 01:14:32
问题 I have seen one other answer link but what I don't understand is what is basis.cm and what's it's use? 回答1: You are asking two questions. What is basis.cm and what's it's use? This is the Basis library. It allows the use of built-in functions. How to compile and execute a stand-alone SML-NJ executable Assuming you followed Jesper Reenberg's tutorial on how to execute a heap image, the next thing you need in order to have SML/NJ produce a stand-alone executable is to convert this heap image.

I want to split a list into a tupple of odd and even elements

a 夏天 提交于 2019-12-10 21:57:51
问题 Hi all im new to programming and im doing a problem for learning and enjoyment. Im a bit stuck at this point.. The problem is from Introduction to Programming using Sml 5.9 I want to split a list of [x1, x2, x3, ... ,xn] = ([x1, x3,....], [x2, x4,...]) This is what I have made so far: fun split [] = [] | split (x1::x2::x3::x4::xs) = ([x1, x3], [x2, x4])::split xs val test1split = split [1, 1, 2, 3]; From this I get: [([1, 2], [1, 3])] .... (I want a tuple with splitting list and not this