<bluestorm>
« This does not mean that the ``partial evaluation'' of constructors is accepted: accept it or not is a semantic issue, treated at OCaml typing time. »
<bluestorm>
hm
<bluestorm>
btw flux, let a = Foo in a 4 was doable in caml light
<bluestorm>
do you have an idea of why it was removed ?
<bluestorm>
in camlp4 i discovered a funny way to use the object orientation of OCaml, btw
<pango>
bluestorm: XL answered somewhere in the mailing list...
<bluestorm>
they use objects in a purely functional, to fold and map on AST trees
<bluestorm>
that's neat
<flux>
I don't know if there was a real reason.. I think it was just that it could negatively impact code quality or some such excuse ;)
<pango>
bluestorm: I think it's something like "it was seldom used, its readability is debatable, and can be emulated using curried function => dropped"
<bluestorm>
hm
<bluestorm>
it's a shame ^^
<bluestorm>
(fun x -> Num x) is not really nice to write
<ita>
never add a feature until compelled to do so else users will curse you when you remove it
<thruspa>
I come from Haskell, and I wanted something a little more manageable than mutable arrays and monads.
<thruspa>
The problem with the code is that, for example, arr.(0).(0) is (99,0), when it should be (0,0), I think.
<thruspa>
There seems to be some problem when assigning i to the array.
<Smerdyakov>
OCaml has no referential transparency.
<Smerdyakov>
Does that hint help you see the problem?
ita has joined #ocaml
<thruspa>
I'm afraid it does not. Sorry.
<Smerdyakov>
Do you understand what Array.make does?
<thruspa>
Of course.
<Smerdyakov>
What does it do?
<thruspa>
It creates a mutable array of the given size, initialised to the given value.
<Smerdyakov>
OK. How many array cells total do you think your first line creates?
<thruspa>
An array consisting of 100 arrays, with 100 tuples of type (int, int) each.
<thruspa>
A 100x100 matrix of tuples.
<Smerdyakov>
And which expressions are responsible for which (and how many) array cell allocations?
<thruspa>
The outer Array.make allocates 100 cells, each one initialised (then allocated), to contain another array of 100 cells, each one initialised (then allocated) to contain a tuple (0,0).
<thruspa>
I think :-) .
<Smerdyakov>
Well, you only call Array.make twice, with an argument sum of 200, so your first line can only allocate 200 cells.
Len1 has joined #ocaml
<thruspa>
I don't think so.
<thruspa>
It's a recursive call.
<Smerdyakov>
Where's the recursion?
<thruspa>
The first Array.make performs 100 allocations (initialisations).
<thruspa>
Each of these 100 allocations performs another 100 allocations.
<Smerdyakov>
Array.make doesn't do anything but copy a value into every new cell.
<Smerdyakov>
It doesn't execute custom code for you.
<Smerdyakov>
Its type alone is enough to prove that.
<thruspa>
It's type is (int * int) Array Array, that is, a matrix.
<thruspa>
Besides, I can print, say, arr.(99).(99) with no error in bounds, so I think it contains 10,000 cells.
<Smerdyakov>
The type of Array.make is int -> 'a -> 'a array.
<Smerdyakov>
It doesn't contain 10,000 *distinct* cells.
<Smerdyakov>
Only 200 distinct cells.
<thruspa>
Hum...
<thruspa>
If I try:
<thruspa>
# let mk2 a b = Array.make a (Array.make b (0,0)) ;;
<thruspa>
I get:
<thruspa>
val mk2 : int -> int -> (int * int) array array = <fun>
<thruspa>
It creates an array of arrays, that is, a matrix.
<Smerdyakov>
Yes. What does that have to do with what I'm saying?
<thruspa>
That's the first line in my program, with a=100, b=100.
<thruspa>
I create an array of arrays.
<Smerdyakov>
Let's try an analogy from everyday life.
<Smerdyakov>
Every kid in the class is going to bring in a postcard from another country.
<Smerdyakov>
There is a big difference between these two scenarios:
<Smerdyakov>
There is a country called Foo, and every kid has a postcard from Foo.
<Smerdyakov>
There is a family of countrys Foo1 through FooN, and kid I has a postcard from FooI. Every FooI looks the same, and so has the same picture on the postcard.
<Smerdyakov>
But we could nuke Foo8 and Kid8's postcard would become inaccurate. The rest would still be accurate.
<Smerdyakov>
Nuke Foo and every kid in the first scenario has an inaccurate postcard.
<Smerdyakov>
<end of parable>
_JusSx_ has quit ["leaving"]
<thruspa>
I think I understand what you are trying to say.
<thruspa>
The array array is created, but each entry is an aliased reference to the same vector.
<Smerdyakov>
Your code now implements the first scenario.
<thruspa>
That is, the inner Make Array is only called once.
<thruspa>
Of course, once I see it it's clearly obvious.
<thruspa>
Thank you very much!
<Smerdyakov>
Any time. :)
<Smerdyakov>
This is a demonstration of why imperativity is bad.
<thruspa>
I'd say that imperativity is evil, but sometimes a neccesary evil :) .
<thruspa>
How do you think I should define an array of arrays in OCaml. With a loop?
<Smerdyakov>
Array.create_matrix seems to do what you want handily.
<Smerdyakov>
I guess Array.make_matrix is the non-deprecated name.
<Smerdyakov>
I never use loops, personally, and I yell at people who _do_ use them. :-)
<thruspa>
Thanks. I think a need a good session of RTFM :-)
<thruspa>
Yes, it's Array.make_matrix.
<thruspa>
Well, I am really grateful. I was about to return to Haskell in desperation.
<Smerdyakov>
BTW, if you want high-performance numeric array code, consider SML w/ MLton instead of OCaml.
<Smerdyakov>
I guess OCaml special-cases representation of numeric arrays, but it won't do that for other related structures.
<Smerdyakov>
Boxing floating point values left and right
<thruspa>
OCaml has some great libraries. I chose it for that, besides the performance.
<thruspa>
I love the Graphics library. The Haskell equivalent is currently rather broken.
<danderson>
so, if I wanted to learn ocaml coming from a C/C++/Python background, where should I start?
<danderson>
I've also done a little lisp, but I do feel like I'm stepping into a very alien world with ocaml
<Smerdyakov>
danderson, you will want a book that introduces functional programming.
<danderson>
like I said, I have done some functional programming in the past, so it's not entirely alien. Just very :-)
<Smerdyakov>
If you can grok the tutorial in the OCaml manual, then that should do fine.
<thruspa>
Functional programming looks strange at first, but is very rewarding and productive.
<Smerdyakov>
People without significant FP experience generally can't, though.
<thruspa>
I love it.
<danderson>
right, I hear that of FP. But I'm still in the head-scratching phase so far.
<thruspa>
I'm currently learning OCaml, but I have been working with FP for some time. I started with Haskell, and I think OCaml is much more accesible.
<Smerdyakov>
I'm sorry to have to tell you that you may have trouble with OCaml if you find BNF diagrams complex. ;)
<ita>
what is a bnf diagram once again ? :-)
<Smerdyakov>
Just a context-free grammar in usual notation.
<Smerdyakov>
Folks generally feel free to include syntactic sugar for things like repetition, but it's basically CFG.
<thruspa>
Well, I've been having trouble with OCaml, as you know... :)