Anyone knows Ocaml???

Status
Not open for further replies.

neelu09

Broken In
i need some help with ocaml programm...can anyone help
i have to write a programme which will tell whther a date is valid or not
i have written
let datevalid : int*string -> bool = function
(d,m) -> if (d>31 || d<1 || m<>"January"||m<>"February"||m<>"March"||m<>"April"||m<>"May"||m<>"June"||m<>"July"||m<>"August"||m<>"September"||m<>"October"||m<>"November"||m<>"December") then false
else if (d>28 && m="February") then false
else if (d=31 && (m<>"January"||m<>"March"||m<>"May"||m<>"July"||m<>"August"||m<>"October"||m<>"December")) then false
else true
;;


but this function gives false for valid dates also please help
 
Last edited:

chandru.in

In the zone
I have never worked on Ocaml. But I guess || stands for "or" and && stands for "and" in it just like other C-like languages. If that is true you have an error in your logic.

Try this code instead.

Code:
let datevalid : int*string -> bool = function
(d,m) -> if ((d>31 || d<1) || (m<>"January"&&m<>"February"&&m<>"March"&&m<>"April "&&m<>"May"&&m<>"June"&&m<>"July"&&m<>"August"&&m< >"September"&&m<>"October"&&m<>"November"&&m<>"December")) then false
else if (d>28 && m="February") then false
else if (d=31 && m<>"January" && m <> "March" && m <>"May" && m<>"July" && m <>"August" && m<>"October" && m<>"December") then false
else true
;;


Note:
You are also not considering leap years when checking for February.
 
Status
Not open for further replies.
Top Bottom