sml

VAL keyword within IF condition won't allow reassignment in SML

放肆的年华 提交于 2019-12-11 14:45:47
问题 Given this following code (which does not work): 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 wasn't a multiple, or subtraction operation *) val last_sub = 0 val problem = 0 (* if value is 1 then there is a problem with the input *) val myList = [1,2,3

Recursive function to pretty-print the elements of a list

≯℡__Kan透↙ 提交于 2019-12-11 11:05:55
问题 I need to write a function which takes input of type (int * int) list and prints pairs of integers. This function should leverage another function printGenList (takes a function f and a list l and applies f to every element of list recursively) whose code I have written like this - fun printGenList f l = if NULL l then () else ( (f (HD l) ); printGenList (f) (TL l) ); and provide an anonymous function (the fn … => … construct) that will do the appropriate pretty printing. 回答1: The type

Standard ML Expand List

烂漫一生 提交于 2019-12-11 10:45:51
问题 Directions Function expand that receives a list of any type and an integer number n , and returns a list in which each item of input list is replicated n times. For example, expand [1,2,3] 3 must be evaluated to [1,1,1,2,2,2,3,3,3].The type of the function must be ‘a list→int→‘a list. Here's my solution where I cheated around the requirements a bit by having two functions. What I am struggling with is resetting n to the original value when I move on to the next item in the list. I do so in my

Count elements in a list

不羁岁月 提交于 2019-12-11 10:38:15
问题 I have been trying to count elements in a list of integer 3-tuples, that equals a given integer using SML, but it's not working. Can anyone help me figure out what's wrong with the below code or straighten it up for me? fun number_in_month(x : int*int*int list, m: int) = if null x then 0 else let fun inc x = x + 1; in val counter = 0; if m = #2 (hd x) andalso m > 0 then inc counter number_in_month((tl x), m) ` else number_in_month((tl x), m) end This function is supposed to return the number

'Unpacking' the data in an SML DataType without a case statement

独自空忆成欢 提交于 2019-12-11 10:29:38
问题 I have an SML program which represents a language with Expressions that are comprised of Values: datatype Value = IntVal of int | ListVal of Value list datatype Exp = Const of Value | Plus of Exp * Exp | Minus of Exp * Exp | Times of Exp * Exp I'm also writing an eval function that converts an expression into a value. If the expression is a Plus expression (e.g. Plus (Const (IntVal 1), Const (IntVal 1)) which represents 1+1 ), I just want to take out the integer stored in the IntVal and just

Getting max value from a list in SML

岁酱吖の 提交于 2019-12-11 08:35:49
问题 I'm currently studying SML and I'm having a hard time understanding the code below fun good_max (xs : int list) = if null xs then 0 else if null (tl xs) then hd xs else (* for style, could also use a let-binding for (hd xs) *) let val tl_ans = good_max(tl xs) in if hd xs > tl_ans then hd xs else tl_ans end hd xs is of type int and tl_ans , I think is of type list . Why does this code work? How does the system evaluate the recursion? It would be great if you could use xs = [3, 4, 5] to show me

How to move the cursor in SML/NJ's REPL in terminal on Mac?

守給你的承諾、 提交于 2019-12-11 07:26:36
问题 In the terminal of Mac OSX, I open SML , if I type something wrong, I wish to move my cursor to that place to modify something or add/delete something, but once I hit <- (the left arrow ) on the keyboard, the REPL gives me Yes, that ^[[D thing. So how do I move the cursor? 回答1: Yes, the REPL of SML/NJ does not support arrow keys. You can use the rlwrap tool to fix this. That allows you to use the left and right arrow keys to navigate within a line and the up and down arrow keys to recall

How do I create a call-by-need list with increasing size in Standard ML?

冷暖自知 提交于 2019-12-11 07:24:04
问题 I am trying to create a lazy list with list elements which together represent all the combinations of zeros and ones. Example: [[], [0], [1], [0,0], [0,1], [1,0]...] Is this even possible in ML? I can't seem to find a way to change the pattern of the list elements once I have defined it. It seems that there is also a need to define a change in the binary pattern, which is not really possible in a functional language (I've never encountered binary representations in functional language)? 回答1:

Standard ML, Infix identifier ERROR code

…衆ロ難τιáo~ 提交于 2019-12-11 07:22:42
问题 exception div; fun f(x,y) = let val before = 2.0 * x + 3.0 * y in (before + (1.0 / (if x > 0.0001 then x else raise div)) + 2.0 / y) handle div => before / 6.0 end This code yields some compile error. That is e.sml:4.8-4.14 Error: expression or pattern begins with infix identifier "before" e.sml:6.8-6.14 Error: expression or pattern begins with infix identifier "before" e.sml:6.57-6.60 Error: expression or pattern begins with infix identifier "div" e.sml:6.81-6.84 Error: expression or pattern

Understanding user defined append list Standard ml

眉间皱痕 提交于 2019-12-11 06:08:02
问题 Im having trouble understanding this implementation of lists in Standard ML. Here is how it is defined: An append list is a (simple) implementation of the list abstract data type that makes construction cheap (O(1)), but makes destruction expensive (O(n)). The 'a alistNN and 'a alist types are defined as follows: datatype 'a alistNN = Sing of 'a | Append of 'a alistNN * 'a alistNN datatype 'a alist = Nil | NonNil of 'a alistNN The 'a alistNN type represents the “non-nil” append lists, while