<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^^>
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.
<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
<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"?
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."]