ml

Standard ml loop troubles

吃可爱长大的小学妹 提交于 2019-12-11 05:21:30
问题 I am setting up a function that will simulate a loop until a condition is met. My overall plan is to use recursion but I am trying to get the basics down first. I got a basic function working using an If statement that is seeing what the value of X is. I plan to use recursion to use X as an counter but I will get to that later. My main concern right now is, it seems I can only do 1 command after the "then" statement. fun whileloop (x,a) = if (x<4) then a+1 else a; So this function works

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

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

Writing an interpreter in OCaml [closed]

隐身守侯 提交于 2019-12-10 20:22:03
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 3 years ago . I'm following a course in my University that ask me to write an interpreter in OCaml of a language starting from its operational semantics. Unfortunately, they didn't give us much resources from which we can learn about it, except from the lesson's slides. Can someone suggest me some book or some website where I

How can I parse String to (int * int) tuple in SML?

做~自己de王妃 提交于 2019-12-10 19:43:17
问题 I have a string something like this "3,4\r\n" , and I want to convert them into a tuple i.e (3,4) . How can we achieve this in SML ? The reason why I'm getting a string value is because I'm reading a file which returns strings like that. 回答1: You need a simple parser to achieve that. An appropriate function to parse integers is already available in the library as Int.scan (along with friends for other types), but you have to write the rest yourself. For example: (* scanLine : (char, 's)

ml function of type fn : 'a -> 'b

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-10 18:19:32
问题 The function: fn : 'a -> 'b Now, are there any functions which can be defined and have this type? 回答1: There are two possible implementations for that function signature in Standard ML. One employs exceptions, the other recursion: val raises : 'a -> 'b = fn a => raise Fail "some error"; (* Infinite looping; satisfies the type signature, *) (* but won't ever produce anything. *) val rec loops : 'a -> 'b = fn a => loops a; The first solution may be useful for defining a helper function, say bug

ML assignment operation

时光怂恿深爱的人放手 提交于 2019-12-08 12:38:39
问题 everyone, what is difference between the following assignments in ML, val n = 5; and n := 1; 回答1: The former is a declaration of a new, immutable variable. The latter is how you re-assign the value of a reference cell. 来源: https://stackoverflow.com/questions/5168889/ml-assignment-operation

File seeking with SML Basis

北城余情 提交于 2019-12-07 05:38:47
问题 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. 回答1: 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 ,

Better to use “and” or “in” when chaining “let” statements?

≯℡__Kan透↙ 提交于 2019-12-05 12:44:41
问题 I realize this is probably a silly question, but... If I'm chaining a bunch of let statements which do not need to know each other's values, is it better to use and or in ? For example, which of these is preferable, if any: let a = "foo" and b = "bar" and c = "baz" in (* etc. *) or let a = "foo" in let b = "bar" in let c = "baz" in (* etc. *) My intuition tells me the former ought to be "better" (by a very petty definition of "better") because it creates the minimum number of scopes necessary

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/