buluca has quit [Read error: 110 (Connection timed out)]
seafood has quit []
|jedai| is now known as Jedai
Ched- has quit [Read error: 110 (Connection timed out)]
Ched- has joined #ocaml
threeve has joined #ocaml
ofaurax has quit ["Leaving"]
jeddhaberstro has joined #ocaml
Palace_Chan has joined #ocaml
jeddhaberstro__ has quit [Read error: 110 (Connection timed out)]
threeve has quit []
mbishop has quit [Read error: 113 (No route to host)]
Jedai has quit [Read error: 110 (Connection timed out)]
Jedai has joined #ocaml
Jedai has quit [Connection timed out]
Jedai has joined #ocaml
jeddhaberstro has quit []
rwmjones has joined #ocaml
det has joined #ocaml
seafood has joined #ocaml
seafood has quit [Read error: 104 (Connection reset by peer)]
seafood_ has joined #ocaml
seafood_ has quit []
Linktim has joined #ocaml
|jedai| has joined #ocaml
Jedai has quit [Read error: 110 (Connection timed out)]
Associat0r has quit []
Myoma has quit [Read error: 113 (No route to host)]
Myoma has joined #ocaml
johnnowak has joined #ocaml
johnnowak has quit []
|jedai| has quit [Connection timed out]
|jedai| has joined #ocaml
Palace_Chan has quit ["Leaving"]
SanguineV has quit []
maattd has left #ocaml []
vpalle has quit [Read error: 104 (Connection reset by peer)]
vpalle has joined #ocaml
tomh_-_ has joined #ocaml
Linktim_ has joined #ocaml
Linktim has quit [Read error: 113 (No route to host)]
<det>
et rec fold_lines f accu in_file =
<det>
try
<det>
let line = input_line in_file in
<det>
fold_lines f (f accu line) in_file
<det>
with
<det>
End_of_file -> accu
<flux>
I guess there's a question coming in?
<det>
How can I limit the scope of catching End_of_file to "input_line in_file" without introducing an option type ?
<flux>
a nice question, as indeed not introducing the option type isn't obvious
<flux>
actually I wonder if it can be done without it :)
<det>
Hmm, I feel really stupid :-)
<flux>
let rec fold_lines2 f accu in_file = (try let line = input_line in_file in fun () -> fold_lines2 f (f accu line) in_file with End_of_file -> fun () -> accu ) ()
<flux>
but that may not be what you're looking for
<flux>
you might want to use this kind of construct in general when dealing with exceptions:
<flux>
type 'a value = Value of 'a | of Exn of exn
<flux>
let valuefy f a = try Value (f a) with exn -> Exn exn
Heffalump has left #ocaml []
<Eridius>
huh, that closure idea is interesting. unfortunately it introduces the possibility of stack overflow
<flux>
eridius, how come?
<Eridius>
because you're producing a chain of nested functions
<flux>
then you can do: try match valuefy input_line in_file with Value x -> .. | exn -> ..
<flux>
eridius, what kind of chain?
<Eridius>
...I think. Let me think this through again before I say that definitively
* Eridius
notes it's 5am, so he may be a bit off
<det>
ahh, yes, the closure thing is doable, but better to just use an option type in that case :-)
<Eridius>
whup, nevermind. no chain. I misread it because of the lack of newlines/indentation
<det>
can you pattern match over an exn outside of a with clause ?
<det>
I really dislike functions that signify EOF with an exception rather than an option.
<Eridius>
you could also introduce a new exception type for WrappedException and do something like try let line = input_line in_file in let f_applied = (try f accu line with e -> raise WrappedException e) in fold_lines f f_applied in_file with End_of_file -> accu | WrappedException e -> raise e
<det>
I wonder if an exception is more efficient.
<flux>
I'm thinking many share that dislike
<Eridius>
granted, that's kinda silly
<Eridius>
well, it means you don't have to create a lot of throwaway Option objects when most of the time the function succeeds
<det>
Yeah, that is what I was thinking.
<det>
But, my original function, is not tail-recusrive, right?
<Eridius>
sure it is
<flux>
det, your original function isn't tail-recursive
<Eridius>
does exception handling break tail recursion? I assumed it didn't
<flux>
eridius, it's difficult not to
<det>
How to right my original function in a tail-recursive way without introducing a closure or option type?
<flux>
I suppose it could handle that for some special cases
<det>
write*
<Eridius>
well, it depends on how exception handling works, but yeah I guess in retrospect I should have assumed it does
<flux>
eridius, think what would happend if you added (raise (Value accu)) to the exception handler
<flux>
hm, actually I'm not sure if that would make sense
<Eridius>
if the type system also encapsulated which exceptions could be triggered from a function, it would work
<Eridius>
...I think
<flux>
but anyway, rising anything in the exception handler should bring the issue in front
<Eridius>
if it was known for a fact that the handler could not raise its own exception, then it would work
<Eridius>
det: maybe there's a way to test if the in_file is at EOF before reading? Although from experience with unix file APIs in other languages, I'm inclined to say it doesn't know it's at eof until it tries to read a char
<Eridius>
if you really want to avoid an option, you could do it using a ref
<flux>
eridius, anyway, such a rule might perhaps be more difficult for the developer to understand, so it's best not to do it
<flux>
you might even call a function in the handler that does that rising-of-exception for you
<Eridius>
let rec fold_lines f accu in_file = let line = ref "" in if (try line := input_line in_file; true with End_of_file -> false) then fold_lines f (f accu !line) in_file else accu
<flux>
and then benchmark which one of them is the fastest, including gc time :)
<Eridius>
although if you're going to do that, you may as well drop the recursion entirely
<flux>
btw, here's a function that should demonstrate the case when such functions cannot be tail-recursive:
<flux>
let rec foo n = if n < 0 then failwith "n=0" else try foo (n - 1) with exn -> Printf.ksprintf failwith "%s plus foo=%d\n" (Printexc.to_string exn) n
<Eridius>
flux: again, if the type system contained info about which functions could raise which exceptions, then the compiler could detect when an exception handler was incapable of raising an exception, and in that situation optimize
<Eridius>
but I agree that this would probably confuse the user
<Eridius>
det: let fold_lines f start in_file = let accu = ref start in try while true do let line = input_line in_file in accu := f accu line done with End_of_file -> !accu
<det>
Yeah, it's possible with a ref
<Eridius>
granted, this still wraps the call to f inside the exception handler
<det>
it just seems you should be able to write the equivalent of:
<det>
let rec fold_lines f accu in_file =
<det>
match input_line in_file with
<det>
None -> accu
<det>
| Some line -> fold_lines f (f accu line) in_file
<det>
using th exception
<Eridius>
if you use the closure approach, you can kinda do that
<flux>
or input_line' in_file @@ function | None -> accu | Some line -> fold_lines f (f accu line) in_file with the approriate definitions of (@@) and input_line'..
<Eridius>
what's with (@@)? Just use match (input_line' in_file) with ...
<Eridius>
but this still uses the option type
<Eridius>
I think det wants a way to avoid using an extra object
<det>
I don't think it's possible to do while preserving tail recursion
<Eridius>
or without catching exceptions raised by f
<det>
that too
<Eridius>
well, a potential End_of_file exn raised by f
<Eridius>
if I were you, I'd either drop the recursion and use the while loop, or I'd just use the option type
* Smerdyakov
squeals in delight as his web language compiler compiles a non-trivial program (that uses meta-programming) to code that needs no heap allocation.
authentic has quit [Read error: 104 (Connection reset by peer)]
^authentic is now known as authentic
<kamljunkie>
Laconic!
<Smerdyakov>
I changed the name to Ur/Web.
<kamljunkie>
oh
adema has joined #ocaml
<adema>
hello
<adema>
i need some help with camllight
<Smerdyakov>
Then this must be for a class, so you should ask your teacher instead!
<adema>
it's a syntax problem
<flux>
I guess that's one advantage of "teaching languages", everyone can see that it's a school task ;)
<kamljunkie>
heh:P
<adema>
i just need to know how can i do multiple test is a when clause
<adema>
in a rec function
<Smerdyakov>
adema, did you read the manual?
<flux>
if it's anything like ocaml's when, it's a boolean expression what you stick to it. so use logical and or or operation?
<adema>
not entirely, i tried to find the answer in the manual yes
<adema>
but i found nothing
<kamljunkie>
google ftw!
kamljunkie1 has joined #ocaml
<adema>
|Node (e,g,d) when d=Vide and g=Vide -> e
<adema>
it yeld on the "and"
<Smerdyakov>
adema, you are making guesses about how boolean operators are written in Caml....
kamljunkie1 has quit [Client Quit]
<adema>
well, i thought that boolean expression were like any other boolean expression in almost very language
vpalle has quit [Read error: 110 (Connection timed out)]
<adema>
every*
<Smerdyakov>
I think that's mostly true, and you have a weird idea of how they're written in most other languages.
<Smerdyakov>
But, really, just read the manual to find out for sure.
<Myoma>
adema: You should either drop that assumption or program in languages you already know
<Myoma>
adema: (In general, not just ocaml)
<adema>
well, i went here to ask a question
<adema>
if you don't want to help me
itewsh has quit [Remote closed the connection]
<adema>
then don't help me
<Myoma>
adema: Are you talking to me?
<adema>
i'm talking to guys which are just saying "GIYF" or "RTFM" or "LEARN BOOLEAN EXPRESSION"
<flux>
why, in our opinion that's mighty helpful. you originally thought you had problem with the syntax of when, now you know it's the a problem with boolean expressions :P
<struktured>
adema: they just want you to learn on your own rather than being lazy and asking for answers to very basic questions about caml
<adema>
i'm not lazy! I just asked ONE question
<adema>
and i programmed everyday in other languages, but for school task i need to write a sample code in camllight, i don't have the time to read the entire manual
<struktured>
adema, reading chapter 1 would suffice for your problem
vpalle has joined #ocaml
<flux>
it shouldn't take long to find caml light manual and the approriate section dealing with boolean types from it.
vpalle_ has joined #ocaml
<adema>
i found it, i assumed "and" is just "&&"
Linktim has joined #ocaml
<Myoma>
adema: I thought IRC channels were mostly about talking to people about stuff we're interested in, not .. .. reading manuals for people who are too busy to do it themselves
<adema>
i'm really for asking help ...
<adema>
you're right!
<adema>
no one should ask for help on IRC
<Myoma>
I think asking for help is fine if you intend to do what the people advise ...
<Myoma>
if you ask for advice then tell people they are cruel and evil and they should not have told you that it seems counterproductive
<adema>
i'm not asking for advice
<Myoma>
I know, you're complaining meaninglessly
<adema>
not that boring kinf of adivce "rtfm"
<Smerdyakov>
adema, when it's clear you haven't read the manual, that's the right advice.
<flux>
and then the person has his next question, and next, and next, which all would likely be answered had the person just read the manual in the first place :P
vpalle_ has quit ["Leaving"]
<adema>
do you realize that if you have told me that and was && in caml you and me wouldn't have been talking for so long about what i should do or not
<Myoma>
adema: You are still complaining
<adema>
ahaha
kamljunkie has quit [Connection timed out]
kamljunkie1 has joined #ocaml
<kamljunkie1>
Is ocaml bot running in here?
<flux>
false;;
<kamljunkie1>
ah well..
vpalle has quit [Read error: 110 (Connection timed out)]
threeve has joined #ocaml
Linktim_ has joined #ocaml
Linktim_ has quit [Read error: 104 (Connection reset by peer)]
Linktim_ has joined #ocaml
Associat0r has joined #ocaml
kamljunkie1 has left #ocaml []
Linktim has quit [Read error: 110 (Connection timed out)]
hkBst has joined #ocaml
Linktim_ has quit [Read error: 110 (Connection timed out)]