sml

Filter function in SML

可紊 提交于 2019-12-25 00:43:23
问题 I use this function to filter a list of integers. I'm beggining in SML and I don't know where is the error. fun filter f = fn [] => [] | fn (x::xs) => if f(x) then x::(filter f xs) else (filter f xs) fun g(x) = if x>5 then true else false val listTest = filter g [1, 2, 4, 6, 8, 10] Thank you! 回答1: Definitions with fn look like definitions with fun , but without the name and an arrow instead of = : fn a0 => e0 | a1 => e1 | ... This would be correct: fun filter f = fn [] => [] | (x::xs) => if f

Questions on SML type ckecking and inference

為{幸葍}努か 提交于 2019-12-24 19:39:01
问题 First of all, since the question is somehow related to a school project I don't think that posting my code is appropriate. Plus, as I explain later on I only have a modified version of the code in question. And I explain myself. I should implement a version of Dijkstra's algorithm using a priority queue. I thought that a simple functional way to do so is define a dijkstra function with inputs the queue and the targeted node and a helper function to enqueue the nodes that are neighbors to the

Inserting values in a Trie

岁酱吖の 提交于 2019-12-24 12:36:20
问题 I found this implementation of a Trie in an SML directory: signature DICT = sig type key = string (* concrete type *) type 'a entry = key * 'a (* concrete type *) type 'a dict (* abstract type *) val empty : 'a dict val lookup : 'a dict -> key -> 'a option val insert : 'a dict * 'a entry -> 'a dict val toString : ('a -> string) -> 'a dict -> string end; (* signature DICT *) exception InvariantViolationException structure Trie :> DICT = struct type key = string type 'a entry = key * 'a

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

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

SML - Get Indices of List

三世轮回 提交于 2019-12-24 02:09:54
问题 I'm working on a program that appends either a '+' or '-' to an element of a list, depending on whether the index of that element is odd or even (i.e an alternating sums list). However, I'm having trouble identifying what the index of each element is. I have code that I believe should append the correct symbol, using if statements and mod fun alternating([]) = 0 | alternating(l) = if List.nth(l,hd(l)) mod 2 == 0 then '+'@hd(l)@alternating(tl(l)) else '-'@hd(l)@alternating(tl(l)) However, List

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

Read command-line arguments in SML

删除回忆录丶 提交于 2019-12-23 21:15:35
问题 I am trying to read the name of my input file that is argv[1] . This is what I’ve done so far : val args = CommandLine.arguments() ; val (x::y) = args ; val _ = agora x but I keep getting this error message : uncaught exception Bind [nonexhaustive binding failure] . Can anyone help ? Thank you in advance ! 回答1: This is the compiler warning you that you can't be sure that the bind pattern always holds. For example, given the following program: val args = CommandLine.arguments () val (x::y) =