smlnj

SMLNJ Insertion Sort Operator and Operand dont agree error

我的梦境 提交于 2019-12-12 04:03:02
问题 Im making an insertion sort code in SML, here it is fun compare(x:real, y:real, F) = F(x, y); fun isEqual(x:real, y:real) = ((x <= y) andalso (x >= y)); fun rinsert(x: real, [], F) = [x] |rinsert(x, (y::ys), F) = if isEqual(x, y) then rinsert (x, ys, F) else if compare(x, y, F) then x::y::ys else y::(rinsert (x, ys, F)); fun rinsort([], F) = [] |rinsort(x::xs, F) = rinsert(x, (rinsort(xs, F), F)); However, on running it i get this error val isEqual = fn : real * real -> bool val rinsert = fn

SML splitting string on first space

僤鯓⒐⒋嵵緔 提交于 2019-12-12 02:34:54
问题 As of right now I am reading in an entire input file using inputAll and then using String.tokens to split each word at every occurrence of space. val file = TextIO.openIn input val _input = TextIO.inputAll file val _ = TextIO.closeIn file String.tokens Char.isSpace _input Ex) "red blue green" would look like this ["red", "blue", "green"] However, now I would like to change it to only split the string at the first occurrence of a space char on each line. Ex) "red blue green" should look like [

VAL keyword within IF condition won't allow reassignment in SML

放肆的年华 提交于 2019-12-11 14:45:47
问题 Given this following code (which does not work): fun func() = val decimal = 0 (* the final result *) val multiple = 0 (* keeps track of multiples, eg. In XXV, X would be a multiple *) val current = 0 (* the digit currently being processed *) val top = 0 (* value of the last element in the list *) val last_add = 0 (* the last digit that wasn't a multiple, or subtraction operation *) val last_sub = 0 val problem = 0 (* if value is 1 then there is a problem with the input *) val myList = [1,2,3

Recursive function to pretty-print the elements of a list

≯℡__Kan透↙ 提交于 2019-12-11 11:05:55
问题 I need to write a function which takes input of type (int * int) list and prints pairs of integers. This function should leverage another function printGenList (takes a function f and a list l and applies f to every element of list recursively) whose code I have written like this - fun printGenList f l = if NULL l then () else ( (f (HD l) ); printGenList (f) (TL l) ); and provide an anonymous function (the fn … => … construct) that will do the appropriate pretty printing. 回答1: The type

How to move the cursor in SML/NJ's REPL in terminal on Mac?

守給你的承諾、 提交于 2019-12-11 07:26:36
问题 In the terminal of Mac OSX, I open SML , if I type something wrong, I wish to move my cursor to that place to modify something or add/delete something, but once I hit <- (the left arrow ) on the keyboard, the REPL gives me Yes, that ^[[D thing. So how do I move the cursor? 回答1: Yes, the REPL of SML/NJ does not support arrow keys. You can use the rlwrap tool to fix this. That allows you to use the left and right arrow keys to navigate within a line and the up and down arrow keys to recall

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 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.

SMLNJ want to remove “val it = () : unit” from every print statement execution

余生长醉 提交于 2019-12-10 21:07:46
问题 I am writing sml programs which run on SML/NJ and MLton (not interactive). When I use print statements in the the sml file, SML/NJ always adds val it = () : unit to the output, which clutters up the output. MLton does not do this. Is there a way to remove this output? I have tried CM_VERBOSE=false, which did not help. Running SML/NJ v110.73. 回答1: Without examples of the code that produces this, it is a bit hard to help, however it seems that your "issues" are somewhat related to this question

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)

SML function on record list

最后都变了- 提交于 2019-12-10 16:21:36
问题 I'm trying to declare a function that takes a list of records inside a tuple as an argument but the syntax is not as intuitive as I would have liked. Here's what I'm trying to do: type Player = {id:int, privateStack:int list}; fun foo(({id, x::xs}:Player)::players, ...) = (* wrong syntax *) (* do something *) 回答1: Pattern matching requires binding record fields to some values so you have to use explicit record syntax. Therefore, fun foo(({id = id, privateStack = x::xs})::players, ...) = (* do