sml

what are curry and uncurry in high-order functions in ML

一个人想着一个人 提交于 2020-01-02 04:01:25
问题 fun curry f x y = f (x, y); fun uncurry f (x, y) = f x y; fun compose (f, g) x = f (g x); I understand compose function, but not quite understand curry and uncurry in ML. Can anyone explain these? Also, what do the following two lines mean? (1) compose (compose, uncurry compose) (2) compose (uncurry compose, compose) 回答1: If you look at the types, then you will clearly see what curry and uncurry does. Remember that it is possible to define function which either takes its arguments as one big

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

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

Curried function for evaluating an equation in SML

雨燕双飞 提交于 2019-12-25 18:25:20
问题 The exact problem I need to work out is below. Basically, I am given a list of real numbers, representing coefficients. The first element is the constant value, and all that follow it are the coefficients of the polynomial equation. So, eval [1.0, 5.0, 3.0] 2.0 would be constructing the equation "1 + 5x + 3x^2" and evaluating it at 2.0, resulting in 23.0. The function needs to be of type "real list -> real -> real". Now, what I need to do is implement eval within SML using a curried function,

How to determine which day of a year belongs to what month? [closed]

寵の児 提交于 2019-12-25 18:21:29
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 6 years ago . I'm a beginner and I gotta write a function in SML. The assignment question is : write a function named what_month that takes the

standard ml make bst out of a list

半腔热情 提交于 2019-12-25 03:32:55
问题 I want to make a function standard ml that takes a list and function and makes a BST out of it. The function's type is: 'a list -> ('a * 'a -> bool) -> 'a tree , but I'm having some problems with it, here are the code I wrote: datatype 'data tree = EMPTY | NODE of 'data tree * 'data * "data tree; fun makeBST [] f = EMPTY | makeBST (x::xs) f = let fun insert EMPTY x = NODE(EMPTY, x, EMPTY) | insert (NODE(left, root, right)) x = if f(x, root) then insert left x else insert right x in makeBST xs

Trying to Define `$` Type

六月ゝ 毕业季﹏ 提交于 2019-12-25 01:57:59
问题 Using Standard ML of New Jersey (v110.77), I'm trying to define the $ data type from Purely Functional Data Structures: datatype alpha susp = $ of alpha But I get an error: - datatype alpha susp = $ of alpha; stdIn:1.11-2.7 Error: syntax error: deleting ID ID EQUALOP What am I doing wrong? 回答1: In Standard ML, type variables are prefixed with a single quote: datatype 'alpha susp = $ of 'alpha 来源: https://stackoverflow.com/questions/28802305/trying-to-define-type

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