sml

Sml folding a tree

女生的网名这么多〃 提交于 2019-11-29 12:26:53
I am trying to get the product of a tree by using the fold function so far this is what I have. I am confused on how to use the fold method while transversing the tree datatype 'a bin_tree = Leaf of 'a | Node of 'a bin_tree * 'a bin_tree fun treefold g z Empty = z | treefold g z (Node (l, x, r)) = g(x, g(treefold g z l, treefold g z r) First, some pointers about what isn't quite in order in your attempt. The base case of your treefold function matches against the value constructor Empty , but you don't define the bin_tree datatype to include an Empty value constructor. As John Coleman points

Standard sorting functions in SML?

て烟熏妆下的殇ゞ 提交于 2019-11-29 10:25:51
Are there standard sorting functions in SML? The documentation on the Internet is so scarce I couldn't find any. Jesper.Reenberg Rachel is only partly right. It is true that there is no sorting functionality defined in the SML Basis Library, however most implementations extend the basis library and add extra functionality. As such MosML has both an ArraySort and a Listsort module, and SML/NJ has a LIST_SORT signature with a ListMergeSort implementation. It also features some other sorting features on arrays as MosML. See the toc of the SML/NJ Library Manual, for a full list. As Jesper Reenberg

How does the compiler determine types in sml

匆匆过客 提交于 2019-11-29 08:19:04
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 boolean otherwise it wouldn't work. ('b->bool) -> 'b -> 'c -> 'b re done so 'a is the beginning: ('a-

Could I ask for physical analogies or metaphors for recursion?

℡╲_俬逩灬. 提交于 2019-11-29 07:20:35
I am suddenly in a recursive language class (sml) and recursion is not yet physically sensible for me. I'm thinking about the way a floor of square tiles is sometimes a model or metaphor for integer multiplication, or Cuisenaire Rods are a model or analogue for addition and subtraction. Does anyone have any such models you could share? Imagine you're a real life magician, and can make a copy of yourself. You create your double a step closer to the goal and give him (or her) the same orders as you were given. Your double does the same to his copy. He's a magician too, you see. When the final

Increasing the print depth in SML/NJ

家住魔仙堡 提交于 2019-11-29 03:22:17
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 You might wan't to be more precise in the future. You could for example give some sample output and a link to where you found the above. If I understand your problem correct,

How to 'fix' the SML/NJ interactive system to use Arrow Keys

倖福魔咒の 提交于 2019-11-29 00:54:22
问题 I'm having some trouble using SML/NJ interactive system, namely, that when I try to use my arrow keys (either left or right to make a correction in the expression I've typed, up to repeat the last expression), my Terminal prints codes. (e.g. ^[[A for up ^[[D for left, etc.). While I can still use the system, it makes it very tedious. I've looked around in Control.Compiler, is there something I'm missing? For whatever its worth, I'm using the Mac Terminal. Thanks ^_^ 回答1: Try this. You can use

SML-NJ, how to compile standalone executable

旧城冷巷雨未停 提交于 2019-11-28 16:20:29
I start to learn Standard ML, and now I try to use Standard ML of New Jersey compiler. Now I can use interactive loop, but how I can compile source file to standalone executable? In C, for example, one can just write $ gcc hello_world.c -o helloworld and then run helloworld binary. I read documentation for SML NJ Compilation Manager, but it don`t have any clear examples. Also, is there another SML compiler (which allow standalone binary creating) available? Both MosML and MLton also have the posibility to create standalone binary files. MosML through mosmlc command and MLton through the mlton

Binding an objects value within a function (closure)

时间秒杀一切 提交于 2019-11-28 13:59:08
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? 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 variables are looked up in the function's scope when the function is run . (If they're not found in the

What is wrong with my code in sml?

梦想与她 提交于 2019-11-28 11:40:34
问题 I don't know why my code doesn't work. fun lookup _ [] = 0 | lookup key ((k,v)::entries) = if k = key then v else (lookup key entries) That's what happened when I tested it in cmd. val lookup = fn : ''a -> (''a * int) list -> int - lookup (1,[(1,2),(2,3)]); val it = fn : ((int * (int * int) list) * int) list -> int 回答1: There's nothing wrong with your code, you just didn't call lookup with enough arguments. You make a common mistakes among beginner SML programmers coming from other languages.

Expanding # in sml

﹥>﹥吖頭↗ 提交于 2019-11-28 11:23:50
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? 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 which ellipsis begins. stringDepth The length of strings at which ellipsis begins. Thus for example we can