sml

SML: Remove the entry from the List

一世执手 提交于 2020-01-15 11:14:26
问题 How can I delete the element elem in list L ? If the list does not contain elem, then the function should return the list unchanged. For instance: L = [1, 3, 4, 0, 5, 7] elem = 5 So far I have the following function: fun removeElem elem myList[] = myList | removeElem (myList::tl) = if mem myList elem then rm elem myList[] else removeElem elem tl 回答1: You can turn the question around and ask how to keep only those items not equal to elem . This fits cleanly with filter : fun removeElem elem

SML: Remove the entry from the List

别来无恙 提交于 2020-01-15 11:09:02
问题 How can I delete the element elem in list L ? If the list does not contain elem, then the function should return the list unchanged. For instance: L = [1, 3, 4, 0, 5, 7] elem = 5 So far I have the following function: fun removeElem elem myList[] = myList | removeElem (myList::tl) = if mem myList elem then rm elem myList[] else removeElem elem tl 回答1: You can turn the question around and ask how to keep only those items not equal to elem . This fits cleanly with filter : fun removeElem elem

Writing multiple functions in SML - Sequential Composition

依然范特西╮ 提交于 2020-01-06 08:23:55
问题 I would like to understand how sequential composition works much better than I do now in SML. I have to write a program that takes a list of integers and moves the integer at index zero to the last index in the list. ie. [4, 5, 6] -> [5, 6, 4]. The code I have right now is: - fun cycle3 x = = if length(x) = 1 then x = else (List.drop(x, 1); = x @ [hd(x)]); val cycle3 = fn : 'a list -> 'a list The question lies in my else statement, what I want to happen is first concatenate the first term to

SML: Why no new line can't be written to file with “\n”

十年热恋 提交于 2020-01-06 02:19:10
问题 Below is a simple program to write an int list to a file: fun write(num_list, file) = let val output = TextIO.openOut file fun num(nil) = TextIO.closeOut output | num(n::ns) = (TextIO.output(output, Int.toString(n)); TextIO.output(output, "\n"); num(ns)) in num(num_list) end; Why no new line was written to file after each number printed? 回答1: It appears that your code works and that a newline character is written after each number. I have provided an alternative definition for your write

Standard ML: Operator and Operand Don't Agree (Circularity)

放肆的年华 提交于 2020-01-05 08:29:26
问题 I'm trying to write a function in SML to flip alternate elements of a list. Here's my function: fun flipAlternate(nil) = nil | flipAlternate([x]) = x | flipAlternate(x::y::xs) = y::x::flipAlternate(xs); When I go to use my file (Ullman.sml) in the interactive interpreter, I get this compilation error: - use "Ullman.sml"; [opening Ullman.sml] Ullman.sml:5.31-5.54 Error: operator and operand don't agree [circularity] operator domain: 'Z list * 'Z list list operand: 'Z list * 'Z list in

Standard ML: Operator and Operand Don't Agree (Circularity)

大兔子大兔子 提交于 2020-01-05 08:29:08
问题 I'm trying to write a function in SML to flip alternate elements of a list. Here's my function: fun flipAlternate(nil) = nil | flipAlternate([x]) = x | flipAlternate(x::y::xs) = y::x::flipAlternate(xs); When I go to use my file (Ullman.sml) in the interactive interpreter, I get this compilation error: - use "Ullman.sml"; [opening Ullman.sml] Ullman.sml:5.31-5.54 Error: operator and operand don't agree [circularity] operator domain: 'Z list * 'Z list list operand: 'Z list * 'Z list in

Partition a list into equivalence classes

老子叫甜甜 提交于 2020-01-05 06:50:52
问题 I am trying to write a function in SML which when given a list of general elements, reorders its elements into equivalent classes and returns a list of these classes (type "a list list). Leave the elements in the classes in the same order as in the original list. A given function defines the equivalence of the elements and it returns true if the elements are equivalent or false otherwise. I cannot seem to get a grip on the solution. fun sample x y = x = y Required type: fn : (''a -> ''a ->

SML/NJ: How to use HashTable?

不想你离开。 提交于 2020-01-04 14:27:06
问题 I really want to create a HashTable in SML, it seems there already is a structure for this in SML/NJ. The question is, how do I use it? I've not fully understood how to use structures in SML, and some of the very basic examples in the book I read gives me errors I don't even know how to correct, so using the HashTable structure might be an easy thing, but I wouldn't know. If someone could explain this, then that'd be wonderful too! I'm thinking it's something like this: val ht : string * int

Theorem proving from first principles using SML with HOL inference rules

♀尐吖头ヾ 提交于 2020-01-04 09:23:31
问题 I am trying to prove the theorem [] |- p /\ q <=> q /\ p :thm using SML with HOL Inference Rules. Here's the SML code: val thm1 = ASSUME ``p:bool /\ q:bool``; val thm2 = ASSUME ``p:bool``; val thm3 = ASSUME ``q:bool``; val thm4 = CONJ thm2 thm3; val thm5 = CONJ thm3 thm2; val thm6 = DISCH ``(q:bool/\p:bool)`` thm4; val thm7 = DISCH ``(p:bool/\q:bool)`` thm5; val thm8 = IMP_ANTISYM_RULE thm6 thm7; The result of the above code produces : val thm8 = [(p :bool), (q :bool)] |- (q :bool) /\ (p

How to use IntInf or LargeInt in SML?

我只是一个虾纸丫 提交于 2020-01-04 05:07:34
问题 I want to to perform computations with large integers in SML, through functions like pow in this link: http://www.standardml.org/Basis/int-inf.html#IntInf:STR:SPEC But how do I get to use this "library"? UPDATE: Thanks for the answer. I got it. I also had to change the limit for printing with Control.Print.intinfDepth := 10000; I made my own pow function for IntInfs (and it works) like this: fun power 0 = IntInf.toLarge 1 | power n = IntInf.toLarge 2 * power(n-1); 回答1: It depends on which