nkoza has joined #ocaml
Kinners has joined #ocaml
mattam has quit [Remote closed the connection]
polin8 has joined #ocaml
docelic has joined #ocaml
lament has quit ["It's not like I'm using."]
remote has joined #ocaml
* remote
checkout some links ...
malc has joined #ocaml
foxen5 has joined #ocaml
malc has quit [Read error: 110 (Connection timed out)]
docelic has quit ["Client Exiting"]
remote has quit ["dljas"]
Kinners has quit ["leaving"]
mattam has joined #ocaml
lament has joined #ocaml
xtrm has quit [Remote closed the connection]
mattam has quit [Read error: 110 (Connection timed out)]
docelic has joined #ocaml
docelic has quit ["later all"]
taw has quit ["Client Exiting"]
mattam has joined #ocaml
systems has joined #ocaml
systems has quit ["Client Exiting"]
lament has quit ["It's not like I'm using."]
taw has joined #ocaml
systems has joined #ocaml
lament has joined #ocaml
systems has quit ["Client Exiting"]
gene9 has joined #ocaml
gene9 has left #ocaml []
lament has quit ["It's not like I'm using."]
graydon has joined #ocaml
stephane has joined #ocaml
async has joined #ocaml
olczyk has joined #ocaml
<
olczyk>
ANyone here?
<
taw>
happy new year
<
olczyk>
Not yet for me. About 11hrs from now.
<
olczyk>
I've got a function I'm trying to write.
<
olczyk>
List.iter print_int l;
<
olczyk>
OOps Sorry.
<
olczyk>
List.iter print_int l;
<
taw>
what about separators ?
<
olczyk>
Sorry. Keep messing up copy and paste.
<
olczyk>
One more time.
<
olczyk>
let rec gen_int i n l=
<
olczyk>
List.iter print_int l;
<
olczyk>
l::i::(gen_int ( i + 1 ) n l)
<
olczyk>
It "works" when I don't have the begin print and end statements.
<
olczyk>
But gives the wrong answer.
<
taw>
it compiles ?
<
olczyk>
top level accepts it.
<
olczyk>
I haven't tried compiling.
<
olczyk>
But that when I delete those three statements.
<
olczyk>
let rec gen_int i n l=
<
olczyk>
l::i::(gen_int ( i + 1 ) n l)
<
taw>
it doesn't accept it here
<
olczyk>
Since it gives the wrong answer I insert begin end and print.
<
olczyk>
Sorry it doesn't accept it when I put in begin end print.
<
taw>
you can't do 'a list :: int :: 'b
<
taw>
you should say:
<
taw>
l @ i::(gen_int ( i + 1 ) n l)
<
olczyk>
What does the @ mean?
<
taw>
concatenate 2 lists
<
olczyk>
Ok. It does work thought.
<
olczyk>
Take the second thing and: gen_int 0 5 0::[];; gives: [ 0; 0; 0; 1; 0; 2; 0; 3; 0; 4;]].
<
olczyk>
Is there a way to append directly to the end ( of course l@(i::[]) works ).
<
taw>
and that's what it's supposed to do, isn't it ?
<
taw>
x @ [i] would even look nicer ;)
<
taw>
but you should do it that way
<
taw>
append ad begining at then reverse list
<
taw>
this is performance-effective way
<
olczyk>
Hmmm. reverse is slow in lisp.
<
taw>
the same applies to Lisp
<
taw>
you have to do just 2 reverses
<
taw>
one for result
<
taw>
(and one for l)
<
olczyk>
Since l is supposed to be generated fromscratch.. No.
<
taw>
let gen_int i n l =
<
taw>
let rec gen_int_aux j =
<
olczyk>
what I wanted was let make_int n=let gen_int i n l ... in gen_int 1 n 0::[];;
<
taw>
i messed that ;)
<
olczyk>
Same thing.
<
taw>
[0] looks nicer than 0::[]
<
olczyk>
OK. But I do need the 0 no?
<
olczyk>
Otherwise how does ocaml know what type of list.
<
taw>
it will find that out ;)
<
taw>
you just want list like [0;1;2;3;4] ?
<
olczyk>
Yes. For testing things.
<
taw>
let rec gen i = if i = 0 then [] else i :: (gen (i-1));;
<
taw>
- : int list = [5; 4; 3; 2; 1]
<
taw>
i thought you are doing something more complex ;)
<
taw>
your code looked complex
<
olczyk>
let rec gen_int i n l=
<
olczyk>
List.iter print_int i;
<
olczyk>
l@i::(gen_int ( i + 1 ) n l)
<
olczyk>
Still produces an error.
<
olczyk>
Sorry change the i to an l.
<
olczyk>
It works but nothing is printed.
<
taw>
let gen_int n = let rec gen i = if i = 0 then [] else (i-1) :: (gen (i-1)) in List.rev gen n;;
<
taw>
why don't you do it that way ?
<
taw>
let gen_int n = let rec gen i = if i = 0 then [] else (i-1) :: (gen (i-1)) in List.rev (gen n);;
<
olczyk>
You know what there are about a million ways.
<
taw>
so you should choose the simplest one
<
taw>
i'm usually programming perl ;)
<
taw>
i know what i'm talking about hehe
<
olczyk>
I probably picked this way because because it was closest in style to something I was looking at.
<
olczyk>
But why doesn't the last thing function print the list?
<
taw>
(with List.iter print_int l;)
<
taw>
let rec gen_int i n l= begin List.iter print_int l ; if i < n then l@i::(gen_int ( i + 1 ) n l) else [] end;;
<
taw>
val gen_int : int -> int -> int list -> int list = <fun>
<
taw>
# gen_int 0 5 [0];;
<
taw>
000000- : int list = [0; 0; 0; 1; 0; 2; 0; 3; 0; 4]
lament has joined #ocaml
<
olczyk>
Weird. I get -: int list [0; 1; 2; 3;4]
<
olczyk>
Hi lament. Happy new year in a few hours.
<
olczyk>
taw: What version are you using?
<
taw>
but that shouldn't affect it
<
lament>
happy new year to you too.
<
taw>
maybe output get's buffered and isn't flrushed ?
<
olczyk>
Oh wait. I use gen_int 0 5 [];;
<
olczyk>
Yeah but why?
<
taw>
that explains
<
taw>
you are printing l
<
taw>
that won't help you much ;)
<
olczyk>
Ok. I see. I thought it was printing the running list. But it's printing the original list.
<
olczyk>
In my case null, so nothing.
<
olczyk>
In your case [0].
<
taw>
#trace gen_int
<
taw>
it will tell you about all calls to this function
<
olczyk>
Yeah but the point is that I should be able to print out these kinds of things.
<
olczyk>
I have the time now to learn so I should learn.
<
taw>
you should probably learn to do this the ocaml way
<
taw>
on ocaml course on my uni solution that is written in performance-inefficient way is not accepted
<
taw>
appending to end of list (which turns O(n) operation to O(n^2)) was one of most popular mistakes
<
olczyk>
For first few weeks kind of dumb. They should just be happy that it works.
<
olczyk>
Later yes.
<
taw>
first few weeks ?
<
taw>
people are excepted to be able to program already
<
taw>
they are just learning functional way on this course
<
olczyk>
No first few weeks of programming in any language.
<
olczyk>
Especially something like Lisp, OCaml or Haskell.
<
taw>
well, maybe i'm on a bit too good uni ;)
<
taw>
this is one of 2 most popular computer science unis in poland
<
taw>
so they expect a bit more from students
<
olczyk>
Maybe not. They're violateing Knuth's rule.
<
taw>
what is that rule ?
<
olczyk>
Knuth. Donald Knuth.
<
taw>
but what's the rule
<
olczyk>
A program spends 90% of it's time in 10% of the program.
<
olczyk>
So don't optimise prematurely.
<
olczyk>
Can you get at Programming Pearls by Peter Bently?
<
taw>
doesn't apply here
<
olczyk>
Also More Programming Pearls.
<
taw>
it's complexity problem
<
taw>
if you do it within the same compexity class
<
taw>
then it's acceptable
<
taw>
i think i could, are they any good ?
<
olczyk>
I like the part where they described how a group found a function in an OS that
<
olczyk>
was used 90% of the time, then optimised it.
<
taw>
with idle loop
<
olczyk>
Turned out they gained no effriciency.
<
olczyk>
Except Bently was there.
<
olczyk>
Very good read.
<
olczyk>
Anyway. My point wasn't that you shouldn't write this efficiently.
<
olczyk>
Good programmers right inefficient code in only one case.
<
olczyk>
When they don't understand what they are doing, and are stumbling in the dark to figure
<
olczyk>
out what to do to get a correct program.
<
taw>
good programmers never write inefficient code
<
olczyk>
Like when first using a language.
<
olczyk>
Just wait untill you get a job.
<
taw>
good programers use profilers
<
olczyk>
Just wait untill you get a job.
<
taw>
to find what is main bottleneck
<
taw>
rest of program must just work then
<
olczyk>
My first boss actually recommended I rip out an O(n) algorithm for an O(n^3) algorithm.
<
taw>
and it will be efficient
<
taw>
why did he do so ?
<
olczyk>
The first operated on a sorted list, the second on a random list.
<
olczyk>
He wanted to use random lists.
<
olczyk>
He was sorry later.
<
taw>
but then the algorithm is O(n^2 log n)
<
taw>
do_it_with_algorithm_for_sorted_list (sort_list (random_list))
<
olczyk>
BTW appending to the end of a list should be as efficient as appending to the begining.
<
olczyk>
All you do is replace the end of the cons cell with the new list.
<
taw>
you don't have pointer to final cell
<
taw>
so you can't do it
<
taw>
you have to go from first cell
<
olczyk>
That's right. Sigh.
<
taw>
and if you kept pointer to final cell
<
taw>
you'd have to upgrade it in all cels
<
olczyk>
You could.
<
taw>
but that's what Array is for
<
olczyk>
There is good reason not to use reverse. It generates a lot of garbage.
<
taw>
how does it so ?
<
olczyk>
That's why Lisp has nreverse ( in place reverse).
<
taw>
what does nreverse do ?
<
olczyk>
Let's see.
<
olczyk>
It reverses in place.
<
taw>
ocaml uses pointer iirc
<
taw>
so it doesn't really matter
<
taw>
objects don't get moved around
<
taw>
and cell is just pair of pointers
<
olczyk>
That would be a side effect.
<
taw>
since when values in ocaml are modificable ?
<
taw>
but making things less GC intense
<
taw>
may be quite good optimalization
<
olczyk>
Ok. I give up on getting reverse right.
<
olczyk>
Here's my try,
<
olczyk>
let my_rev l=
<
olczyk>
let rec my_rev_i list retval =function
<
olczyk>
| [] x ->[] x
<
olczyk>
| h::t x ->my_rev_i t h::x in my_rev_i l,[];;
<
olczyk>
But it show's the algorithm.
<
olczyk>
In lisp garbage is created via h ( car ) t (cdr) and in the return values.
<
olczyk>
Maybe not in caml.
<
olczyk>
I have to go. Thank you for your help.
notkoza_ has joined #ocaml
<
taw>
happy new year
<
notkoza_>
hi and the same for you :)
taw_ has joined #ocaml
lament has left #ocaml []
taw has quit [Read error: 110 (Connection timed out)]
taw_ is now known as taw
graydon has quit ["xchat exiting.."]
lament has joined #ocaml
Kinners has joined #ocaml