Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function declaration without arguments

Tags:

sml

How do I declare a function in the signature for that doesn't take arguments?

I've only seen function signatures with arguments like this: leq:item*item->bool and I am looking to create a signature for function like this:

initBTree = E   (* where empty is of type tree *)

this doesn't work: val initBTree:->tree

like image 615
Alicester4WonderlandPresident Avatar asked Feb 23 '26 17:02

Alicester4WonderlandPresident


2 Answers

You can make a function that takes unit as its parameter, like this:

fun initBTree () = E

And call it like this:

initBTree ()

It has type

fn : unit -> tree

If E has type tree.

That's kinda pointless, though. You might as well just say E, or if you really want it to be called initBTree:

val initBTree = E
like image 139
Tayacan Avatar answered Feb 26 '26 06:02

Tayacan


As you probably know all functions in SML takes exactly one argument. Thus creating a function that takes no arguments is impossible, as such a "thing" would actually just be a value.

Your code

val initBTree : -> tree

makes absolutely no sense. If you say that you have a value constructor E which is the empty tree, why would you wan't to create an init function that doesn't initialise a tree with any thing? In that case initBTree would be a synonym for E and you could do

val initBTree = E

However this would still be pointless.

like image 45
Jesper.Reenberg Avatar answered Feb 26 '26 05:02

Jesper.Reenberg