sml

How to detect eof in ml-lex

久未见 提交于 2019-12-10 11:45:15
问题 While writing a code in ml-lex we need to write to write the eof function val eof = fn () => EOF; is this a necessary part to write also if i want my lexer to stop at the detection of an eof then what should i add to the given function. Thanks. 回答1: The User’s Guide to ML-Lex and ML-Yacc by Roger Price is great for learning ml-lex and ml-yacc. The eof function is mandatory in the user declarations part of your lex definition together with the lexresult type as: The function eof is called by

Statically “extend” a record-ish data type without indirection hassle

烈酒焚心 提交于 2019-12-10 01:00:55
问题 I am currently working with a three-level process for which I need some information to flow being accessed and updated. The information is also three-leveled, in such a way that a process at one level may need to access/update information at its level and at higher levels. type info_0 = { ... fields ... } type info_1 = { ... fields ... } type info_2 = { ... fields ... } fun0 will do some stuff with an info_0 , then pass it to fun1 along with an info_1 , then get back the resulting info_0 and

SML error: operator and operand don't agree when comparing integer in list to an integer

不打扰是莪最后的温柔 提交于 2019-12-09 23:46:02
问题 I am new to Standard ML, and can't figure out why I am getting this type mismatch error: fun number_in_month (month : int, dates : int list) = if null dates then 0 else if (month = (hd (tl (hd dates)))) then number_in_month(month, (tl dates)) + 1 else number_in_month(month, (tl dates)) Evaluating this function results in the following error: Error: operator and operand don't agree [tycon mismatch] 5 operator domain: 'Z list 6 operand: int 7 in expression: 8 tl (hd dates) However, at the REPL,

Lazy datatypes in Objective C

拜拜、爱过 提交于 2019-12-09 17:56:04
问题 In SML, the following is possible for modelling lazy programming, // Have a datatype to wrap a computation datatype 'a susp = Susp of (unit -> 'a) // A function to hold the computation fun delay(f ) = Susp(f) I know that closures can be written using Blocks, int multiplier = 7; int (^myBlock)(int) = ^(int num) { return num * multiplier; }; So I think I can use it as a function argument. The next step would be how to use functions with no real arguments ( unit value e.g. in SML fn () => ) and

How to decide whether to parameterize on the type-level or the module-level when designing modules?

丶灬走出姿态 提交于 2019-12-09 11:54:46
问题 I'm working towards a deep understanding of ML-style modules: I think the concept is important and I love the kind of thinking they encourage. I am just now discovering the tension that can arise between parametric types and parametric modules. I am seeking tools for thinking about the matter that will help me make smart design decisions as I build up my programs. Fist I will try to describe my question in general. Then I will provide a concrete example from a learning project I am working on

What is a good data structure to represent an undirected graph?

一曲冷凌霜 提交于 2019-12-09 11:49:50
问题 I need to construct an undirected graph. I don't need it to do anything too fancy, but ideally it would work like this: structure UDG = UndirectedGraph val g = UDG.empty val g = UDG.addEdges(g, n1, [n2, n4, n7]) (* n1 is connected to n2, n4, and n7 *) val g = UDG.addEdge(g, n2, n3) UDG.connected(g, n2) (* returns [n1, n3] *) Is there a good data structure in SML/NJ to model these relationships? Should I just roll my own? Updates I've gone ahead and tried rolling my own, but I get a type

This pattern seems exhaustive, but I'm still getting warnings

扶醉桌前 提交于 2019-12-08 21:41:21
问题 I'm learning sml, and wrote the following simple function: (* Return a list with every other element of the input list *) fun everyOther [] = [] | everyOther [x] = [x] | everyOther x = let val head::head2::tail = x in head::everyOther(tail) end; Which is generating the following warning: ! Toplevel input: ! val head::head2::tail = x ! ^^^^^^^^^^^^^^^^^ ! Warning: pattern matching is not exhaustive I believe the function can never fail, since val head::head2::tail will always work for lists

Weight-Biased Leftist Heaps: advantages of top-down version of merge?

♀尐吖头ヾ 提交于 2019-12-08 19:32:07
问题 I am self-studying Okasaki's Purely Functional Data Structures, now on exercise 3.4, which asks to reason about and implement a weight-biased leftist heap. This is my basic implementation: (* 3.4 (b) *) functor WeightBiasedLeftistHeap (Element : Ordered) : Heap = struct structure Elem = Element datatype Heap = E | T of int * Elem.T * Heap * Heap fun size E = 0 | size (T (s, _, _, _)) = s fun makeT (x, a, b) = let val sizet = size a + size b + 1 in if size a >= size b then T (sizet, x, a, b)

Beginner SML Syntax

◇◆丶佛笑我妖孽 提交于 2019-12-08 12:38:44
问题 I am very new to SML. I am currently working on a project that is checking to see if a mobile is balanced. My datatype mobile is defined as follows: datatype mobile = Object of int | Wire of mobile * mobile Then I have a weight function to check the weight of a mobile: fun weight (Object w) = w | weight (Wire (l,r)) = weight l + weight r I am now trying to check if the mobile is balanced. I have the following: fun balanced (Object w) = true | balanced (Wire (l,r)) = if weight l = weight r and

ML assignment operation

时光怂恿深爱的人放手 提交于 2019-12-08 12:38:39
问题 everyone, what is difference between the following assignments in ML, val n = 5; and n := 1; 回答1: The former is a declaration of a new, immutable variable. The latter is how you re-assign the value of a reference cell. 来源: https://stackoverflow.com/questions/5168889/ml-assignment-operation