Loading ...
Sorry, an error occurred while loading the content.

13969testing keyboard input for a digit

Expand Messages
  • Jean Saint-Remy
    Oct 23, 2013

      I am trying to test user's input to see if it contains a digit, but it is not going so well

      let rec kbd () =
        let ls = [0;1;2;3;4;5;6;7;8;9;] (* there doesn't seem to be a way of expressing a range 0..9 *)
        let rec find test = function
          | [] -> raise Not_found
          | hd :: tl -> if test hd then hd else find test tl in
             print_string "Please enter an item number: ";
                let item = read_line () in
                if (item = find ls) then failwith "an item should contain a digit ";
           with Failure msg -> (print_endline (msg ^ ", try again"); kbd ()) ;;

      -: Error This expr "ls" has type int list but an expression was expected of type 'a -> bool

      Thank you for any suggestion

    • Show all 2 messages in this topic