Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert negative integers to strings in SML with minus sign instead of tilde?

Tags:

sml

smlnj

The standard SML library function Int.toString prefixes negative numbers with ~ instead of -. Is there a library function to use - instead, short of writing

fun i2s i =
    if i < 0 then "-" ^ Int.toString (~i) else Int.toString i
like image 522
Jay Lieske Avatar asked Nov 19 '11 07:11

Jay Lieske


1 Answers

In short, No.

SML is designed to use ~ for unary minus to avoid confusion with - (binary minus). It's a sensible decision when you have each operator for only one purpose and SML users have to live with that.

Although it's strange to read a string representation of an integer starting with ~, there's no library function to convert it to a string in the normal convention. BTW, your function is a good way to do so.

like image 127
pad Avatar answered Sep 22 '22 22:09

pad