OCaml Test if the line is almost empty or contains keywords

I have a problem in OCaml, I'm currently studying it, but I'm still a beginner. I would like to make a function returning true if the string is empty or contains only spaces and at the same time removes any occurrence beginand end.

I already tried this:

let isEmptyString s =  
  let rec empty i =
    if i < 0 then true
    else
          let c = String.get s i in
          if c = ' ' || c = '\009' then empty (pred i)
          else false
  in
  s = Str.global_replace( Str.regexp "begin") "" s;
  s = Str.global_replace( Str.regexp "end") "" s;
  empty (pred (String.length s))

But, obviously, this function does not work as we would like, because after calling it beginin Formula.importNrAgentsFormulaI got more Formula.importNrAgentsFormula... Here is my way to name it:

while true do
   let input = read_line () in
   if not (isEmptyString input) then
      let (nr, f) = Formula.importNrAgentsFormula input in
      incr counter;
      flush stdout;
      match choice with
       | "graph" -> printRes (Graph.isSat ~verbose:verb nr f)
       | _ -> printUsage ()
   else ()
done

If someone who has more experience in OCaml could notice and explain a mistake to me, I would be happy :)

Thanks in advance,

Regards.

+4
source share
2

isEmptyString (isBlankString ?) , ( , ), . :

while true do
   let input = read_line () in
   let input = Str.global_replace( Str.regexp "begin") "" input in
   let input = Str.global_replace( Str.regexp "end") "" input in
   if not (isEmptyString input) then
   ...

. , :

OCaml, :

Warning 10: this expression should have type unit.

s = Str.global_replace( Str.regexp "begin") "" s;. , = OCaml , , . OCaml e1 e1;e2 unit, .

OCaml , :

  • , @Jason : let t = Str.global_replace( Str.regexp "begin") "" s
  • "shadow" , : let s = Str.global_replace( Str.regexp "begin") "" s
  • ( ): let s = ref "before" in s := "after", , , !: !s. , , - OCaml, .
+3

, , , , :

val global_replace : regexp -> string -> string -> string

, ";" , -

let () = print_endline("foobar")

, let, s. , , . - :

    let t = (Str.global_replace( Str.regexp "begin") "" s)

, . , , true false, ( , ). , , - , . , "begin" "end" . (bool, string). (,

let b,s = isEmptyString "foobar" in
if not b then:
  rest of your code

, . - ? (, , , ). , , ( OCaml

fun

:). , , OCaml , . , .

+2

All Articles