sml

Better display of boolean formulas

假如想象 提交于 2019-11-28 02:05:59
I want to implement a method for showing a propositional formula in SML. The solutions that I found so far was of this type: fun show (Atom a) = a | show (Neg p) = "(~ " ^ show p ^ ")" | show (Conj(p,q)) = "(" ^ show p ^ " & " ^ show q ^ ")" | show (Disj(p,q)) = "(" ^ show p ^ " | " ^ show q ^ ")"; This produces unnecessary braces: ((~p) & (q | r)) when, what I'd like to have is: ~ p & (q | r) I saw, that Haskell has a function (display?) which does this nicely. Can someone help me out a little bit. How should I go about this? If you want to eliminate redundant parentheses, you will need to

How does the compiler determine types in sml

谁都会走 提交于 2019-11-28 01:50:56
问题 I've been given the following code and I've been asked to determine the type. exception Break; fn f => fn a => fn b => (f(raise Break) handle Break => if (f a) then a else raise Break) handle Break => if not(f a) then a else b; I know that this function takes in f, a and b and outputs a in all instances so it must be equivelant to: fn f => fn a => fn b => a which has the type: 'a -> 'b -> 'c -> 'b because 'if( f a)' we can derive that 'a must be a function that takes in type 'b and outputs a

Increasing the print depth in SML/NJ

人走茶凉 提交于 2019-11-27 17:28:28
问题 I'm trying to get SML/NJ to print out a result at the top level without putting # signs everywhere. According to some old docs (and a post to this newsgroup on 2001), it should be possible to use Compiler.Control.Print.printDepth However, on SML/NJ version 110.7, this just gives an error: - Compiler.Control.Print.printDepth := 100; stdIn:1.1-30.8 Error: unbound structure: Control in path Compiler.Control.Print.printDepth 回答1: You might wan't to be more precise in the future. You could for

Tail-recursion on trees

白昼怎懂夜的黑 提交于 2019-11-27 15:22:25
I have a data structure, datatype 'a tree = Leaf | Branch of 'a tree * 'a * 'a tree and I want to write a function that traverses this tree in some order. It doesn't matter what it does, so it could be a treefold : ('a * 'b -> 'b) -> 'b -> 'a tree -> 'b . I can write this function like this: fun treefold f acc1 Leaf = acc1 | treefold f acc1 (Branch (left, a, right)) = let val acc2 = treefold acc1 left val acc3 = f (a, acc2) val acc4 = treefold acc3 right in acc4 end But because I inevitably have two branches in the last case, this is not a tail-recursive function. Is it possible to create one

Binding an objects value within a function (closure)

烈酒焚心 提交于 2019-11-27 08:05:26
问题 In SML (a functional programming language that I learned before Python), I can do the following: val x = 3; fun f() = x; f(); >>> 3 val x = 7; f(); >>> 3 In Python, however, the first call will give 3 and the second one will give 7. x = 3 def f(): return x f() >>> 3 x = 7 f() >>> 7 How do I bind the value of a variable to a function in Python? 回答1: You can use a keyword argument: x = 3 def f( x=x ): return x x = 7 f() # 3 Keyword arguments are assigned when the function is created . Other

Expanding # in sml

六月ゝ 毕业季﹏ 提交于 2019-11-27 06:08:39
问题 Suppose I have a list in sml which is very big then sml shows a few of the entries and then starts showing # character. Could someone tell me how could I view the whole list? 回答1: Assuming this is SML/NJ you could use printLength , printDepth and friends from the Control.Print structure. The following are a snippet from the documentation of the Control.Print structure: printDepth The depth of nesting of recursive data structure at which ellipsis begins. printLength The length of lists at

Difference between “local” and “let” in SML

社会主义新天地 提交于 2019-11-27 06:01:46
问题 I couldn't find a beginner friendly answer to what the difference between the "local" and "let" keywords in SML is. Could someone provide a simple example please and explain when one is used over the other? 回答1: (TL;DR) Use case ... of ... when you only have one temporary binding. Use let ... in ... end for very specific helper functions. Never use local ... in ... end . Use opaque modules instead. Adding some thoughts on use-cases to sepp2k's fine answer: (Summary) local ... in ... end is a

Better display of boolean formulas

白昼怎懂夜的黑 提交于 2019-11-27 04:52:08
问题 I want to implement a method for showing a propositional formula in SML. The solutions that I found so far was of this type: fun show (Atom a) = a | show (Neg p) = "(~ " ^ show p ^ ")" | show (Conj(p,q)) = "(" ^ show p ^ " & " ^ show q ^ ")" | show (Disj(p,q)) = "(" ^ show p ^ " | " ^ show q ^ ")"; This produces unnecessary braces: ((~p) & (q | r)) when, what I'd like to have is: ~ p & (q | r) I saw, that Haskell has a function (display?) which does this nicely. Can someone help me out a

Output is truncated with #-signs in the REPL

霸气de小男生 提交于 2019-11-27 02:10:23
I wrote a function which works as expected but i don't understand why the output is like that. Function: datatype prop = Atom of string | Not of prop | And of prop*prop | Or of prop*prop; (* XOR = (A And Not B) OR (Not A Or B) *) local fun do_xor (alpha,beta) = Or( And( alpha, Not(beta) ), Or(Not(alpha), beta)) in fun xor (alpha,beta) = do_xor(alpha,beta); end; Test: val result = xor(Atom "a",Atom "b"); Output: val result = Or (And (Atom #,Not #),Or (Not #,Atom #)) : prop This is just an output restriction (yes, it's confusing) - by default the depth of value printouts in the top-level

Why can't I compare reals in Standard ML?

馋奶兔 提交于 2019-11-26 10:02:38
问题 Why doesn\'t 1.0 = 2.0 work? Isn\'t real an equality type? It gives the error: Error: operator and operand don\'t agree [equality type required] operator domain: \'\'Z * \'\'Z operand: real * real in expression: 1.0 = 2.0 Why won\'t reals in patterns work like so? fun fact 0.0 = 1.0 | fact x = x * fact (x - 1.0) It gives the error: Error: syntax error: inserting EQUALOP 回答1: Why doesn't 1.0 = 2.0 work? Isn't real an equality type? No. The type variable ''Z indicates that the operands of =