ml

building a lexical analyser using ml-lex

自闭症网瘾萝莉.ら 提交于 2019-12-21 19:53:35
问题 I need to create a new instance of a lexer tied to the standard input stream. However, when I type in val lexer = makeLexer( fn n => inputLine( stdIn ) ); I get an error that I don't understand: stdIn:1.5-11.13 Error: operator and operand don't agree [tycon mismatch] operator domain: int -> string operand: int -> string option in expression: ( makeLexer is a function name present in my source code) 回答1: inputLine returns a string option , and my guess is a string is expected. What you want to

Which English tutorial would you advise to learn OCaml? [closed]

女生的网名这么多〃 提交于 2019-12-18 10:41:30
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 3 years ago . I want to advertise OCaml to beginners, and I am looking for good tutorials in English; not that you have only heard of, but that you have actually tried and found useful... 回答1: I quite like the book Developing Applications With Objective Caml -- I guess the title should be updated to mirror the 'OCaml' naming

circlarity error in ml

懵懂的女人 提交于 2019-12-13 04:16:26
问题 I'm trying to run a function subst(tr, v1, v2) which returns a new ntree where all the values of v1 are replaced by v2 in the output tree. datatype 'a ntree = leaf of 'a | node of 'a ntree list; fun map(f, []) = [] | map(f,x::t)=f(x) :: map(f,t); fun subst(leaf(d), v1, v2) = if d=v1 then v2 else d | subst(node(q), v1, v2) = let fun w(k) = if k=v1 then subst(v2, v1, v2) else subst(k, v1, v2) in map(w, q) end; but i get a circularity error b/c my rhs of clause does not agree w/ the function

ML function currying

情到浓时终转凉″ 提交于 2019-12-12 13:06:36
问题 Could someone please explain the concept of currying to me. I am primarily learning it because we are learning ML in my 'modern programming language' class for a functional language introduction. In particular you can use this example: -fun g a = fn b => a+b; val g = fn: int -> int -> int -g 2 3; val it = 5 : int I'm confused how these parameters are passed or how to even think about it in the first place. Thank you for any help. 回答1: In this case, you make the currying explicit, so it should

Write command-line arguments to file in SML

独自空忆成欢 提交于 2019-12-12 04:12:22
问题 I am trying to write the command line arguments from my SML program into a file, each on a separate line. If I were to run sml main.sml a b c easy as 1 2 3 on the command line, the desired output would be to have a file with the contents: a b c easy as 1 2 3 However, I am getting the following output from SML : $ sml main.sml a b c easy as 1 2 3 val filePath = "/Users/Josue/Desktop/espi9890.txt" : string val args = ["a","b","c","easy","as","1","2","3"] : string list main.sml:4.21 Error:

remove elements from a list in ml

本小妞迷上赌 提交于 2019-12-12 04:05:04
问题 I´m trying to write a function that produces a new list containing the given list without the element x. Moscow ML says that some cases are unused in this match. fun delete (x,list) = delete(x,[]) |delete(x,(first::rest)) = if first = x then delete(x,rest) else first::delete(x,rest) 回答1: Here's how I'd do it on Standard ML:: fun delete (item, list) = case list of []=>[] | xs::ys => if item = xs then delete(item,ys) else xs::delete(item,ys) Without using cases: fun delete (item, list) = List

ML anonymous function alternating sum

最后都变了- 提交于 2019-12-12 03:17:23
问题 For my homework assignment in ML I have to use the fold function and an anonymous function to turn a list of integers into the alternating sum. If the list is empty, the result is 0. This is what I have so far. I think what I have is correct, but my biggest problem is I cannot figure out how to write what I have as an anonymous function. Any help would be greatly appreciated. fun foldl f y nil = y | foldl f y (x::xr) = foldl f(f(x,y))xr; val sum = foldl (op -) ~6[1,2,3,4,5,6]; val sum = foldl

'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

ML. “Unzipping” list of tuples.

寵の児 提交于 2019-12-11 09:42:37
问题 So I have this list of tuples(n=2), witch I'm supposed to "unzip" and by that create a new list like so: for list of tuples like this val it = (1,"one") :: (2,"two") :: nil : (int,string) alterlist, the unzip function will create a list like so [(1,2),("one", "two")]. This is what I got so far: datatype ('a, 'b) alterlist = nil | :: of ('a*'b) * ('a, 'b) alterlist; infixr 5 :: fun build4(x, one, y, two) = (x,one)::((y,two)::nil); fun unzip(alterlist) = let fun extract([], i) = [] | extract((x

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