ml

How to convert a string to integer list in ocaml?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-31 04:44:08
问题 I need to pass two list as command line arguments in ocaml. I used the following code to access it in the program. let list1=Sys.argv.(1);; let list2=Sys.argv.(2);; I need to have the list1 and list2 as list of integers. I am getting the error This expression has type string but an expression was expected of type int list while processing. How can I convert that arguments to a list of integers. The arguments are passed in this format [1;2;3;4] [1;5;6;7] 回答1: Sys.argv.(n) will always be a

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

Standard ML : How to access column from list of list in standard ML

折月煮酒 提交于 2019-12-24 11:47:13
问题 Suppose I have a list of list list = [[1,2,3],[4,5,6],[7,8,9]] and i want to calculate the sum of the columns. i.e The first column is [1,4,7]and its sum is 12 Second column is [2,5,8] ans sum is 15 and so on Is there any efficient way(with less complexity) in standard ML to do this?? Please help 回答1: For example: fun transpose [] = [] | transpose ([]::xss) = [] | transpose xss = map hd xss :: transpose (map tl xss) val sum = foldl op+ 0 val sumsOfColumns = map sum o transpose Example use:

How can I update lists in SML using functions?

有些话、适合烂在心里 提交于 2019-12-24 06:27:02
问题 I need to write an SML function that looks like this: update(FLR, (x,y)) Where FLR is a finite list of tuples that looks like the following: [(1,1),(2,4),(3,9),(4,16)] But it can contain any number of tuples. What this update function needs to do is take in the list of tuples as the first argument and an x / y tuple as the second argument. If there is a tuple in the list that has the same x value as the one given to the function, it needs to update the y value of the list to the y value given

How can I update lists in SML using functions?

╄→гoц情女王★ 提交于 2019-12-24 06:26:09
问题 I need to write an SML function that looks like this: update(FLR, (x,y)) Where FLR is a finite list of tuples that looks like the following: [(1,1),(2,4),(3,9),(4,16)] But it can contain any number of tuples. What this update function needs to do is take in the list of tuples as the first argument and an x / y tuple as the second argument. If there is a tuple in the list that has the same x value as the one given to the function, it needs to update the y value of the list to the y value given

How can I update lists in SML using functions?

ε祈祈猫儿з 提交于 2019-12-24 06:26:00
问题 I need to write an SML function that looks like this: update(FLR, (x,y)) Where FLR is a finite list of tuples that looks like the following: [(1,1),(2,4),(3,9),(4,16)] But it can contain any number of tuples. What this update function needs to do is take in the list of tuples as the first argument and an x / y tuple as the second argument. If there is a tuple in the list that has the same x value as the one given to the function, it needs to update the y value of the list to the y value given

Shortest subarray containing all elements without using arrays?

删除回忆录丶 提交于 2019-12-24 03:19:00
问题 Problem: Find the length of the shortest subarray that contains all elements Example : 1 2 2 3 2 2 1 3 Answer : 3 I have read that the best approach to this problem is by using the sliding window approach. But this approach requires using arrays. Is there any other efficient approach that does not require to use arrays by storing the number of appearances of each element? ( I would like to use this approach without arrays by writing it in ML ) 回答1: I assume that the reason you want to avoid

How to define the type profile for this function?

匆匆过客 提交于 2019-12-24 00:01:35
问题 I have to define the type profile of this function: twice f x = f (f x); The result should be the following, but I don't really get why. ('a -> 'a) -> 'a -> 'a 回答1: (a -> a) -> a -> a is the right answer. Let's split it to pieces to find out why. your function takes two arguments, f and x , so the signature will have three parts - say, a -> c -> d first of these arguments is an unary function - that makes a = (a -> b) (remember that a can be any type, as long as it appears only once in the

What is the difference between 'a and ''a in SML?

风流意气都作罢 提交于 2019-12-22 10:58:24
问题 For example: fun example (a:'a list) : list = a will have a signatures of: 'a list -> 'a list What if I define it differently but with same content like fun example (a : ''a list) : list = a its signature will be: ''a list -> ''a list What's the difference? 回答1: A plain type variable like 'a can be substituted with an arbitrary type. The form ''a is a so-called equality type variable, which means that it can only be substituted by types that admit the use of the equality operator = (or <> )