sml

what is a mutually recursive type?

会有一股神秘感。 提交于 2019-12-02 14:21:34
问题 If in ML, an example of a recursive datatype is: datatype llist = Nil | Node of int * llist What is a mutually recursive datatype and whats an example of it, in ML? 回答1: One such example could be these stupid datatypes. datatype a = A | Ab of b and b = B | Ba of a They make no sense, but they show that it is possible to use the and keyword (just as with functions) to reference something "ahead" which is normally not possible They are mutually (as they both...) recursive (...reference each

Horner's algorithm in SML? [closed]

懵懂的女人 提交于 2019-12-02 13:43:13
I am trying to implement Horner's algorithm in SML. fun horner(lst1:real list,x:real) = let val i = ref 1 val result = ref (List.last(lst1)) in if (lst1) = ([]:real list) then 0.0 else while (!i <= length(lst1)-1) do (result:=!result*x+List.nth(lst1,length(lst1)-(!i)-1); i := !i+1); !result end; Takes on a{n}, the coeff of x^n, as its initial result, then using horner's evaluates a polynomial. Evaluates as ((a{n}*x+a{n-1})*x+a{n-2})..The list contains the coefficients of the polynomial. Problem is the "if lst1 = []....else" part. Employing only the while loop makes the program run well. But I

Partial sum in Standard ML?

余生长醉 提交于 2019-12-02 10:53:05
Im new to functional programming and I have an assignment to compute partial sum of a list. E.g. - psum [1,1,1,1,1]; val it = [1,2,3,4,5] : int list Here is the my code so far. However in function psum2[L] i dont know how to go through each value and add them up so I just print the list. fun psum2(L) : int list = if L=nil then [] else L; fun pSum(L) : int list = psum2(L); exception Empty_List; psum([2,3,4]); Your question is a little broad, but here's one way to sum a list. Perhaps you can adapt it to your purposes: fun sum [] = 0 | sum (h::t) = h + sum t 来源: https://stackoverflow.com

what is a mutually recursive type?

寵の児 提交于 2019-12-02 08:11:01
If in ML, an example of a recursive datatype is: datatype llist = Nil | Node of int * llist What is a mutually recursive datatype and whats an example of it, in ML? One such example could be these stupid datatypes. datatype a = A | Ab of b and b = B | Ba of a They make no sense, but they show that it is possible to use the and keyword (just as with functions) to reference something "ahead" which is normally not possible They are mutually (as they both...) recursive (...reference each other) The standard basic example of mutually recursive data types is a tree and a forest: a forest is a list

Hint for SML type inference

本秂侑毒 提交于 2019-12-02 07:27:05
问题 I am new to SML and I am trying to practice in SML type reference.I am trying to deduct the below types: a)fun add42 x =x+42 b)fun comp F G = let fun C x = G(F(x)) in C end c)fun compA42 x = comp add42 x d)val foo = compA42 add42 e)fun compCompA42 x = comp compA42 x I think the solutions for the first four is: a)int->int b)(a->b)->(b->c)->a->c c)(int->a)->int->a d)int->int But I am a little bit confused about the last one. Is there any hint to deduct the last type?? Thanks a lot. 回答1: Let's

Hint for SML type inference

半城伤御伤魂 提交于 2019-12-02 03:43:35
I am new to SML and I am trying to practice in SML type reference.I am trying to deduct the below types: a)fun add42 x =x+42 b)fun comp F G = let fun C x = G(F(x)) in C end c)fun compA42 x = comp add42 x d)val foo = compA42 add42 e)fun compCompA42 x = comp compA42 x I think the solutions for the first four is: a)int->int b)(a->b)->(b->c)->a->c c)(int->a)->int->a d)int->int But I am a little bit confused about the last one. Is there any hint to deduct the last type?? Thanks a lot. Let's do this manually, step-by-step: fun compCompA42 x = comp compA42 x It's a function, so compCompA42 has type α

Type variable to be unified occurs in type

╄→尐↘猪︶ㄣ 提交于 2019-12-02 00:58:32
I have a function to reconstruct a tree from 2 lists. I return a list on all branches, but I am getting an error that I don't understand. But I assume it has to do with the return types. The error is this: Can't unify ''a with ''a list (Type variable to be unified occurs in type) Found near recon ( ::( preoH, preoT), ::( inoH, ...)) Exception- Fail "Static errors (pass2)" raised The line the error occurs at is the headline of the function definition fun recon (preoH::preoT, inoH::inoT) = What does that error mean exactly and why does it occur? (* Reconstruts a binary tree from an inorder and a

Value of bindings in SML?

懵懂的女人 提交于 2019-12-02 00:50:24
问题 can someone please explain why is "ans" is bound to value of 16 in here after evaluation - this is a correct answer? I thought the answer 3 since we're calling function f and sending values 1 and 2 as function f doesn't also see the values 5 and 10 but I guess I am wrong. val x = 1 val y = 2 val f = fn y => x + y val x = 5 val y = 10 val ans = f x + y 回答1: What you are seeing is sometimes called lexical scoping . The function f was defined in the scope of a certain binding for x , that scope

SML not detecting OS on OS X Mavericks

旧城冷巷雨未停 提交于 2019-12-01 23:03:08
I could not run SMLNJ on Mavericks It shows me the error sml: unable to determine architecture/operating system I also looked in /usr/local/smlnj/config/_arch-n-opsys file and Mavericks is mentioned there. 13*) OPSYS=darwin; HEAP_OPSYS=darwin ;; # MacOS X 10.9 Mavericks Can some one help me on this? You can use this .pkg file to reinstall the SML and see if the problem happens again. 来源: https://stackoverflow.com/questions/20009628/sml-not-detecting-os-on-os-x-mavericks

How to combine equal sequence elements (functional programming)?

岁酱吖の 提交于 2019-12-01 21:32:59
I want to write a function that takes in a sequence <1,1,2,2,3> and returns the sequence with equal elements grouped like <<1,1>, <2,2>, <3>>. I'm using sequences, not lists, but some of the functions are similar. Some of the functions I am thinking of using are map, reduce, tabulate, filter, append etc.. Reduce takes in an associative function and returns the sequence that is "reduced" by that operator. So, reduce op+ 0 <1,2,3> = 6. My first thought was to use map to raise the sequence by one level. So, <1,1,2,2,3> => <<1>,<1>,<2>,<2>,<3>>. Then, I was thinking of using reduce, in which I