sml

number_in_month exercise (Error in SML function to build a list of integers from a list of tuples)

纵然是瞬间 提交于 2020-03-23 09:03:28
问题 val test1 = [(1,5,3),(3,5,2),(3,4,5)] fun number_in_month dates_and_month = case dates_and_month of (x,y,z)::xs' => y :: number_in_month xs' This code produces the following error when I run in the REPL with test1: uncaught exception Match [nonexhaustive match failure] raised at: hw1pm.sml:28.49 Any clue why? 回答1: It did not know what do when the list was empty. Working code: fun number_in_month dates_and_month = case dates_and_month of [] => [] | (x,y,z)::xs' => y :: number_in_month xs' 回答2:

number_in_month exercise (SML list iteration)

两盒软妹~` 提交于 2020-03-02 12:40:21
问题 I need to take a list of dates and a list of months and get a total count of the amount of dates that are in any of the months listed. Thus returning a single integer I had a previously defined number_in_month function which takes in a list of dates and a single month and returns the amount of dates that fall in that month. It has been tested and works correctly. I use this as the foundation for the latter function. I've traced through the number_in_months function many times and I can't seem

Help sml - troubled

谁说我不能喝 提交于 2020-01-30 11:12:10
问题 How to declare a function so Listn : ' ' a list -> ' ' a list -> bool, listn xs and ys return true. Example: lisen [#"1" #"2"] , [#"1" "#3"] return false and [#"1" , #"2"] [#"2" , #"1"] return true 回答1: Try this: infix member fun x member [] = false | x member (y::ys) = x = y orelse x member ys; fun listn (x::xs) ys = x member ys andalso listn xs ys | listn [] _ = true; 来源: https://stackoverflow.com/questions/3946390/help-sml-troubled

Help sml - troubled

a 夏天 提交于 2020-01-30 11:11:04
问题 How to declare a function so Listn : ' ' a list -> ' ' a list -> bool, listn xs and ys return true. Example: lisen [#"1" #"2"] , [#"1" "#3"] return false and [#"1" , #"2"] [#"2" , #"1"] return true 回答1: Try this: infix member fun x member [] = false | x member (y::ys) = x = y orelse x member ys; fun listn (x::xs) ys = x member ys andalso listn xs ys | listn [] _ = true; 来源: https://stackoverflow.com/questions/3946390/help-sml-troubled

How to keep elements in list through out the program in SML?

白昼怎懂夜的黑 提交于 2020-01-25 21:34:13
问题 Suppose I have to update a list with each call to a function, such that, the previous element of the list are preserved. Here is my attempt: local val all_list = []; in fun insert (x:int) : string = int2string (list_len( ((all_list@[x])) ) ) end; The problem is that each time I call to insert, I get the output "1", which indicates that the list is initiated to [] again. However I was expecting output of "1" for the first call to insert, and "2" for the second call,...etc. I am not able to

How to export Standard ML functions to JavaScript via EMSCRIPTEN?

*爱你&永不变心* 提交于 2020-01-25 11:29:11
问题 Is it possible to export a Standard ML function such as fun fac 0 = 1 | fac n = n * fac (n - 1); To a respective JavaScript function, via Emscripten? I know the compilation can be done, but I'm not sure how exactly you would access "fac" from JS code. 来源: https://stackoverflow.com/questions/20918629/how-to-export-standard-ml-functions-to-javascript-via-emscripten

Tail recursion in SML does not present any output

元气小坏坏 提交于 2020-01-25 07:31:06
问题 Following my previous post here , I tried to do what was suggested and convert the code into a Tail-recursion method with let . The original code - which does not work (due to using val inside if condition) : fun func() = val decimal = 0 (* the final result *) val multiple = 0 (* keeps track of multiples, eg. In XXV, X would be a multiple *) val current = 0 (* the digit currently being processed *) val top = 0 (* value of the last element in the list *) val last_add = 0 (* the last digit that

Horner's rule for two-variable polynomial

狂风中的少年 提交于 2020-01-21 11:26:07
问题 Horner's rule is used to simplify the process of evaluating a polynomial at specific variable values. https://rosettacode.org/wiki/Horner%27s_rule_for_polynomial_evaluation#Standard_ML I've easily applied the method using SML, to a one variable polynomial, represented as an int list: fun horner coeffList x = foldr (fn (a, b) => a + b * x) (0.0) coeffList This works fine. We can then call it using: - val test = horner [1.0, 2.0, 3.0] 2.0; > val test = 17.0 : real Where [1.0, 2.0, 3.0] is the

SML List Deletion

帅比萌擦擦* 提交于 2020-01-17 03:44:29
问题 I am trying to write a function to delete a list from another list. ''a list -> ''a list -> ''a list Here's what I have so far: fun delete _ [] = [] | delete (h1::t1) (h2::t2) = if h1=h2 then t2 else h2::delete (h1::t1) t2; I am using MoscowML and it gives me a Warning: pattern matching is not exhaustive error. A test of the above function: - delete [4,5] [1,2,3,4,5,6,7,8]; > val it = [1,2,3,5,6,7,8] : int list The desired output is: > val it = [1,2,3,6,7,8] : int list 回答1: There are two

SML List Deletion

陌路散爱 提交于 2020-01-17 03:44:06
问题 I am trying to write a function to delete a list from another list. ''a list -> ''a list -> ''a list Here's what I have so far: fun delete _ [] = [] | delete (h1::t1) (h2::t2) = if h1=h2 then t2 else h2::delete (h1::t1) t2; I am using MoscowML and it gives me a Warning: pattern matching is not exhaustive error. A test of the above function: - delete [4,5] [1,2,3,4,5,6,7,8]; > val it = [1,2,3,5,6,7,8] : int list The desired output is: > val it = [1,2,3,6,7,8] : int list 回答1: There are two