smlnj

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

喜欢而已 提交于 2020-03-15 09:37:07
问题 I am fresh on SML and doing a homework by that. "Write a function number_in_month that takes a list of dates and a month (i.e., an int) and returns how many dates in the list are in the given month." That's what I worked out and cannot see anything wrong with it. Please help. ` fun number_in_month (dates: (int*int*int) list,month:int) = if ((#2 (hd dates)) = month) then val flag=1 flag+number_in_month(tl dates, month) else number_in_month((tl dates),month)` REPL tells that: replacing VAL with

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

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

Partition a list into equivalence classes

老子叫甜甜 提交于 2020-01-05 06:50:52
问题 I am trying to write a function in SML which when given a list of general elements, reorders its elements into equivalent classes and returns a list of these classes (type "a list list). Leave the elements in the classes in the same order as in the original list. A given function defines the equivalence of the elements and it returns true if the elements are equivalent or false otherwise. I cannot seem to get a grip on the solution. fun sample x y = x = y Required type: fn : (''a -> ''a ->

SML/NJ: How to use HashTable?

不想你离开。 提交于 2020-01-04 14:27:06
问题 I really want to create a HashTable in SML, it seems there already is a structure for this in SML/NJ. The question is, how do I use it? I've not fully understood how to use structures in SML, and some of the very basic examples in the book I read gives me errors I don't even know how to correct, so using the HashTable structure might be an easy thing, but I wouldn't know. If someone could explain this, then that'd be wonderful too! I'm thinking it's something like this: val ht : string * int

What does 'int ?. tree' mean in an SML error message?

怎甘沉沦 提交于 2020-01-04 05:14:06
问题 I have the following SML code that I wrote for a class: fun lookup (cmp: 'a * 'a -> order) (x: 'a, t: 'a tree) : 'a option = case t of Empty => NONE | Node(l,y,r) => case cmp(x,y) of EQUAL => SOME y | LESS => lookup (cmp) (x,r) | GREATER => lookup (cmp) (x,l) In testing this with: val SOME 3 = lookup Int.compare (3, Node(Empty,3,Empty)); And getting the following error back: stdIn:153.1-166.12 Error: operator and operand don't agree [tycon mismatch] operator domain: int * int ?.tree operand: