Questions tagged [smlnj]

Standard ML of New Jersey (SML/NJ)

SML/NJ is a popular implementation of the Standard ML programming language that was co-developed jointly by Bell Laboratories and Princeton University. It is released under a BSD like license.

References:

Wiki page

866 questions
3
votes
1 answer

Pattern matching in SML - representing a list as (x::y)

I just started learning functional programming and I find myself very confused by the concept of pattern matching (i'm using SML). Take for example the following expression to insert an element in an ordered list: fun insert (n,nil) = [n] | insert…
macalaca
  • 988
  • 1
  • 13
  • 31
3
votes
3 answers

Confusing SML statement

I have this statement: let val x = let val x = 5 in(fn y =>(y,x+y)) end in let val y=3 and z=10 in x z end end; The output is : (10,15) I've been trying to track how this answer was produced but am getting…
user2796815
  • 465
  • 2
  • 11
  • 18
3
votes
2 answers

SML map function

I have the function: map(map(fn x =>[x])) [[],[1],[2,3,4]]; Which produces: val it = [[],[[1]],[[2],[3],[4]]] I don't understand how this function works. Doesn't each map function need both a function and a list? It seems that there isn't enough…
user2796815
  • 465
  • 2
  • 11
  • 18
3
votes
1 answer

How do I get an unhandled exception to be reported in SML/NJ?

I have the following SML program in a file named testexc.sml: structure TestExc : sig val main : (string * string list -> int) end = struct exception OhNoes; fun main(prog_name, args) = ( raise OhNoes …
3
votes
1 answer

Better display of boolean formulas

I want to implement a method for showing a propositional formula in SML. The solutions that I found so far was of this type: fun show (Atom a) = a | show (Neg p) = "(~ " ^ show p ^ ")" | show (Conj(p,q)) = "(" ^ show p ^ " & " ^ show q ^ ")" |…
TheAptKid
  • 1,559
  • 3
  • 25
  • 47
3
votes
4 answers

Filtering elements of one list by looking at boolean values from the second list

I have two lists of equal length. I want to filter the elements of the first list by looking, if the element, with the same index in the second list, has a true boolean value. Example: [1,2,3,4,5]:int list [true,false,false,true,false]:bool…
TheAptKid
  • 1,559
  • 3
  • 25
  • 47
3
votes
1 answer

Does using closures in sml affect the speed of running code in the interactive compiler?

If I have a program say: fun foo x = let fun bar y = y * y in x + (bar x) end foo 123; When I run this program in SML/NJ without compiling, will it have to process the declaration of bar each time foo is called? As it…
3
votes
2 answers

SML/NJ: How to use HashTable?

I really want to create a HashTable in SML, it seems there already is a structure for this in SML/NJ. The question is, how do I use it? I've not fully understood how to use structures in SML, and some of the very basic examples in the book I read…
Horse SMith
  • 1,003
  • 2
  • 12
  • 25
3
votes
3 answers

How do I use the Queue library in SML/NJ

I see that the SML/NJ includes a queue structure. I can't figure out how to use it. How do I use the additional libraries provided by SML/NJ?
Joshua
  • 26,234
  • 22
  • 77
  • 106
3
votes
1 answer

Using folding procedures in sml

fun Dbt (nil,_) = nil | Dbt (x::xs,y::ys) = (x::y)::(Dbt(xs,ys)) | Dbt (x::xs,nil) = [x]::(Dbt(xs,nil)); Is there a way of defining this function non-recursively by using higher order and or in built functions in sml??I have tried all I can…
Emma
  • 323
  • 3
  • 16
3
votes
2 answers

Understanding sigs and structs with casting a built-in list type to a custom stack type

Suppose I have the beginning of the definition for a Stack like the following: signature STACK = sig type 'a stack end; structure Stack :> STACK = struct type 'a stack = 'a list end; Clearly this doesn't work, because I can't cast a list to a…
AJcodez
  • 31,780
  • 20
  • 84
  • 118
3
votes
3 answers

SML/NJ - Pattern Matching an Dynamic Typing

Is it possible to write functions with dynamically typed input parameters? I tried pattern matching, but apparently it does not work like this. I wish to do something like this: fun firstStr (0,n:string) = n | firstStr (b:string,n:string) = if b>n…
Artium
  • 5,147
  • 8
  • 39
  • 60
3
votes
1 answer

Why do I have unexpected '#' in the output for this code?

I am trying to create a deriv function to differentiate using a datatype as follows: datatype Symex = RCOEFF of real | COEFF of string | VAR of string | POWER of Symex * int | NEG …
KVV
  • 61
  • 4
3
votes
2 answers

filtering values into two lists

So i'm new to sml and am trying to understand the ins/out out of it. Recently i tried creating a filter which takes two parameters: a function (that returns a boolean), and a list of values to run against the function. What the filter does is it…
theStig
  • 612
  • 1
  • 13
  • 33
3
votes
2 answers

Signature inside of a structure

I want to place signature/structure pair inside a structure, like so: structure Outer :> OUTER = struct signature INNER = sig ... end structure Inner :> INNER = struct ... end end but even the simplest of…
Andrew Keeton
  • 22,195
  • 6
  • 45
  • 72