VictorNicollet / 99-Problems-OCaml

Solving "99 List Problems" using Objective Caml
http://www.ic.unicamp.br/~meidanis/courses/mc336/2006s2/funcional/L-99_Ninety-Nine_Lisp_Problems.html
Other
84 stars 14 forks source link

Problem 96 #3

Closed MassD closed 10 years ago

MassD commented 10 years ago

Hi Victor

If you have time, could you please have a look at problem 96, the syntax checker?

Syntax checker. (medium)

In a certain programming language (Ada) identifiers are defined by the syntax diagram >(railroad chart) opposite. Transform the syntax diagram into a system of syntax >diagrams which do not contain loops; i.e. which are purely recursive. Using these >modified diagrams, write a function identifier : string -> bool that can check whether or >not a given string is a legal identifier.

More example is here: http://www.haskell.org/haskellwiki/99_questions/95_to_99

I have solved this problem here:

https://github.com/MassD/99-Problems-OCaml/blob/master/91-99/p96.ml

do you think my solution is correct? I feel it is too simple. If you believe it is correct, I can produce a pull request.

VictorNicollet commented 10 years ago

At a first glance, I'd rename and rewrite your is_letter_dig function:

let is_alphanumeric = function 'a' .. 'z' | 'A' .. 'Z' | '0' .. '9' -> true | _ -> false

For readability, I'd move the n <> 0 test outside the recursive function.

Other than that, it looks pretty good. Thanks for your contributions !

VictorNicollet commented 10 years ago

Post-scriptum: make sure 9a is detected as invalid because it starts with a digit.

MassD commented 10 years ago

Thanks for the suggestions.