sml

Reference type for SML

北城余情 提交于 2019-12-12 04:39:57
问题 I have two refs in SML: val a = ref 5; val b = ref 6; Currently a = b evaluates to false. Is it possible to somehow assign variable b so that a = b evaluates to true? Thanks 回答1: No. It's not, they are pointers to different object. Technically you could do val a = ref 5; val b = a; That would work. 来源: https://stackoverflow.com/questions/22235507/reference-type-for-sml

Write command-line arguments to file in SML

独自空忆成欢 提交于 2019-12-12 04:12:22
问题 I am trying to write the command line arguments from my SML program into a file, each on a separate line. If I were to run sml main.sml a b c easy as 1 2 3 on the command line, the desired output would be to have a file with the contents: a b c easy as 1 2 3 However, I am getting the following output from SML : $ sml main.sml a b c easy as 1 2 3 val filePath = "/Users/Josue/Desktop/espi9890.txt" : string val args = ["a","b","c","easy","as","1","2","3"] : string list main.sml:4.21 Error:

remove elements from a list in ml

本小妞迷上赌 提交于 2019-12-12 04:05:04
问题 I´m trying to write a function that produces a new list containing the given list without the element x. Moscow ML says that some cases are unused in this match. fun delete (x,list) = delete(x,[]) |delete(x,(first::rest)) = if first = x then delete(x,rest) else first::delete(x,rest) 回答1: Here's how I'd do it on Standard ML:: fun delete (item, list) = case list of []=>[] | xs::ys => if item = xs then delete(item,ys) else xs::delete(item,ys) Without using cases: fun delete (item, list) = List

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

Modifying tuples after counting a tree in SML

久未见 提交于 2019-12-12 02:47:01
问题 I have a question I'm working on, where I have to traverse a tree once and count how many children each node has. There are two parts to this, the datatype, and the function itself. Datatype The datatype requires that the internal nodes store a value of any type and have anywhere from 1-3 children. The leaves themselves store either a real number or a string list. datatype leaf = Slist of string list | Real of real; datatype tree = Empty | Leaf of leaf | Node of leaf * 'a tree | Node of leaf

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 [

char list list -> bool

烂漫一生 提交于 2019-12-12 01:34:26
问题 Does anyone know how to declare a function rscheck : char list list -> bool , that checks, if the number 1-9 are used once in every row and every list... so it returns true? like in a sudoku game.. Thnx. 回答1: It could be done like this (also there is a better way for sure, just some training for me): http://pastebin.com/ViJM48LV 来源: https://stackoverflow.com/questions/3949072/char-list-list-bool

how to install SML-New jersey .93 version?

核能气质少年 提交于 2019-12-11 21:05:57
问题 I am trying to find installer for sml nj .93 version,and also how to install it .I have been advised to use this version rather than sml nj 110 version 回答1: SMLNJ version 0.93 was released in 1993. It is available for historical purposes. SML/NJ 0.93 has been tested on these machine/OS combinations: Sun M680x0/SunOS (x >= 2) HP M680x0/HPUX[6,7,8] ** NeXT M68040/NeXTStep[2.0,3.0] Apple M680x0/AUX[3.0] Apple M680x0/MacOS Sun SPARCstation/SunOS[4.1.1, 4.1.3] DEC MIPS/ULTRIX[UWS4.2] DEC Mach MIPS

How to do pattern matching on string in SML?

此生再无相见时 提交于 2019-12-11 20:42:30
问题 for example, fun f #"a"::_ = "first character is a" But this does not work in sml. Is there anyway I can do pattern matching on string without turning it to a char list? 回答1: Your code doesn't work because you forget to include the bracket, so it should be something like this: fun f (#"a"::_) = "first character is a"; If you want to do a pattern matching on string, you can use substring directly. In this case, it can be: fun f (str) = if substring(str, 0, 1) = "a" then "first character is a"

SML function that takes a filename and a list

允我心安 提交于 2019-12-11 17:11:47
问题 I want to write a function that takes a filename as a string and a list of pairs of characters. This function must open the named file, read the contents of the file, and echo the characters to the screen. Any occurrence of a character in the first position of a pair must be echoed as the character in the second position of the pair. For example, an invocation such as fileSubst "inputFile" [(#"a", #"b"), (#"b", #"z")] will echo the contents of inputFile with all occurrences of the character a