Anonymous user
Modulinos: Difference between revisions
→{{header|OCaml}}: more reasonable example using Sys.interactive
(→{{header|OCaml}}: more reasonable example using Sys.interactive) |
|||
Line 1,030:
scriptedmain.ml
<lang ocaml>
Printf.printf "Main: The meaning of life is %d\n"
meaning_of_life
if not !Sys.interactive then
main ()</lang>
Invoked as a script:
./scriptedmain.ml▼
<lang sh>$ ocaml scriptedmain.ml
Main: The meaning of life is 42</lang>
Loaded into an ocaml toplevel/utop:
<lang>$ ocaml
...
val main : unit -> unit = <fun>
# meaning_of_life;;
- : int = 42
# </lang>
The limit of this technique is "avoiding running something when loading a script interactively". It's not applicable to other uses, like adding an example script to a file normally used as a library, as that code will also fire when users of the library are run.
▲let meaning_of_life : int = 42
▲let _ =
▲ main ()
=={{header|Octave}}/{{header|MATLAB}}==
|