Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typecasting in SML

Tags:

sml

smlnj

I'm new to SML, and am using the SMLNJ dialect.

For some purpose I have been trying to typecast 3 to 3.0 (int to real).

Could not find a way out. How can I do this? How can I convert between types?

like image 479
Dave Avatar asked Jan 24 '12 01:01

Dave


People also ask

How do I create a list in SML?

We write lists as a sequence of integers, separated by commas, all enclosed with two square brackets. So then we have valid int lists as [1, 2, 3] , [1, 5, 1, 5, 0] , and [] , with the latter representing the empty list. We also refer to the empty list as nil (in addition, you can type nil instead of [] in code).

How do you define a function in SML?

Functions in SML We can declare a function with the fun keyword. fun fact (0 : int) : int = 1 | fact (n : int) : int = n * fact (n - 1) The above example serves to initialize a function that computes the factorial function, and then bind it to the identifier fact .


1 Answers

You can use the function real (or Real.fromInt) to convert an int to a real.

For further information you can see a list of functions available in the Top-level environment here and an overview of the Basis library here.

like image 98
sepp2k Avatar answered Nov 07 '22 09:11

sepp2k