sml

How to do bitwise AND in SML/NJ?

女生的网名这么多〃 提交于 2019-12-20 01:09:30
问题 Need it for a program I'm writing (repeated squaring to comput x^n). I can't seem to find the syntax for it, or if it is even supported. 回答1: They're available within the Word8 and Word structures. let open Word8 infix andb orb xorb notb << >> ~>> in print (Word8.fmt StringCvt.BIN 0wxF) (* 1111 *) ; print "\n" ; print (Word8.fmt StringCvt.BIN 0wxA) (* 1010 *) ; print "\n" ; print (Word8.fmt StringCvt.BIN (0wxF andb 0wxA)) (* 1010 *) ; print "\n" end 来源: https://stackoverflow.com/questions

SML How to define proper option

跟風遠走 提交于 2019-12-19 10:22:05
问题 Why doesn't the following code doesn't work? fun sum_list xs = case xs of [] => NONE | x::xs' => SOME (x+sum_list xs') This code works well when Instead of NONE it is zero and when I remove SOME. I know that for sum of an empty list zero is the reasonable answer. But why does the following example fails? Update: Made it work by following Diego's Answer: fun sum_list xs = case xs of [] => NONE | x => let fun slist x = case x of [] => 0 | x::xs' => x + slist xs' in SOME (slist x) end 回答1: The

What does this function signature mean in sml?

自作多情 提交于 2019-12-19 04:35:28
问题 I'm looking through some notes that my professor gave regarding the language SML and one of the functions looks like this: fun max gt = let fun lp curr [] = curr | lp curr (a::l) = if gt(a,curr) then lp a l else lp curr l in lp end Could someone help explain what this is doing? The thing that I am most confused about is the line: let fun lp curr [] = curr What exactly does this mean? As far as I can tell there is a function called lp but what does the curr [] mean? Are these arguments? If so,

Sml folding a tree

时间秒杀一切 提交于 2019-12-18 07:17:18
问题 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) 回答1: 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

Standard sorting functions in SML?

家住魔仙堡 提交于 2019-12-18 05:51:43
问题 Are there standard sorting functions in SML? The documentation on the Internet is so scarce I couldn't find any. 回答1: 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

SML-NJ, how to compile standalone executable

对着背影说爱祢 提交于 2019-12-17 21:44:01
问题 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? 回答1: Both MosML and MLton also have

Tail-recursion on trees

左心房为你撑大大i 提交于 2019-12-17 13:27:13
问题 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

Standard ML multiple condition statement

喜夏-厌秋 提交于 2019-12-14 03:21:51
问题 I am about finished with a script I am writing but I have one last condition statement to add to my function. fun whileloop (x:real,a:int,b:real) = if (a<1) then (x,a,b) else whileloop(x+1.0,a-1,b-1.0) This is my current loop I have created. It is basically accomplishing everything I need under one exception. I want it to exit its loop once the b variable hits zero[if this happens before a reaches zero). I believe Standard ML will not let me do a condition statement for a real variable...such

How to generate an infinite list of fibonacci numbers using curried function in SMLNJ?

余生长醉 提交于 2019-12-13 18:50:28
问题 I have coded up a general purpose routine that takes multiple arguments and generates an infinite list of fibonacci numbers which is as follows: datatype 'a seq = Nil | Cons of 'a * (unit -> 'a seq) ; fun fibo (a,b) = Cons(a, fn () => fibo(b,a+b)); val fib = fibo(0 , 1); But the problem is I want to use currying technique to generate this infinite list of fibonacci numbers starting from 0 and 1, I am totally perplexed about the concept of currying. Can some enlighten me about the concept of

SML tuples - Combination

三世轮回 提交于 2019-12-13 17:58:09
问题 I've come across a tuples problem where given a list of pair tuples it should become a pair of lists: i.e. [(1,2),(3,4),(5,6)] should return ([1,3,5],[2,4,6]) . I've tried to solve it using this code: fun convert L = foldl (fn(a,b) => #1a::b) [] L; But I get an error saying: unresolved flex record. Anyone able to explain why I'm getting this and how it could be fixed? 回答1: Looking at a , the compiler can tell it's supposed to be a tuple (since you're calling #1a ), but it can't tell how big