ML is a typed language. Your explicit type annotation determines that the second argument of the function has type int (which actually is already determined implicitly by its comparison to 0, so the annotation is redundant). So obviously, there cannot be anything other than an integer to match, and the...

"Type casting" can mean a lot of different things. There are at least 3 completely different use cases: Converting between specific different types (e.g. numeric types). That is possible in ML through respective functions, they just aren't disguised as "casts" Casting back and forth from/to a top type (like Object...

haskell,functional-programming,sequence,ml

First off, variables in Haskell have to be lower case. Secondly, Haskell doesn't let you mix integers and fractions so freely as you may be used to from untyped or barely-typed languages. If you want to convert from an Int or an Integer to, say, a Double, you'll need to...

This code runs in PolyML 5.2: fun sum_pairs (l : (int * int) list) = if null l then [] else ((#1 (hd l)) + (#2 (hd l))) :: sum_pairs(tl l) (* ------------^-------------^ *) The difference from yours is subtle, but significant: (#1 hd(l)) is different from (#1 (hd l));...

In Standard ML, type variables are prefixed with a single quote: datatype 'alpha susp = $ of 'alpha ...

list,find,ocaml,ml,memory-consumption

Your function consumes constant (O(1)) space, because it's tail recursive. You can read about tail recursion at OCaml.org, here. Update Here is a non-tail-recursive solution: exception Failure of string let rec find x lst = match lst with | [] -> raise (Failure "Not Found") | h :: t ->...

Yes, they can, but the matched list won't be empty, so the result of the null function holds, i.e., [] :: [], which is equivalent to [[]], is not an empty list. No, that's syntactically invalid. However, it can be shortened to this: fun null [] = true |...

You can define the Operator case of the Formula union using an explicit tuple: type Formula = | Operator of (string * Formula * Formula) | Number of double If you do this, the compiler will let you pattern match using both Operator(name, left, right) and using a single argument...

function,functional-programming,sml,smlnj,ml

It's simple indeed: an identifier cannot start with an underscore. So _a is parsed as if you had written _ a, in accordance with the usual maximal munch rule for lexical syntax. Edit: Extra tip: Your function does not have the type 'a list -> 'b, because help returns a...

The -> operator is for function types. a -> b means "a in, b out", so float -> float is the type of functions that take a float as their argument and produce a float as their result. What about float -> float -> float -> is right-associative, so a...

The sml command is intended to be used interactively. It sounds to me like you would be better off building a standalone executable from your program instead. There are a few options: If you are relying on SML/NJ extensions, or if you simply cannot use another ML implementation, you can...

haskell,f#,functional-programming,ml,mutual-recursion

Functional languages usually do so-called proper tail call optimisation, which is completely independent of recursion. Any tail call is optimised into a jump, be it a recursive call, a call to a previously defined function, a partially applied function, or even a call to a first-class function. For example: g...

This will give your desired output. fun map_one [] (old,x) = x | map_one ((p,f)::fs) (old,x) = if p old then map_one fs (old,(f x)) else map_one fs (old,x) fun map_many _ [] = [] | map_many fs (x::xs) = map_one fs (x,x) :: map_many fs xs Note that map_one...

You are probably using an outdated version of Moscow ML. Try version 2.10 or later, which has String.isSubstring.

You can parameterize the type in the signature. But composing monoids then becomes odd or impossible. Haskell solves this using type classes, which you can emulate in SML with functors, but the syntax is heavy (as you've noticed). signature MONOID = sig type 'a monoid val neutral : 'a monoid...

When you see an error of the form: operator domain: <type1> operand : <type2> then it is saying that it is expecting something of type <type1>, but you are giving it something of type <type2>. In your case, get_nth is expecting a tuple, where the first element is a list...

The structure-level equivalent to include is open: structure Foo1 = struct val val1 = "val1" end structure Foo2 = struct val val2 = "foo2" end structure Foo = struct open Foo1 open Foo2 end ...

Don't use ^ to concatenate a bunch of strings in performance critical code, as it will lead to quadratic complexity; Try not to rely on *printf functions, when performance matters (although in OCaml 4.02 it is pretty fast); Don't apply several iterations on a list in a row, since...

The problem is that parse, using scanner, builds a list of pairs — (int * real) list — while tr expects to get a list of triples — (int * 'Z * 'Y) list. Not knowing what tr is supposed to do, the quick and dirty fix would be to...