Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] Calling a function with a self-defined type as argument
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Oliver Bandel <oliver@f...>
Subject: [Caml-list] Calling a function with a self-defined type as argument
Hello,

after rewriting a perl-program into Ocaml,
I tried it in different ways.
After getting a minimal working-version ready,
I tried some OO-stuff today first time in Ocaml.
That's nice. I like it. Normally OO is annoying,
but in OCaml, it's really fun. :)

But during rewriting the program, I saw that
I could do some advantages in using own types.
I had tried it before, but had no success
with using  "try ...with ..."  and the self
defined type.

But today I had better Ideas and so I started
concentrating on working with the type, and
after that maybe add some OO-stuff (if necessary/
helpful).

During experimenting with the type, I experienced a
- for me - strange behaviour. You can see my questions
in the last lines of the code (see below).


If you have other comments (optimizing code, better indenting-style),
please let me know.

(The program-code is not complete, here; only the necessary
stuff for showing my question is in this mail.)

###########################################################################
###########################################################################
###########################################################################
let suchstring = if Array.length Sys.argv > 1 then Sys.argv.(1) else ""

let inch = open_in "de-tex-faq.txt"  (* where to read from (inch: in-channel) *)

type contents =   EMPTY
                | Line of string
                | Empty_Line
                | EOF

type inputline = { mutable line: contents; mutable lastline: contents }

let il = { line = EMPTY; lastline = EMPTY }  (* il: inputline *)



(* rl: read line *)

let rl channel =
  let content = try Line(input_line channel) with
                 | _ -> EOF
     in
      let res content = match content with
       | Line ("")  -> Empty_Line
       | l   -> l                  (* should match all other stuff *)
       in
         il.lastline <- il.line;
         il.line <- content


let x y = match y with
  | EOF        -> print_string "EOF!!\n"
  | Empty_Line -> print_string "Empty_Line!!\n"
  | EMPTY      -> print_string "unexpected EMPTY-case!\n"
  | Line txt   -> print_string txt

(* yes, "x" is a stupid name, but it was only a test until now. *)


let _ = x Empty_Line;;   (* expected behaviour *)
let _ = x EOF;;          (* expected behaviour *)
let _ = x EMPTY;;        (* expected behaviour *)

(*
   let _ = x Line "h0oifdaji oi" ;;
   This last one does not work: => "This function is applied to too many arguments"

   let _ = x Line( "reuruhjf" ) ;;
   This last one does not work: => "This function is applied to too many arguments"

   But it works in this way:
   let _ = x (Line "reuruhjf") ;;

   WHY?         (why *only* that?)
  
   At least the second example (with parantheses around the
   Line's arguments) should work...?!  
*)

###########################################################################
###########################################################################
###########################################################################



Ciao,
   Oliver

P.S.: I love that language. It's really fine :)

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners