jcreigh has quit ["Cuius rei demonstrationem mirabilem sane detexi. Hanc marginis exiguitas non caperet."]
pango_ has joined #ocaml
pango has quit [Remote closed the connection]
bzzbzz has quit ["leaving"]
jcreigh has joined #ocaml
svref has joined #ocaml
<svref>
How do I read STDIN into a big string, so I can operate on it with the Str.regexp functions?
mwc has quit [Remote closed the connection]
<svref>
I am totally new to Ocaml and I can't find my way to basic file I/O stuff like open "filename.txt"... help!
<Smerdyakov>
First, it's probably not advisable to be writing impure programs while "totally new to OCaml."
<Smerdyakov>
And file IO requires impurity in OCaml.
<svref>
impurity=side effects?
<Smerdyakov>
Yes.
<svref>
Do you worry for my immortal soul, or is it somehow harder than in other languages?
<Smerdyakov>
Impurity makes programs harder to understand.
<svref>
well, a program that doesn't work is easy to understand, but not very useful. :)
<svref>
I just need to read a bunch of files to make a hash table. Once I have the hash table created, then I will _rule the world_ with functional programming, I promise. :)
<Smerdyakov>
I'm trying to get a URL for the manual section on the Pervasives module, which is opened initially, but the web server is too slow; it's this module which has the basic IO code.
<svref>
AHA!
<svref>
That explains why I can't find it, its in Pervasisves. I forgot about that, cause, its, well, pervasive.
<Smerdyakov>
Do you have prior experience with functional programming?
<svref>
yeah, I'm an old lisp guy.
<Smerdyakov>
Well, there are some common Lisp styles that are very un-functional...
<dylan>
svref: man Pervasives, if you're on *nix
<Smerdyakov>
In general, the things that folks new to ML find tricky are completely independent of IO.
<Smerdyakov>
And it's a waste of time to write programs that do IO.
<svref>
yep, but if there's a good non-imperative way to break a file up into a sequence of words, I'm all ears, but I think imperative is the order of the day here. :(
<svref>
(word = [a-zA-Z+])
<Smerdyakov>
If you want to learn ML properly, you won't deal with files for a while.
<Smerdyakov>
Thinks of functions from typed data structures, in place of reading files.
jcreigh_ has joined #ocaml
<svref>
ah, it turns out they're called "channels" instead of "streams"...
<Smerdyakov>
Why are you writing a program that uses files?
<svref>
because I have a problem I need to solve that envolves reading files! I don't know how to explain it better than that! :)
<svref>
I promise to write several binary tree searches after this in penance.
<Smerdyakov>
What is the problem, and why does it involve reading files at a fundamental level?
<svref>
I want to write a program that reads a series of filenames from the command line, reads them in, performs textual analysis, and spits out a modified stream of text. Think "jive filter".
<Smerdyakov>
Sounds to me like a stream transformer, which can be purely functional.
<Smerdyakov>
(Use your shell to do file IO)
jcreigh has quit [Read error: 110 (Connection timed out)]
<svref>
you're saying do something like "sed 's/foo/bar/g'" to do stream editing?
<Smerdyakov>
Yes.
<Smerdyakov>
You would need to write a small function from a stream transformer to an imperative program, but that would be the only portion requiring imperativity.
<svref>
yes, but the thing I need to write is a bit trickier than a jive filter,...augh, its too complex to explain in IRC
<svref>
basically, I analyze pairs of words that appear in a training text. Then I recombine those pairs in unexpected ways to generate semi-non-sensical text. Its fun.
<Smerdyakov>
I leave now, with this parting advice: modularize your program so that as many modules as possible are pure, and try to keep the impure modules as small as you can.
<dylan>
Sounds like a lovely exercise.
<svref>
okay, thanks Smerdy
<svref>
So it seems like the regexp features in Str.regexp don't really work on channels, only on strings. So I have to read a whole file into a huge buffer...ugly. Maybe easier to ditch regexps and just read individual chars.
pango_ has quit [Remote closed the connection]
sylvan has joined #ocaml
tristram has joined #ocaml
Smerdyakov has quit ["Leaving"]
pango_ has joined #ocaml
smimou has joined #ocaml
jcreigh_ has quit ["Cuius rei demonstrationem mirabilem sane detexi. Hanc marginis exiguitas non caperet."]
cmeme has quit [calvino.freenode.net irc.freenode.net]
Hadaka has quit [calvino.freenode.net irc.freenode.net]
shekmalhen has joined #ocaml
cmeme has joined #ocaml
Hadaka has joined #ocaml
Naked has joined #ocaml
Hadaka has quit [Read error: 111 (Connection refused)]
Naked is now known as Hadaka
svref has quit ["Client exiting"]
rillig has joined #ocaml
pango_ has quit [Remote closed the connection]
shekmalhen has quit ["Snakes on a Plane sti... des serpents sur un avion! Fallait y penser."]
mnemonic has joined #ocaml
<mnemonic>
hi
pango has joined #ocaml
smimou has quit ["bli"]
Revision17 has joined #ocaml
revision17_ has quit [Read error: 110 (Connection timed out)]
hikozaemon has quit ["Leaving..."]
mnemonic has quit ["leaving"]
mnemonic has joined #ocaml
mnemonic has quit [Client Quit]
mnemonic has joined #ocaml
Snark has joined #ocaml
dark_light has quit [Remote closed the connection]
mnemonic has quit ["leaving"]
woggle has quit [Connection timed out]
ppsmimou has quit ["Leaving"]
smimou has joined #ocaml
cjeris has joined #ocaml
pango has quit ["Leaving"]
Hadaka has quit [Remote closed the connection]
pango has joined #ocaml
cjeris has left #ocaml []
finelemon has joined #ocaml
finelemo1 has quit [Read error: 110 (Connection timed out)]
svref has joined #ocaml
<svref>
# Str.regexp "[\n \t]";;
<svref>
Reference to undefined global `Str'???
<zmdkrbou>
you have to use : "ocaml str.cma" as a command line
<svref>
tahnks!
chessguy has joined #ocaml
<zmdkrbou>
(this is the case for unix module too)
<pango>
or #load "str.cma" ;;
<svref>
type word_or_number = Sentance of string | Num of int
<svref>
List.map (function x -> Phrase x) ["foo"; "bar"];;
<svref>
That works, but this doesn't:
<svref>
List.map Phrase ["foo"; "bar"];;
<svref>
So "Phrase" isn't really a function, is it?
<dylan>
How do you define Phrase?
<pango>
beware of "function", you probably mean "fun"
<pango>
dylan: I guess Phrase = Sentance
<dylan>
Ah.
<pango>
svref: Phrase is a constructor, and indeed it's not a function
<dylan>
variant constructors (Num and Sentance) arn't first class in ocaml.
<dylan>
they were in caml light, and they are in haskell. But it's not much overhead to write fun x -> Sentence x
<zmdkrbou>
(and sentence is spelled with a 'e' :p)
asbeta has joined #ocaml
<pango>
(mmmh well actually in this context, "function" does work...)
Snark has quit ["Leaving"]
<svref>
thanks for figuring out what I meant. :)
* svref
looks up the difference between fun and function
<asbeta>
it's in the manual :)
<zmdkrbou>
function <matching> is a abbreviation for fun x -> match x with <matching>
<Tekhne>
i'm very new to ocaml, and i'm trying to install and use the ocaml-mysql module, but i get the error "Unbound module Mysql" when compiling the demo.ml that comes with it. can i assume i've installed ocaml-mysql incorrectly, or am i missing something?
maml has joined #ocaml
<rillig>
Tekhne: you need to specify the mysql module on the command line.
<Tekhne>
as an argument to ocamlc?
<rillig>
I think yes.
<rillig>
maybe the file is called mysql.cms
<rillig>
maybe the file is called mysql.cma
<Tekhne>
ok. thanks. i'll look into that. maybe i should even get further along in the tutorials before i try stuff like this =]
<rillig>
Tekhne: you should in any case record everything you do to a file, for later reference.
<rillig>
:)
<Tekhne>
hehe, yea =]
<rillig>
on Unix-like system, script(1) provides useful.
<rillig>
does it work even if you leave out the -I option and the path?
<Tekhne>
i'll try now...
<Tekhne>
hmm, nope
<rillig>
same for me. :)
<Tekhne>
so, i take it that all ocaml programs must be compiled in some way. that is, you can't use the "#!/usr/local/bin/ocaml" trick at the top of a source file and then just run the source file, right?
<pango>
Tekhne: that works
<Tekhne>
oh. hmm.
<Tekhne>
i must be doing something silly then
Revision17 has quit ["Ex-Chat"]
<pango>
Tekhne: however it's not the preferred way... First, it will be compiled each time it's run... And toplevel does byte-compilation, not native compilation
<Tekhne>
ok. makes sense.
<pango>
Tekhne: there's actually a package called camlscript (or ocamlscript ?) that removes both limitation
<pango>
Tekhne: caching native compiled version when script is not modified
<Tekhne>
like python?
<pango>
is python native compiled ?
<pango>
thought it was some kind of bytecode
<Tekhne>
oh, well. no, but it caches the byte-compilation
<Tekhne>
but only if the file ends in .py
<Tekhne>
then you get a .pyo
<pango>
which is probably some kind of bytecode
<pango>
like .cma's
<Tekhne>
ok, my two-liner is tossing a shell error: '#!/usr/local/bin/ocaml\nprint_endline "Hello world!"'
<Tekhne>
pango: yes, .pyo's are bytecode
<pango>
did you chmod +x the file ?
<Tekhne>
pango: yea
<Tekhne>
called it ocaml_test
<Tekhne>
./ocaml_test gives:
<rillig>
Tekhne: what's the exact error message?
<rillig>
:)
<Tekhne>
./ocaml_test.ml: line 2: print_endline: command not found
<Tekhne>
ok, called it ocaml_test.ml =]
<rillig>
I think /usr/local/bin/ocaml is just a shell script itself.
<rillig>
look at the file to find out.
<rillig>
it must be a native (ELF) binary, otherwise it cannot be used in #! scripts.
<Tekhne>
/usr/local/ocaml-3.09.2/bin/ocaml: a /usr/local/ocaml-3.09.2/bin/oca script text
<rillig>
indeed.
<pango>
$ cat hello_world.ml
<pango>
#!/usr/bin/ocaml
<pango>
print_endline "Hello world!"
<pango>
$ ./hello_world.ml
<pango>
Hello world!
<Tekhne>
i see. well, is there a way of creating the native binary from the ocaml source?
<pango>
ocamlopt
<rillig>
pango: in my installation, bin/ocaml is a script as well.
<rillig>
*wondering*
<pango>
ocamlc is bytecode compiler, ocamlopt is the native one
<Tekhne>
ah, ocamlopt
<Tekhne>
i'll try that
<pango>
rillig: shouldn't matter
<Tekhne>
hmm same error. ocamlopt is of the same type as ocaml
<Tekhne>
hmm, maybe ocamlopt.opt
<rillig>
Tekhne: run "file /usr/local/ocaml-*/bin/*" and look for files that are ELF executables.
<pango>
rillig: when the executable attribute is seen by the shell, it's exec()ed; the kernel executable loader notices the #! signature, and exec the mentionned interpreter with current file name appended to parameters...
<Tekhne>
rillig: there are a number. perhaps ocamlrun?
<rillig>
pango: yes, and it requires that the script interpreter is an ELF executable, as far as I remember.
<rillig>
Tekhne: just try it.
<pango>
rillig: I don't see why it couldn't happen several times in a row (exec() using exec() ...)
<pango>
$ cat /tmp/myownocaml
<pango>
#!/bin/bash
<pango>
exec /usr/bin/ocaml "$@"
<Tekhne>
here are the ELFs i have: camlp4o.opt camlp4r.opt ocamlc.opt ocamldep.opt ocamldoc.opt ocamllex.opt ocamlopt.opt ocamlrun ocamlyacc
<pango>
$ cat hello_world.ml
<pango>
#!/tmp/myownocaml
<pango>
print_endline "Hello world!"
<pango>
$ ./hello_world.ml
<pango>
Hello world!
<Tekhne>
ocamlrun appears to want bytecode
<pango>
Tekhne: yes, bytecode executables start with #!.../ocamlrun
<pango>
it's the bytecode "runtime"
<Tekhne>
but #!/usr/local/bin/ocamlrun doesn't work
<rillig>
pango: what system are you using?
<Tekhne>
as the first line of my script
<pango>
Tekhne: unless you know how to write bytecode manually, indeed
<Tekhne>
hehe, yea
<Tekhne>
this works: echo 'print_endline "Hello world!";;' | ocaml
<Tekhne>
of course, i get REPL garbage
<Tekhne>
but it works
<Tekhne>
hmm
<Tekhne>
it's not a big deal. i was just wondering. i can compile my ocaml programs.
<Tekhne>
it would just make it easier to mess with ocaml
<pango>
Makefiles... and when projects get bigger, they're tons of tools to generate Makefiles, or replace them with something else ;)