Banana changed the topic of #ocaml to: OCaml 3.08 "Bastille Day" Release available ! -- Archive of Caml Weekly News: http://pauillac.inria.fr/~aschmitt/cwn , A tutorial: http://merjis.com/richj/computers/ocaml/tutorial/ , A free book: http://cristal.inria.fr/~remy/cours/appsem, Mailing List (best ml ever for any computer language): http://caml.inria.fr/bin/wilma/caml-list
zaza^^ has joined #ocaml
<zaza^^> hello
<zaza^^> :)
<zaza^^> anyone here ?
<Smerdyakov> Here and lovin' it, baby.
<zaza^^> :)
<zaza^^> I have an exercise to do in caml
<zaza^^> and I can't do it with my poor caml skills
<ronwalf> That's better than having to exorcise
<zaza^^> someone could maybe help me ?
<ronwalf> I dunno, is that allowed?
<zaza^^> of course
<zaza^^> it's not an exam, just an exercise
<zaza^^> and it's borring me, I can't get an idea on how to do it
<ronwalf> If we lift the bar for you, it's not much exercise, is it?
<ronwalf> maybe...
<zaza^^> I must do a function which replaces a string by another in a file
<zaza^^> like sed s/s1/s2/
<zaza^^> like=as
<zaza^^> but I don't know how to do..
<zaza^^> no one wanna help ? :/
gl has joined #ocaml
<ronwalf> z: Sure
<zaza^^> I have no idea about how to do it
<ronwalf> Neither do I (my ocaml skills are minimal)
<ronwalf> But I can look at docs!
<zaza^^> I do :(
<zaza^^> I did
<zaza^^> but ..
<ronwalf> of a big file
<ronwalf> ?
<zaza^^> no
<ronwalf> Do you have to do this streaming?
<zaza^^> an easy text file
<zaza^^> no
<Smerdyakov> zaza^^, can you give a description of the algorithm you would like to use?
<zaza^^> First I would do a function string -> string -> string
<zaza^^> hmm
<zaza^^> no
<zaza^^> string -> string -> int -> string
<zaza^^> somethink like that
<zaza^^> which takes a string
<zaza^^> an int
<zaza^^> and a string
<zaza^^> and.. (hard in english)
<zaza^^> exemple:
<zaza^^> myfunction "string1" "string2" 2
<zaza^^> it gives ststring2ring1
<zaza^^> I need a function which says if a string is in another string to
<zaza^^> to=too
<zaza^^> ..
<zaza^^> I dunno
<zaza^^> In C it's so easy to do it :(
<Smerdyakov> That doesn't seem to be a description of an algorithm for solving the problem.
<zaza^^> an algo
<zaza^^> err
<Smerdyakov> Rest assured, almost everything is much easier to do in ML than C.
<zaza^^> "for each line of the file, check if string2 is in the line, and if yes replace string2 by string1"
<zaza^^> here is my algo :)
<ronwalf> I wouldn't do it by line
<ronwalf> What if the replacement is a newline?
<ronwalf> or you are replacing newlines
<zaza^^> hm
<zaza^^> I could do by caracter then
<ronwalf> hum, String contains a lack-luster set of string manipulatinos...
<zaza^^> I know all the string manipulation functions
<zaza^^> I read docs
<ronwalf> What about the Buffer module?
<ronwalf> add_substitute looks promising
<zaza^^> I don't know it
<ronwalf> myabe not
<ronwalf> oh, it's not
<ronwalf> It for shell like variable replacement
<ronwalf> it's for
<Smerdyakov> zaza^^, any string manipulation function that the standard library lacks should be trivial for you to write.
* ronwalf ponders p'ocaml again
<Smerdyakov> zaza^^, you have functions to get and set characters of strings, so what more would you need?
<Riastradh> 'p'ocaml?'
<ronwalf> Ocaml for the perl/python hacker :)
<zaza^^> Smerdyakov: what more I need ?
<zaza^^> I need more caml skills
<zaza^^> ^^
<ronwalf> No, the index and index_from, combined with sub should do the trick
<Smerdyakov> zaza^^, this is trivial. If you can write a C program to do this, you are not lacking the skills to do it in OCaml, OR, you should be able to ask very specific questions about how to do very specific things in OCaml.
<Smerdyakov> ronwalf, I really doubt he is meant to use any standard library functions for this.
<zaza^^> iterative and functionnal are so different
<zaza^^> in C it's natural to do it
<Smerdyakov> zaza^^, OCaml is both imperative and functional.
<Smerdyakov> zaza^^, anyway, you can expect no help unless you ask _very_specific_ questions on how to do things in OCaml.
<zaza^^> k
<zaza^^> so I guess I can except no help :)
<ronwalf> Smerdyakov: How can you access strings other than through the string library?
<ronwalf> Can you iterate over them?
<zaza^^> s.[1], etc.
<Smerdyakov> What he said. :)
<Riastradh> zaza^^, you know, it might help if _you_ tried applying thought to the problem, rather than deferring that to us.
<Smerdyakov> zaza^^, it is ridiculous for you to say that you don't know enough to ask very specific questions.
<Smerdyakov> zaza^^, if you know how to do this in C, you know almost everything about how to do it in OCaml.
<ronwalf> I think the String library is fairly comparable to the string utils in C
* ronwalf notices the str lib
<zaza^^> anyway, I have not to do it by strings
<zaza^^> [02:27] <ronwalf> I wouldn't do it by line
<zaza^^> [02:27] <ronwalf> What if the replacement is a newline?
<zaza^^> you were true
carlossch has joined #ocaml
carlossch has left #ocaml []
bk_ has quit ["Leaving IRC - dircproxy 1.1.0"]
<j_n> are there any libraries that demonstrate tcp/ip pakcet manipulation in ocaml
<zaza^^> :(
<sic-> yea, there's a few
<sic-> Iox, mail-smtp, netstring etc
vezenchio has quit ["I thought what I'd do was, I'd pretend I was one of those deaf-mutes ^_^"]
GreyLensman has joined #ocaml
<zaza^^> how is it possible de read one caracter of a file ?
<Smerdyakov> I can tell you haven't read the manual page for Pervasives.
<zaza^^> I don't know what the word "pervasive" mean
<Smerdyakov> Pervasives is the module that is opened by default. It contains all the default definitions of things.
<zaza^^> cool
<zaza^^> input_char ?
<Smerdyakov> Need you ask when you can try it and see?
<zaza^^> don't need no
<zaza^^> but is it a problem ?
<Smerdyakov> What?
<zaza^^> If you don't want to answer, then don't do ?
<zaza^^> no ?
<Smerdyakov> Then you might be sitting around waiting for an answer.
<Smerdyakov> I'd rather let you know that I'm not planning to answer.
<zaza^^> I prefer no answer than "don't ask" :)
<Smerdyakov> That's your fault. :P
<zaza^^> I'm not able to do it, I go to sleep
<zaza^^> bye
<zaza^^> thank's
<Smerdyakov> Tata
<zaza^^> tata ?
<zaza^^> what's tata ?
<Smerdyakov> "Bye"
<zaza^^> I'm zaza
<zaza^^> is it possible to take the whole file in a string ?
<zaza^^> I read each line and concat it ?
<Smerdyakov> Clearly you can do it yourself, even if there is no standard function for it.
<zaza^^> hmm
<zaza^^> maybe I'll try to do it that way
<zaza^^> I put the whole file in a string, then I do the work on the string
<zaza^^> you would say me the name of the function used to concat 2 strings ?
<Smerdyakov> It's in Pervasives.
<zaza^^> thank's
<zaza^^> you could see a litle code and say me where's the problem ?
<Smerdyakov> How do you know there's a problem?
<zaza^^> This expression has type 'a ref but is here used with type string
<zaza^^> ^^
<Smerdyakov> That means you are using a ref where a string is expected. :)
<zaza^^> thank's for the translation :)
<Smerdyakov> If you put a small amount of this code on the web and provide a URL, I will look.
<zaza^^> the error is on the second "chaine" of chaine := chaine ^ (input_line fichier);
<Smerdyakov> The := operator is only usable with refs.
<Riastradh> You can't assign variables; variables aren't mutable.
<Riastradh> This isn't Pascal. You don't assign an output variable to 'return' something.
<zaza^^> I was about to ask how to return "chaine" ^^
<Riastradh> Yes, but it seemed apparent that that was what you were trying to do.
<Riastradh> Otherwise, why add the intermediate indirection of assigning chaine at all? Why don't you pass (chaine ^ (input_line fichier)) to fichier_chaine?
<zaza^^> true
<zaza^^> but you understand, I need to use the string "chaine" in another function, how the hell I can use it after ?
<zaza^^> without returning it
<Riastradh> Might in inquire as to what fichier_chaine is supposed to do?
<zaza^^> it takes a file
<Smerdyakov> "Might in inquire"!!! HAHA!! Riastradh is a mental cripple!
<zaza^^> and put it in a string
<zaza^^> it concates all the lines of the file
<Riastradh> Smerdyakov, oooh, go insult ribbon in that same way in #haskell, quick!
<zaza^^> isn't it what this function does ?
<zaza^^> it puts all fichier in chaine, true ?
<zaza^^> fichier = file
<zaza^^> chaine = string
<Riastradh> I don't know what 'fichier' and 'chaine' are.
<zaza^^> in my strange language
<Smerdyakov> Wow! Double whammy! Now it's revealed that Riastradh doesn't speak French after all!
<Riastradh> Je ne parle pas francais tres bien.
<zaza^^> I don't speak english very well either :)
<zaza^^> Fortunately I can speak english better than I code caml
<zaza^^> if I do:
<zaza^^> try
<zaza^^> blabla
<Riastradh> Ou plutot, je ne sais pas beaucoup terminologie d'ordinateur en francais.
<zaza^^> with
<zaza^^> End_of_file -> chaine;;
<zaza^^> it will return chaine, no ?
<Riastradh> If blabla raises End_of_file, yes.
<zaza^^> blabla = fichier_chaine fichier (chaine ^ (input_line fichier));
<Riastradh> Correct. Note, however, that your code is dangerous if the file has many lines, because it may blow the stack.
<zaza^^> I know it
<zaza^^> but.. I don't realy care
<zaza^^> I guess the teacher won't test it on big files
<zaza^^> I hope so
<zaza^^> I have to do my sed s/foo/bar/
<zaza^^> I have to concat "\n" too no ?
<zaza^^> (chaine ^ "\n" ^ (input_line fichier))
<Riastradh> It is up to you to determine that. This is your program.
<zaza^^> gna gna gn
<zaza^^> how can I concat a EOL ?
<zaza^^> - : string = "\nlel\nlol\nlul"
<zaza^^> :(
<Smerdyakov> That's what you want.
<Smerdyakov> The toplevel displays strings in results just like you would enter them in OCaml code.
<zaza^^> it looks like it concated '\'
<zaza^^> then 'n'
<Smerdyakov> Nope.
<Smerdyakov> See above.
<zaza^^> (your sentence is too hard to be understood :p)
<Smerdyakov> When you type "\n", you know what it means, right?
<zaza^^> in C, yes, I think it's the same thing in caml
<Smerdyakov> OK. When the toplevel prints a string, it prins it just like you would type it in.
<Smerdyakov> Now how simple is that? :)
<zaza^^> what is "toplevel" ?
<Smerdyakov> The interactive prompt-and-respond thing.
<Riastradh> The REPL. The interactive interpreter.
<Smerdyakov> What you get when you run 'ocaml'.
<zaza^^> k
<zaza^^> so my string is what I want, "\n" is for an EOL, not '\'^'n' ?
<Riastradh> Yes. You were right the first time to write "\n".
GreyLensman has quit ["Leaving"]
<zaza^^> I don't know how to solve this ***** problem
<zaza^^> so borring
<debona|r> zaza^^: dude, what are you studying?
<zaza^^> err.. how to say in english :)
<debona|r> anyway, going to bed
<debona|r> computer science?
<zaza^^> ok :) good night :)
<zaza^^> yes
<debona|r> why are you studing it if you think this is boring?
<debona|r> I'm doing it for fun
<zaza^^> I don't like caml, but I have to do those exercices if I want to have my exam
<debona|r> zaza^^: you prefer imperative programming?
<Riastradh> zaza^^, what would you prefer?
<zaza^^> I like C, C++, yes
<zaza^^> I'm a noob you know
<zaza^^> I've been studying comp sciences for 2 years only
<debona|r> zaza^^: there's a famous computer scientist (I don't know who) who said that unless a language changes the way you think about programming, it's not worth learning
<debona|r> zaza^^: that's why I'm doing ocaml, coz I don't really know any functional languages
<Riastradh> You're aware that those are _terrible_ for modelling any abstraction of computer science, aren't you, zaza^^?
<zaza^^> It's pretty funny, but if I wouldn't have, I wouldn't study it
<zaza^^> have = have to
<zaza^^> Riastradh: yeah
<Riastradh> Alan Perlis said that, debona|r.
<debona|r> Riastradh: thanks, I was going to ask if someone knew
<zaza^^> in fact, I had a lot of classes with caml
<zaza^^> this year
<zaza^^> But I couldn't go to them
<zaza^^> So I'm really bad
<debona|r> couldn't or wouldn't?
cjohnson has quit [Read error: 54 (Connection reset by peer)]
<zaza^^> couldn't
<debona|r> zaza^^: I think ocaml is fun to learn
<zaza^^> where r u from ?
<debona|r> and I don't have a CS background, so all this 'modelling abstraction' talk is hard for me to get, but .. it's fun
<debona|r> i'm in california
<zaza^^> cool :)
<debona|r> from Sweden
<zaza^^> I know none of them ^^
<zaza^^> I've been one time to usa, when I was young.. but that's all
* zaza^^ is in Paris
<zaza^^> and nobody cares :)
<zaza^^> I have a function which put my file in a string, and a function which says if a substring is in a string (and gives the index)
<zaza^^> better than nothing ^^
<zaza^^> now I have to do a function which replaces a substring by another substring, in the string
<zaza^^> not easy
<debona|r> why?
<zaza^^> I just don't know how I can do it :)
<debona|r> I suppose you can't use regex? :)
<zaza^^> I tryed, but it seems I can't
<debona|r> zaza^^: you might find this useful: http://caml.inria.fr/ocaml/htmlman/libref/
<zaza^^> or maybe I dont understand how the Str module works
<zaza^^> # open Str;;
<zaza^^> # regexp "foo";;
<zaza^^> Reference to undefined global `Str'
<Riastradh> You shouldn't use Str. Writing a function to substitute substrings shouldn't be so difficult.
<debona|r> zaza^^: think about how you'd do it in C first, if that helps
<zaza^^> in fact it's easy to write the substring in the string, but I don't know how to delete the former one
Nutssh has joined #ocaml
<Riastradh> How do you 'write' the substring in the string?'
<Riastradh> ...with balanced quote marks, preferrably!
<debona|r> zaza^^: String.sub or something?
<zaza^^> "foobar" -> I want to replace foo by faa, I'll get "foofaabar"
* Riastradh curses at the location of the quote key and the return key.
<Riastradh> zaza^^, huh? Shouldn't you get "faabar"?
* debona|r snickers at Riastradh
<mflux> zaza^^, in any case, #load "str.cma";; will load the Str module
<mflux> but I do agree that the regexp interface Str provides isn't very nice, although it might be convenient for 'script-like' usage
<debona|r> ok, off to read about how corrupt the world banking system is. good nite all
<zaza^^> Cannot load required shared library: dynamic loading not supported on this platform.
<zaza^^> =|
<mflux> I suppose in that case you need to build your own toplevel
<debona|r> you can write the whole thing just using String, no? no need for regex
<mflux> ocamlmktop -o toplevel str.cma
<debona|r> just some string.index, and some other things
<zaza^^> debona|r: I can, yes.. even if I don't know how to :)
<Riastradh> zaza^^, don't use Str.
<zaza^^> Riastradh: sir yes sir
<Riastradh> Just answer my question: how do you 'write' the substring into the string?
<debona|r> zaza^^: I can't see how it would be so dramatically different than your logic would be in doing it in C
<zaza^^> Riastradh: I think I'll use concat and sub
<zaza^^> I don't know in fact :)
<Riastradh> You said it's quite easy to do it, though.
<Riastradh> I'm not asking you how to delete the original substring just yet.
<zaza^^> s.[1] <= sub.[1]
<zaza^^> err..
<zaza^^> s.[index_where_the_sub_ends] <= sub.[1]
<zaza^^> s.[index_where_the_sub_ends+1] <= sub.[1+1]
<zaza^^> etc.
<Riastradh> That will overwrite whatever was previously in there, however, won't it?
<zaza^^> +2 +3 ... +length_of_the_sub
<zaza^^> yeah, true :(
<Riastradh> OK, just consider: what three strings are involved if you want to insert "quux" in the middle of "foobar"?
<zaza^^> I can't understand the sentence
<Riastradh> Just thinking in general, what do you do to insert "quux" into the centre of "foobar"?
<Riastradh> Abstractly, that is.
<zaza^^> I sub "foobar" -> I get "foo"
<zaza^^> I concat "quux"
<zaza^^> then concat "bar"
<Riastradh> OK, good.
<Riastradh> Now consider the idea that what you're doing here is not just inserting, but substituting "quux" the empty string at that location.
<Riastradh> Now consider if there were a z in the place of epsilon.
<Riastradh> That is, you want to substitute "quux" for the "z" in "foozbar".
<zaza^^> yes
<Riastradh> How would you perform this substitution yourself? -- that is, you yourself; I'm not asking for an algorithm just yet.
debona|r has quit [Read error: 60 (Operation timed out)]
debona|r has joined #ocaml
<zaza^^> it's difficult for me to understand what you're asking :) I take "foo", I concat "quux" then "bar"
<Riastradh> Right.
<zaza^^> I see what you want me to see
<zaza^^> my only problem is:
<Riastradh> Now, how do you know how to get "foo"? -- what range in "foozbar" gives the substring "foo"?
<zaza^^> with the position of z
<zaza^^> the index
<Riastradh> What about the substring "bar"?
<zaza^^> the index of z in foozbar and the length of foozbar
<Riastradh> What's the index of z in foozbar?
<zaza^^> 3
<zaza^^> my only problem is that I don't know how to write it in caml
<zaza^^> [07:25] <debona|r> zaza^^: I can't see how it would be so dramatically different than your logic would be in doing it in C
<zaza^^> the logic is the same
<Riastradh> Is that really what you want, then? What happens if you get the substring starting at 3?
<zaza^^> didn't understand
<Riastradh> Try getting a substring of "foozbar" that starts at index 3.
<zaza^^> zbar
<zaza^^> ?
<Riastradh> So that's not quite right. You want to get "bar".
<zaza^^> I have the length of "z"
<Riastradh> And what do you do with it?
<zaza^^> I get "bar" from "zbar"
<Riastradh> Why don't you combine those two steps, to make it simpler?
<zaza^^> err
<zaza^^> String.sub "foozbar" 0 (contain "foozbar" "z" - String.length "z");;
<zaza^^> string = "foo"
<Riastradh> OK, now how about getting "bar"?
<zaza^^> String.sub "foozbar" (contain "foozbar" "z") (String.length "foozbar");;
<zaza^^> something like that
<zaza^^> Exception: Invalid_argument "String.sub".
<Riastradh> Not quite. String.length "foozbar" will return something too big; you need to get the length of "bar".
<zaza^^> ho it's lenght of the cut ? not the end in the index ?
<zaza^^> true
<zaza^^> # String.sub "foozbar" (contain "foozbar" "z") (String.length "foozbar" - contain "foozbar" "z");;
<zaza^^> - : string = "bar"
<Riastradh> OK. So now that you have the three relevant strings, what do you do?
<zaza^^> I concat them
<Riastradh> Right.
<Riastradh> So the whole substitution function is now...?
<zaza^^> in that exemple ?
<Riastradh> No, in general.
<zaza^^> I have to handle this case:
<zaza^^> foozbazr
<Riastradh> "foozbar" is just the input string to substitute in. "z" is the original substring; "quux" is the substring to substitute for "z".
<Riastradh> This need only do one substitution at a time.
<Riastradh> So it's OK if this function we're talking about right now just produces "fooquuxbazr".
<zaza^^> let substitute s sub_bad sub_good =
<zaza^^> String.sub s (contain s sub_bad) (String.length s - contain s sub_bad) ^
<zaza^^> sub_good ^
<zaza^^> String.sub s (contain s sub_bad) (String.length s - contain s sub_bad);;
<Riastradh> Does this work to substitute a single instance?
<zaza^^> I didn't try it yet ^^
<Riastradh> Well, do so.
<zaza^^> # substitute "foozbazr" "z" "quux";;
<zaza^^> - : string = "bazrquuxbazr"
<zaza^^> o_O
<zaza^^> It seems I forgot something :)
<zaza^^> wait
<Riastradh> How did you get "foo", zaza^^?
<zaza^^> errr yes :) I made a mistake sorry :)
<zaza^^> # substitute "foozbazr" "z" "quux";;
<zaza^^> - : string = "fooquuxbazr"
<zaza^^> Here is better :)
<zaza^^> Then I have to do substitute "fooquuxbazr" "z" "quux";;
<zaza^^> etc..
<zaza^^> until there's no 'z'
<zaza^^> until contain s sub_bad says "not found"
<Riastradh> Not quite.
<Riastradh> What if "quux" were actually "quuz"?
<zaza^^> ouch :(
<zaza^^> good question
<Riastradh> And what if the input string were "foobar"?
<zaza^^> instead of foozbar ?
<Riastradh> Yes.
<zaza^^> the function does nothing
<zaza^^> (in theory)
<Riastradh> Right. But you need to ensure that.
<Riastradh> What will 'contain' return for an input string "foobar"?
<zaza^^> raise Not_found;
<Riastradh> It might be done a bit simpler if you used the 'option' data type.
<zaza^^> ?
<Riastradh> It allows you to specify either the absence of any value or the presence of a single value.
<Riastradh> In this case, the relevant value is the index into the input string of the substring.
maihem has quit ["Read error: 54 (Connection reset by chocolate)"]
<zaza^^> I can't do a try ... with Not_found -> ... ?
<Riastradh> contain "foozbar" "z" (* That would return 'Some 3' *).
<Riastradh> contain "foobar" "z" (* That would return 'None' *).
<Riastradh> Er, with periods in the right location, of course!
<zaza^^> period ?
<Riastradh> zaza^^, well, OK, you can go ahead and continue doing it that way.
<Riastradh> Never mind about the option stuff.
<zaza^^> ok
<Riastradh> So substitute will need to catch the Not_found exception.
<zaza^^> there's a problem
<zaza^^> [08:08] <Riastradh> What if "quux" were actually "quuz"?
<Riastradh> I'll get to that.
<Riastradh> If sub_bad is not found in s, what should substitute do?
<zaza^^> return s ?
<Riastradh> Right.
<zaza^^> try ( blabla ) with Not_found -> s;;
<Riastradh> So what does substitute look like now, if you make it catch Not_found?
<zaza^^> something like that ?
<Riastradh> Right; now write the actual code.
<zaza^^> done
<zaza^^> # substitute "foobar" "z" "quux";;
<zaza^^> - : string = "foobar"
<Riastradh> Good.
<Riastradh> Now, since contain found the _first_ occurrence of sub_bad in the input string, do we need to bother with substituting in the the "fooquux" part at all?
<zaza^^> no :)
<zaza^^> I give "bazr" to substitute
<zaza^^> true ?
<zaza^^> I concat "fooquux" with substitute "bazr" "z" "quux"
<Riastradh> Why don't you try that and find out?
<zaza^^> I'll try
<zaza^^> thank's :)
<Riastradh> Does it work?
<zaza^^> not yet
<Riastradh> What does the code look like now?
<zaza^^> try
<zaza^^> (
<zaza^^> String.sub s 0 (contain s sub_bad - String.length sub_bad) ^
<zaza^^> sub_good ^
<zaza^^> substitute (String.sub s (contain s sub_bad) (String.length s - contain s sub_bad)) sub_bad sub_good
<zaza^^> etc.
<zaza^^> and substitute is a recursive function
<zaza^^> but the with is now -> ""
<zaza^^> I'll test somewhere else if foobar contains z
<zaza^^> before running substitute
<zaza^^> hmm
<Riastradh> Suppose you find the first "z" in "foozbazr".
<zaza^^> it must be wrong
<Riastradh> Does it matter to the substitution of "quux" for "z" in "bazr" what the result of substituting "quux" for "z" initially is?
<Riastradh> That is, does "baquuxr" need to care about "fooquux"?
<zaza^^> my leet english skills are suffering :)
<zaza^^> anyway it seems it's wrong
<zaza^^> # substitute "foozbazaaaazrrrr" "z" "_";;
<zaza^^> - : string = "foo_ba_aaaa_"
<zaza^^> because there is no "z" in "rrrr"
<zaza^^> in fact with Not_found -> s was cool
<Riastradh> Do you get the same result for "bazr" regardless of what you got for "fooz"?
<Riastradh> Oh, never mind.
<Riastradh> Ignore that.
<Riastradh> Does it work now?
<zaza^^> nope
<zaza^^> substitute "foozbarzrrrrzrrz" "z" "_";;
<zaza^^> Exception: Invalid_argument "String.sub".
<zaza^^> # substitute "foozbar" "z" "_";;
<zaza^^> - : string = "foo_bar"
<Riastradh> Why don't you try doing the "foozbarzrrrrzrrz" substitution out by hand?
<zaza^^> it's because z is in the end of the string
<zaza^^> # substitute "z" "z" "_";;
<zaza^^> Exception: Invalid_argument "String.sub".
<Riastradh> Yes, but you need to figure out why this happens.
<zaza^^> String.sub s (contain s sub_bad) (String.length s - contain s sub_bad))
<zaza^^> String.length s - contain s sub_bad = 0
<Riastradh> Write out the whole body of substitute, but with "z" instead of s, "z" instead of sub_bad, and "_" instead of sub_good.
<zaza^^> substitute (String.sub s (contain s sub_bad) (String.length s - contain s sub_bad)) sub_bad sub_good
<zaza^^> it gives: substitute (string.sub "z" (contain "z" "z") 0 "z" "_"
<zaza^^> substitute (string.sub "z" 1 0 ) "z" "_"
<zaza^^> errr..
<zaza^^> no the error doesn't come from this
<zaza^^> err
<zaza^^> I don't know
<Riastradh> Try writing it out a _bit_ more coherently.
<Riastradh> First, write out the whole definition of substitute.
<Riastradh> Then replace every occurrence in that definition of s with "z", sub_bad with "z", and sub_good with "_".
<zaza^^> it gives:
<zaza^^> String.sub "z" 0 (contain "z" "z" - String.length "z") ^
<zaza^^> "_" ^
<zaza^^> substitute (String.sub "z" (contain "z" "z") (String.length "z" - contain "z" "z")) "z" "_"
<zaza^^> I replace contain "z" "z" by 1
<Riastradh> So what's the body now?
<zaza^^> String.sub "z" 0 (1 - String.length "z") ^
<zaza^^> "_" ^
<zaza^^> substitute (String.sub "z" 1 (String.length "z" - 1)) "z" "_"
<zaza^^> I replace S.length "z" by 1
<Riastradh> And it becomes...?
<zaza^^> String.sub "z" 0 0 ^
<zaza^^> "_" ^
<zaza^^> substitute (String.sub "z" 1 0) "z" "_"
<Riastradh> Now reduce the two String.sub calls.
<zaza^^> "" ^
<zaza^^> "_" ^
<zaza^^> substitute ("") "z" "_"
<Riastradh> Now reduce the two ^'s.
<zaza^^> hm ?
<zaza^^> "_" ^ substitute "" "z" "_"
<zaza^^> I have to filter "" in the bzeginning of substitute
<zaza^^> no
<Riastradh> Reduce 'substitute "" "z" "_"' now.
<zaza^^> if s = "" then "" else etc..
<zaza^^> k
<zaza^^> String.sub "" 0 (0 - 1)
<zaza^^> the first line gives this
<zaza^^> String.sub "" 0 (0 - 1) ;;
<zaza^^> Exception: Invalid_argument "String.sub".
<zaza^^> # substitute "z" "z" "_";;
<zaza^^> - : string = "_"
<zaza^^> yeah \o/
<zaza^^> do you see a not handled case now ?
<Riastradh> Why don't you look for one?
<zaza^^> maybe because it's 8:55 am
<zaza^^> I've not slept yet
<zaza^^> hm
<zaza^^> I'll flood.. close your eyes
<zaza^^> substitute "foobar" "z" "_";;
<zaza^^> substitute "foozbar" "z" "_";;
<zaza^^> substitute "foozbarzbar" "z" "_";;
<zaza^^> substitute "zz" "z" "_";;
<zaza^^> substitute "" "z" "_";;
<zaza^^> see another one ?
<Riastradh> That, I'm afraid, is for you to determine.
* zaza^^ nods
<zaza^^> these ones are ok
<zaza^^> I found one
<zaza^^> substitute "foobar" "" "bar";;
<zaza^^> Exception: Invalid_argument "String.sub".
<zaza^^> :p
<zaza^^> It's handled now..
<zaza^^> I've to thank you :)
<zaza^^> so, thank you
<zaza^^> and good night dude
<zaza^^> and c ya tomorrow, I may have other questions :)
* zaza^^ kisses Riastradh, thank's for the precious help
mattam_ has joined #ocaml
Nutssh has quit ["Client exiting"]
mattam has quit [Read error: 110 (Connection timed out)]
smimou has joined #ocaml
kinners has joined #ocaml
mattam_ is now known as mattam
bk_ has joined #ocaml
<zaza^^> hello
<zaza^^> I've got a question :)
<kinners> salut zaza^^
<zaza^^> salut :)
<zaza^^> t'es francophone ?
<kinners> new zealander
<zaza^^> is it possible to do "cat fichier | sed s/foo/bar/ > temp; mv temp fichier && rm temp " without temp ?
<Hipo> sed -i
<zaza^^> hm..
<zaza^^> In fact I had to do a sed s/foo/bar/ in caml
<zaza^^> and I meet this temporary file problem
<zaza^^> I would like not to use one
<zaza^^> is it possible ?
<zaza^^> in fact I open_in and open_out the file
<zaza^^> and it's deleted
<zaza^^> so I MUST use a temp file ?
<kinners> you could read the whole input file before doing any writing
<pnou> exec 3<fichier; rm -f fichier; sed s/foo/bar/ <&3 > fichier; exec 3<&-
TheDracle has quit [niven.freenode.net irc.freenode.net]
gim has quit [niven.freenode.net irc.freenode.net]
mflux has quit [niven.freenode.net irc.freenode.net]
j_n has quit [niven.freenode.net irc.freenode.net]
TheDracle has joined #ocaml
gim has joined #ocaml
mflux has joined #ocaml
j_n has joined #ocaml
bk_ has quit ["Leaving IRC - dircproxy 1.1.0"]
zaza^^ has quit [Read error: 104 (Connection reset by peer)]
<mflux> you could: 1) open source file 2) remove source file 3) open source file in write mode 4) operate
<mflux> works in unix
<mflux> oh, he left already
<mflux> /ignore is a bless, except when it isn't.
<mflux> (joins, parts, etc crap that is ;))
kinners has quit [Read error: 110 (Connection timed out)]
leolio has joined #ocaml
gim has quit ["bbw"]
debona|r has left #ocaml []
gim has joined #ocaml
gl has quit [niven.freenode.net irc.freenode.net]
jrosdahl has quit [niven.freenode.net irc.freenode.net]
ionOSu has quit [niven.freenode.net irc.freenode.net]
awwaiid has joined #ocaml
gl has joined #ocaml
jrosdahl has joined #ocaml
ionOSu has joined #ocaml
awwaiid has quit [Read error: 104 (Connection reset by peer)]
awwaiid has joined #ocaml
awwaiid has quit [Read error: 104 (Connection reset by peer)]
awwaiid has joined #ocaml
<mflux> hmh.. I suppose ocaml doesn't have a module that would provide mapping objects to objects, but so that one could do searches such as 'atleast x' or 'at most x'?
<mflux> ..I suppose Map would have the data structures to do that, but it doesn't provide that capability
<mflux> well, they say ml is excellent for writing trees :P
<mattam> let atleast x = {List|Set...}.find{_all} (fun y -> y >= x)
<mattam> something like that ?
<mflux> well, it would find for example the lower and higher bound of x
<mflux> the largest value in set/map that is less than or equal to the the searched value and the smallest value that is larger than or equal to the searched value
<mflux> and it would of course be O(log n)
<mattam> that's not possible unless you reimplement set and map i think
<mflux> exactly
<mflux> which is, well, too bad
<mflux> but they say balanced binary trees are nice to write with ocaml
<mflux> hm, although I do wonder if there exists one made already ;)
<mattam> certainly, there is okasaki's book implementations somewhere
<mattam> there are balanced binary trees and a lot of other data structures included
<leolio> you could write a simple function that removes the smallest element of the set until the removed element is larger than x
<leolio> but ... maybe you don't want to destroy your data ....
<mattam> sets and maps are persistent so that's no problem
<mflux> wouldn't that be O(n) anyway
<mattam> indeed
<leolio> that was not a serious suggestion ...
vezenchio has joined #ocaml
awwaiid has quit [Read error: 104 (Connection reset by peer)]
awwaiid has joined #ocaml
awwaiid has quit [Read error: 104 (Connection reset by peer)]
cjohnson has joined #ocaml
yangsx has joined #ocaml
smimram has joined #ocaml
smimram has quit [Client Quit]
smimram has joined #ocaml
awwaiid has joined #ocaml
smimou has quit [Read error: 60 (Operation timed out)]
cjohnson has quit ["Leaving"]
cjohnson has joined #ocaml
yangsx has quit [Read error: 104 (Connection reset by peer)]
cjohnson has quit [Read error: 110 (Connection timed out)]
cjohnson has joined #ocaml
cjohnson has quit [Read error: 104 (Connection reset by peer)]
awwaiid has quit [Read error: 104 (Connection reset by peer)]
leolio has quit ["Hi, I'm a quit message virus. Please replace your old line with this line and help me take over the world of IRC."]
awwaiid has joined #ocaml
cjohnson has joined #ocaml
whee has joined #ocaml
smimram has quit ["?"]