Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does OCaml have a module that is like isdigit and isalpha in C/C++?

I have been teaching myself OCaml and having been trying to look through the Pervasives module and others modules to find a module that contains a function that is like C/C++ functions isdigit and isalpha in an attempt to clean up my code a little.

As one of my conditional statements for checking for a letter looks like:

if token > 64 && token < 91 || token > 96 && token < 123 then (* Do something * )

Does OCaml have a module that would be the equivalent of isdigit and isalpha ?

like image 255
TurtleMan Avatar asked Mar 08 '18 23:03

TurtleMan


People also ask

What is Isdigit in C?

The isdigit(c) is a function in C which can be used to check if the passed character is a digit or not. It returns a non-zero value if it's a digit else it returns 0. For example, it returns a non-zero value for '0' to '9' and zero for others. The isdigit() is declared inside ctype. h header file.

What are modules in OCaml?

A module can provide a certain number of things (functions, types, submodules, ...) to the rest of the program that is using it. If nothing special is done, everything which is defined in a module will be accessible from the outside.

What is Isdigit and Isalpha?

isalpha() and isdigit() in C/C++It returns an integer value, if the argument is an alphabet otherwise, it returns zero. Here is the syntax of isalpha() in C language, int isalpha(int value); Here, value − This is a single argument of integer type.

How does Isalpha work in C?

C isalpha() In C programming, isalpha() function checks whether a character is an alphabet (a to z and A-Z) or not. If a character passed to isalpha() is an alphabet, it returns a non-zero integer, if not it returns 0. The isalpha() function is defined in <ctype.


2 Answers

One solution is to use pattern-matching over character ranges:

let is_alpha = function 'a' .. 'z' | 'A' .. 'Z' -> true | _ -> false
let is_digit = function '0' .. '9' -> true | _ -> false
like image 185
octachron Avatar answered Oct 21 '22 08:10

octachron


Using @octachron solution of using pattern-matching I was able to create my own isalpha and isdigit functions to clean up my code.

Solution code:

(* A function that will validate if the input is a ALPHA *)
let is_alpha alpha = 
    match alpha with 
      'a' .. 'z' -> true
    | 'A' .. 'Z' -> true 
    | _ -> false;;

(* A function that will validate if the input is a DIGIT *)
let is_digit digit =
    match digit with
     '0' .. '9' -> true
    | _ -> false;; 

let get_token token =
         if (is_alpha (char_of_int token)) = true 
             then    (* Checking if a APLHA was found *)
                begin
                    Printf.printf("Valid Token Alpha found: %c\n") (char_of_int token);
                end
         else (is_digit (char_of_int token)) = true 
               then    (* Checking if a Digit was found*)
                  begin
                       Printf.printf("Valid Token Digit found: %c\n") (char_of_int token);
                  end
         else
            begin
                Printf.printf("Invalid Token found: %c\n") (char_of_int token);
            end
;;

Thank you for the help in finding a solution to me question @octachron.

like image 23
TurtleMan Avatar answered Oct 21 '22 07:10

TurtleMan