smlnj

How to use infix operator from a SML module?

狂风中的少年 提交于 2020-01-04 04:55:10
问题 I have the following to code and I use SML/NJ: signature STACK= sig type 'a Stack val empty :'a Stack val isEmpty : 'a Stack -> bool val cons : 'a*'a Stack -> 'a Stack val head : 'a Stack ->'a val tail : 'a Stack -> 'a Stack val ++ : 'a Stack * 'a Stack -> 'a Stack end structure List : STACK = struct infix 9 ++ type 'a Stack = 'a list val empty = [] fun isEmpty s = null s fun cons (x,s) = x::s fun head s = hd s fun tail s = tl s fun xs ++ ys = if isEmpty xs then ys else cons(head xs, tail xs

List.filter function not working in SML/NJ

你离开我真会死。 提交于 2020-01-01 19:48:46
问题 I'm building a simple function to remove item from List1 ... fun Strip(item, List1) = filter (fn x => x <> item) List1; Input: Strip(3,[1,2,3,4,3]); Error: Error: Unbound variable or constructor: Strip Alternate input: filter (fn x => x <> 5) [1,3,5,2,5]; Alternate error: stdIn:1.2-1.8 Error: unbound variable or constructor: filter Any ideas why such a simple function isn't working? 回答1: As to the error message "unbound variable or constructor: filter", in this case it means that the

Does SMLNJ have any sort of debugger?

家住魔仙堡 提交于 2020-01-01 04:05:13
问题 I have looked through the SMLNJ User Guide and can't find anything about debugging capabilities. I'd love to just see a stack trace, or step through a function. Is this possible. Are there other implementations for similar variants of SML that do provide this feature? 回答1: From section 3.3 of the SMLNJ faq: Q: Is there a debugger for SML/NJ? What ever happened to Tolmach's debugger for SML/NJ 0.93? A: The short answer is no. Also: Debugging SML * For years, no one had an SML debugger * Why? o

Multiple Patterns in 1 case

僤鯓⒐⒋嵵緔 提交于 2019-12-30 23:22:30
问题 In SML, is it possible for you to have multiple patterns in one case statement? For example, I have 4 arithmetic operators express in string, "+", "-", "*", "/" and I want to print "PLUS MINUS" of it is "+" or "-" and "MULT DIV" if it is "*" or "/" . TL;DR: Is there somewhere I can simplify the following to use less cases? case str of "+" => print("PLUS MINUS") | "-" => print("PLUS MINUS") | "*" => print("MULT DIV") | "/" => print("MULT DIV") 回答1: Given that you've tagged your question with

nested local declarations in ML of NJ

核能气质少年 提交于 2019-12-30 18:26:14
问题 hello everyone I have this snippet of the code: local helper(f, i, j) = local fun NTimesF(f, n:int) = if n = 1 then fn (x) => f(x) else fn (x) => f(NTimesF(f, n - 1)(x)); in if(i <= j) then NTimesF(f, i) :: helper(f, (i+1), j) else [] end in fun compList f n = helper(f, 1, n); end; I need to write program which receives some function f and integer n and produce list of functions such as [f1, f2, ... fn] <- fn is the composition of the function n times but every time I receive an error: -

SML Warning: Type Vars Not Generalized when using Empty Lists or NONE option

眉间皱痕 提交于 2019-12-30 08:07:18
问题 I can't for the life of me figure out why the following SML function is throwing a Warning in my homework problem: fun my_func f ls = case ls of [] => raise MyException | head :: rest => case f head of SOME v => v | NONE => my_func f rest fun f a = if isSome a then a else NONE; Whenever I call my_func with the following test functions: my_func f [NONE, NONE]; my_func f []; I always get the warning: Warning: type vars not generalized because of value restriction are instantiated to dummy types

smlnj listdir problems

喜夏-厌秋 提交于 2019-12-25 17:06:23
问题 I am a newbie learning sml and the question I am given involves IO functions that I do not understand. Here are the 2 questions that I really need help with to get me started, please provide me with code and some explanation, I will be able to use trial and error with the code given for the other questions. Q2) readlist(filename) which reads a list of filenames (each of which were produced by listdir in (Q1) and combines them into one large list. (reads from the text file in Q1 and then

Smallest sub-list that contains all numbers

旧城冷巷雨未停 提交于 2019-12-25 01:27:54
问题 I am trying to write a program in sml that takes in the length of a list, the max number that will appear on the list and the list of course. It then calculates the length of the smallest "sub-list" that contains all numbers. I have tried to use the sliding window approach , with two indexes , front and tail. The front scans first and when it finds a number it writes into a map how many times it has already seen this number. If the program finds all numbers then it calls the tail. The tail

Turn a list with common items in to a list of ordered pairs

回眸只為那壹抹淺笑 提交于 2019-12-24 10:55:42
问题 I am entirely new to functional programming; this is a homework assignment for SML. I have a list of integers and I am trying to get a list of ordered pairs where the second entry of the pair is the number of times the first entry appeared in the initial list. For example: [2,3,3,5] => [(2,1),(3,2),(5,1)] I'm not hoping for somebody to implement this but rather give me an idea of what sort of higher-order function I am looking for, and/or a pointer in the right direction. Again, totally new

handling exceptions in ML

我只是一个虾纸丫 提交于 2019-12-24 01:44:22
问题 everyone, I'm trying to understand how exceptions work in ML, but I have strange error, and I can't figure out what is wrong: exception Factorial fun checked_factorial n = if n < 0 then raise Factorial else n; fun factorial_driver () = checked_factorial(~4) handle Factorial => print "Out of range."; what may be wrong? thanks in advance for any help. 回答1: You need to make sure that factorial_driver has a consistent type. The non-exceptional case returns int , so ML infers the function to be of