sml

Sml Church numeral type inference

。_饼干妹妹 提交于 2019-12-08 11:31:39
问题 I have this expression in SML and need to find the most general type of it. When run through the compiler I get what it shows below. How would I go about finding what the most general type would be of not only this function but other functions like church numerals function "two". val one = fn f => (fn x => f x) Why is the type of this: ('a -> 'b) -> 'a -> 'b 回答1: What you do is, you apply a process called Hindley–Milner type inference . The general principle involves three steps: First, we

SML syntactical restrictions within recursive bindings?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-08 02:14:39
问题 There seems to be syntactical restrictions within SML's recursive bindings, which I'm unable to understand. What are these restrictions I'm not encountering in the second case (see source below) and I'm encountering when using a custom operator in the first case? Below is the case with which I encountered the issue. It fails when I want to use a custom operator, as explained in comments. Of the major SML implementations I'm testing SML sources with, only Poly/ML accepts it as valid, and all

in smlnj how do you convert “string option” to “string”?

僤鯓⒐⒋嵵緔 提交于 2019-12-07 12:24:36
问题 Please help I have no idea how what a string option does. is it possible to convert string option to a string? 回答1: As already pointed out you could use pattern matching to get the desired result. So, something like this: fun foo(NONE) = "" | foo(SOME a) = a; But you could spare the trouble and use Option.valOf function from SML library instead by just doing: Option.valOf(SOME "my string"); (Or just valOf(SOME "my string"); as newacct pointed out in the comments.) 回答2: The "option" type in ML

How can I customize the SML/NJ interactive loop?

↘锁芯ラ 提交于 2019-12-07 12:04:04
问题 I'm new to Standard ML and I'm trying to get my head around the SML/NJ runtime environment. I want to adapt it to my needs. Specifically, I want to: Use IntInf by default Prevent it from truncating strings and IntInf to 70 characters. Here's what I've found in my 8+ hours reading documentation and experimenting. I can overload IntInf on top of int with the command open IntInf; I can control how many characters in a string are displayed with the variable Control.Print.stringDepth. For example,

SML - Creating dictionary that maps keys to values

ε祈祈猫儿з 提交于 2019-12-07 11:49:33
问题 I need to create a dictionary in sml, but I am having extreme difficulty with an insert function. type dict = string -> int option As an example, here is the empty dictionary: val empty : dict = fn key => NONE Here is my implementation of an insert function: fun insert (key,value) d = fn d => fn key => value But this is of the wrong type, what I need is insert : (string*int) -> dict -> dict. I've searched everything from lazy functions to implementing dictionaries. Any help or direction would

What do “continuations” mean in functional programming?(Specfically SML)

≯℡__Kan透↙ 提交于 2019-12-07 09:18:31
问题 I have read a lot about continuations and a very common definition I saw is, it returns the control state. I am taking a functional programming course taught in SML. Our professor defined continuations to be: "What keeps track of what we still have to do" ; "Gives us control of the call stack" A lot of his examples revolve around trees. Before this chapter, we did tail recursion. I understand that tail recursion lets go of the stack to hold the recursively called functions by having an

Turning a string into a char list list using SML

混江龙づ霸主 提交于 2019-12-07 08:30:27
I'm trying to turn a string into a char list list and I have the following code: fun linelist file = let val instr = TextIO.openIn file val str = TextIO.inputAll instr in String.tokens (fn x => x = #"\n")str before TextIO.closeIn instr end; fun getsudo file = map explode (linelist file); I need to turn a file (sudo.txt) with the following 53**7****\n6**195***\n*98****6*\n8***6***3\n4**8*3**1\n7***2***6\n*6****28*\n***419**5\n****8**79\n into [[#"5",#"3",#"*",#"*",#"7",#"*",#"*",#"*",#"*",], [#"6",#"*",#"*",#"1",#"9",#"5",#"*",#"*",#"*",], [#"*",#"9",#"8",#"*",#"*",#"*",#"*",#"6",#"*",], [#"8",

Polymorphic function as return value and value restriction in SML

我们两清 提交于 2019-12-07 07:09:55
问题 Basically, I want to have a function to return a polymorphic function, some thing like this: fun foo () = fn x => x So the foo function takes in a value of type unit and returns a polymorphic identity function and the compiler is happy with that, it gives me: val foo = fn : unit -> 'a -> 'a but once I actually call the foo function, the return value is not what I expected val it = fn : ?.X1 -> ?.X2 Can't generalize because of value restriction it says, any help? thanks in advance 回答1: For

File seeking with SML Basis

北城余情 提交于 2019-12-07 05:38:47
问题 Is there a way, using the SML Basis library, to open a file at a specific position? That is, use an operating system call to change the position, rather than scan through the file and throw away the data. 回答1: This is tricky. Unfortunately, seeking isn't directly supported. Moreover, file positions are only transparent for binary files, i.e., those that you have opened with the BinIO structure [1]. For this structure, the corresponding type BinIO.StreamIO.pos is defined to be Position.int ,

SML - unbound variable or constructor

岁酱吖の 提交于 2019-12-07 03:41:58
问题 I have the next code: datatype expr = K of string| Number2 of expr * (expr list); datatype number = Number1 of string | Number3 of int; fun append (nil, l2) = l2 | append (x::xs, l2) = x::append(xs, l2); fun map [] = [] | map (h::t) = (What h)::(map t); fun What (K x) = [Number1(x)] |What (Number2 (t,[])) = Number3(0)::What(t) |What (Number2 (y,a::b)) = append(What(a), map(b)); It doesn't recognize the function "What".(unbound variable or constructor). How can I fix it, that it will know the