Search code examples
smlsmlnj

Checking if a char is a digit


As part of a practice SML exam I was asked to write a function that checks if a character 'c' is a digit or not.

I was looking into the Char.ord function but it returns the ASCII code and not the integer itself in the character, and I can't seem to check the type.

I looked around it said to use a datatype wrapper, but when implementing this into a function; i didn't get the desired result.

Thanks :)


Solution

  • You could compare the ASCII code you get. If it is a digit, it will be between a range of values (48 - 57 I think). Check the number you get with the number's code in the table.

    Here is an example function:

    fun check x = (ord x > 47) andalso (ord x < 58);