<laynor>
noob question: let's say I want to have a list that contains ints or floats, are variant types the best/only way to go? what about a "sum x y" function wich takes int or float arguments?
<pango_>
there's no such thing as a "list of ints or floats" in OCaml types
<laynor>
pango_: I know, I'm asking if the best way to simulate it is using variants
<laynor>
same for the sum function.
<laynor>
:-)
<Smerdyakov>
There is no good way to write overloaded functions in OCaml.
<Smerdyakov>
Variants are probably the best way to implement lists like you described, but you haven't given enough context for me to say for sure.
<laynor>
I'm just exploring the language while studying programming language semantics, so the question is not in any context, it's just that I'm curious.. OCaml seems a good language to describe formal semantics, so I guess it could be a good language to use to implement new languages.. So comes my interest :)
Smerdyakov has quit ["BRB"]
<laynor>
also, is there a way to detect integer overflow? I noticed that OCaml doesn't raise an exception.
<pango_>
shadow operators with ones that detect overflows
<laynor>
let (+) x y = let z = x + y in if (not x = (-y)) & z = 0 then raise IntegerOverflow else z
<laynor>
something like that?
<pango_>
in case of overflow the result won't be 0
<laynor>
mhm, I recall that it printed 0 with printf yesterday, but I can be wrong. let me check
Smerdyakov has joined #ocaml
<pango_>
there's an Overflow module in the Hump, but un-unusually, the page died
<twobitsprite>
I'm working on ocamlyacc, and I get an error when compiling that says "Unbound type constructor sexpr" but I have a "type sexpr ..." in the %{ %} block... Its complaining about the "%type <sexpr> input" line
<twobitsprite>
actually, it's complaining about the generated .mli file
<Smerdyakov>
The code in that block doesn't go into the .mli file, I think.
<Smerdyakov>
So you can't refer to it in %type directives.
<laynor>
pango_: lol, yesterday it printed 0 just because I was testing with powers of 2..
<twobitsprite>
Smerdyakov: er... so where should I specify it?
<Smerdyakov>
Another .ml file.
<twobitsprite>
ahh
eroyf has quit [Connection timed out]
<twobitsprite>
how do you convert a char into a string?
<twobitsprite>
seems like it should be just string_of_char... or at least listed in the "string conversion functions" section
<twobitsprite>
... or in the String module
ampex has quit []
<twobitsprite>
hmm... actually... if I was working on a compiler which output ocaml code as a target language, would it just be easier to use camlp4?
<twobitsprite>
instead of ocamlyacc/ocamllex and manually generating ocaml code
noteventime has quit ["Leaving"]
smimou has quit ["bli"]
fy__ has joined #ocaml
laynor has quit [Read error: 110 (Connection timed out)]
<Mr_Awesome>
let char_of_string = String.make 1;;
<Mr_Awesome>
i actually prefer haskell's way of handling strings as just a list of char's
spucky has joined #ocaml
<Mr_Awesome>
hmm, perhaps that apostrophe wasnt a good idea there
<Mr_Awesome>
but "chars" isnt a word. i just dont know
<mbishop>
Doesn't SML use a vector of chars?
* mbishop
looks at Smerdyakov for an answer
<Mr_Awesome>
mbishop: you mean vector in the sense of array
<Mr_Awesome>
?
<mbishop>
well, "vector" is the proper term for a single dimension matrix
<mbishop>
but generally yes, vector is the same as array
pango_ has quit [Remote closed the connection]
pango_ has joined #ocaml
<spucky>
I'm very new (as of about 10 minutes). How do I start cameleon?
<mbishop>
I never did cameleon to build right
<spucky>
I installed through Adept on Ubuntu, but I can't find the executable. I looked everywhere. I see an executable named "cameleon2.byte" in usr/bin
bluestorm_ has quit ["Konversation terminated!"]
<Ober>
guess I bought the only book on ocaml :(
<mbishop>
Which one is that?
<Ober>
pratical ocaml
<Ober>
il me lire francais au apprendre cette lang?
<Ober>
if there's a better book in french I can do that
Smerdyakov has quit ["Leaving"]
Smerdyakov has joined #ocaml
pantsd has joined #ocaml
<buluca>
what release is that in ubuntu?
<buluca>
1.9.13, same as debian :(
<spucky>
1.9.13 if you were asking me
Smerdyakov has quit ["Leaving"]
Smerdyakov has joined #ocaml
robyonrails has quit [Read error: 110 (Connection timed out)]
<mbishop>
Ober: there are a few good french books about Ocaml
<mbishop>
Ocaml for Scientists is a nice English one, a lot better than Practical OCaml heh
<Ober>
ok
<Ober>
looking for something for general purposes vs say math etc.
<mbishop>
Well Ocaml for Scientists has quite a bit of math, but nothing like The Art of Computer Programming or anything heh
<Ober>
yeah looking to replace my ruby prototypes :P
Smerdyakov has quit ["Leaving"]
<mbishop>
Ocaml for Scientists is expensive though
piggybox has quit []
shawn has quit ["This computer has gone to sleep"]
<Ober>
ahh
<Ober>
online help looks goo
<Ober>
good
piggybox_ has joined #ocaml
piggybox_ is now known as piggybox
spucky has quit [Remote closed the connection]
piggybox has quit ["Leaving"]
piggybox has joined #ocaml
thesoko has quit [Remote closed the connection]
thesoko has joined #ocaml
piggybox has quit [Read error: 104 (Connection reset by peer)]
piggybox has joined #ocaml
Smerdyakov has joined #ocaml
Smerdyakov has quit ["BRB"]
Smerdyakov has joined #ocaml
pants1 has joined #ocaml
pantsd has quit [Read error: 110 (Connection timed out)]
SmartIRC has joined #ocaml
SmartIRC has quit [Read error: 104 (Connection reset by peer)]
Smerdyakov has quit ["Leaving"]
robyonrails has joined #ocaml
Smerdyakov has joined #ocaml
love-pingoo has joined #ocaml
test1234 has joined #ocaml
ygrek has joined #ocaml
love-pingoo has quit ["Connection reset by pear"]
m3ga has joined #ocaml
test1234 has quit [Read error: 110 (Connection timed out)]
minciue has joined #ocaml
m3ga has quit ["disappearing into the sunset"]
bluestorm_ has joined #ocaml
shawn has joined #ocaml
Mr_Awesome has quit ["time to impregnate a moth"]
test1234 has joined #ocaml
noteventime has joined #ocaml
ygrek has quit []
thesoko has quit [Remote closed the connection]
thesoko has joined #ocaml
smimou has joined #ocaml
buluca has quit [Read error: 113 (No route to host)]
shawn has quit [Read error: 110 (Connection timed out)]
rwmjones_afk is now known as rwmjones
eroyf has joined #ocaml
Demitar has joined #ocaml
kniv7s has joined #ocaml
robyonrails has quit [Connection timed out]
laynor has joined #ocaml
pango_ has quit [Remote closed the connection]
pango_ has joined #ocaml
G has joined #ocaml
minciue has quit ["Ex-Chat"]
hsuh has joined #ocaml
<hsuh>
hello ocamlers
<fremo>
hello hsuh !
<hsuh>
i need help with a little problem today :)
<fremo>
go ahead ! :)
<hsuh>
lets say i need to print numbers in the range 0 to n, randomly
<hsuh>
but most importantly without repeating
<hsuh>
i have two ideas: either use a hash to mark the numbers that have already been printed (i dont like this solution)
<fremo>
true random ?
<hsuh>
or make a list from 0 to n and then remove the sorted number from the list (but this would need some extra functions because array and list don't have remove, right ?)
<hsuh>
no no
<hsuh>
just the ordinary pseudo random.. no scientific work here.. just play
<rwmjones>
hsuh, you want an "unsort" function
<rwmjones>
xavierbot, restart
<xavierbot>
Objective Caml version 3.10.0
<xavierbot>
Camlp4 Parsing version 3.10.0
<hsuh>
unsort, yes
<hsuh>
good name
<rwmjones>
just a sec, I have one
<hsuh>
x*vierbot is so nice
<rwmjones>
first define swap, like this:
<rwmjones>
let swap xs i j = if i <> j then ( let c = xs.(i) in xs.(i) <- xs.(j); xs.(j) <- c ) ;;
<xavierbot>
val swap : 'a array -> int -> int -> unit = <fun>
<rwmjones>
then unsort for arrays is:
<rwmjones>
let unsort_array xs = let n = Array.length xs in for i = 0 to n-2 do let j = Random.int (n - i) in swap xs i j done;;
<xavierbot>
val unsort_array : 'a array -> unit = <fun>
<rwmjones>
then unsort for lists is:
<rwmjones>
let unsort xs = Array.to_list (unsort_array (Array.of_list xs)) ;;
<xavierbot>
Characters 45-63:
<xavierbot>
let unsort xs = Array.to_list (unsort_array (Array.of_list xs)) ;;
<xavierbot>
^^^^^^^^^^^^^^^^^^
<xavierbot>
This expression has type unit but is here used with type 'a array
<rwmjones>
oops, I mean:
<rwmjones>
let unsort xs = let xs = Array.of_list xs in unsort_array xs; Array.to_list xs ;;
<xavierbot>
val unsort : 'a list -> 'a list = <fun>
<rwmjones>
then you need a range function:
<rwmjones>
let rec range a b = if a <= b then a :: range (a+1) b else [] ;;
<xavierbot>
val range : int -> int -> int list = <fun>
<rwmjones>
range 0 10 ;;
<xavierbot>
- : int list = [0; 1; 2; 3; 4; 5; 6; 7; 8; 9; 10]
<rwmjones>
unsort (range 0 10) ;;
<xavierbot>
- : int list = [0; 9; 6; 2; 5; 7; 3; 4; 1; 8; 10]
<rwmjones>
unsort (range 0 10) ;;
<xavierbot>
- : int list = [9; 3; 5; 7; 4; 0; 1; 2; 6; 8; 10]
<hsuh>
rwmjones: thats exactly what i need... many thanks man
<fremo>
nice
<rwmjones>
it's not very efficient ... if you need speed, you could do better by taking the algorithm from unsort_array and applying it directly to an array
<hsuh>
no, i just needed to unsort 400 elements for making a file for povray
<rwmjones>
unsort (range 0 10) ;;
<xavierbot>
- : int list = [8; 9; 0; 5; 7; 3; 4; 1; 6; 2; 10]
<rwmjones>
I think there's a bug in that actually :-)
<rwmjones>
unsort (range 0 10) ;;
<xavierbot>
- : int list = [9; 7; 8; 4; 3; 2; 5; 6; 0; 1; 10]
<fremo>
:)
<rwmjones>
I think the end point of the iteration in unsort_array should be n-1 instead of n-2
<rwmjones>
well, there you go, you can experiment :-)
<hsuh>
of course!
<hsuh>
let unsort_array xs = let n = Array.length xs in for i = 0 to n-1
<hsuh>
do let j = Random.int (n - i) in swap xs i j done;;
<xavierbot>
Characters 1-3:
<xavierbot>
Parse error: illegal begin of top_phrase
<xavierbot>
do let j = Random.int (n - i) in swap xs i j done;;
<xavierbot>
^^
<rwmjones>
you have to paste it in as a single line, it's a pain
<hsuh>
let unsort_array xs = let n = Array.length xs in for i = 0 to n-1
<hsuh>
do let j = Random.int (n - i) in swap xs i j done;;let unsort_array xs = let n = Array.length xs in for i = 0 to n-1 do let j = Random.int (n - i) in swap xs i j done;;
<hsuh>
<xavierbot>
Characters 1-3:
<xavierbot>
Parse error: illegal begin of top_phrase
<xavierbot>
do let j = Random.int (n - i) in swap xs i j done;;let unsort_array xs = let n = Array.length xs in for i = 0 to n-1 do let j = Random.int (n - i) in swap xs i j done;;
<xavierbot>
^^
<hsuh>
let unsort_array xs = let n = Array.length xs in for i = 0 to n-1 do let j = Random.int (n - i) in swap xs i j done;;
<xavierbot>
val unsort_array : 'a array -> unit = <fun>
<hsuh>
ok
<rwmjones>
hsuh, also, if you just redefine unsort_array, then you have to also redefine unsort, because otherwise unsort will continue to use the old definition
<rwmjones>
that's the way ocaml works
<hsuh>
let unsort xs = let xs = Array.of_list xs in unsort_array xs; Array.to_list xs ;;
<xavierbot>
val unsort : 'a list -> 'a list = <fun>
<hsuh>
unsort (range 0 10);
<rwmjones>
unsort (range 0 10) ;;
<xavierbot>
- : int list = [10; 9; 5; 4; 6; 3; 2; 1; 0; 8; 7]
<rwmjones>
unsort (range 0 10) ;;
<xavierbot>
- : int list = [10; 1; 8; 5; 6; 3; 2; 7; 0; 4; 9]
<rwmjones>
heh, still wrong
<rwmjones>
unsort (range 0 10) ;;
<xavierbot>
- : int list = [10; 8; 0; 7; 2; 3; 4; 5; 6; 1; 9]
<hsuh>
odd, my local version is working
<hsuh>
or not
<hsuh>
unsort(unsort(range 0 10));;
<xavierbot>
- : int list = [9; 8; 2; 7; 10; 1; 3; 4; 0; 6; 5]
<hsuh>
so ugly :)
<bluestorm_>
hm
<bluestorm_>
for i = 0 to n-1 do let j = Random.int (n - i) ...
<bluestorm_>
why don't you use for i = n - 1 downto 0 ?
<fremo>
let's do it with a tree now :)
Submarine has quit ["in Soviet Russia, Céline Dion owns you"]
<fremo>
or a hashtable...
<fremo>
no
EliasAmaral has joined #ocaml
thesoko has quit [Remote closed the connection]
thesoko has joined #ocaml
<hsuh>
let unsort_array xs = let n = Array.length xs in for i = n-1 downto 0 do let j = Random.int (n - 0) in swap xs i j done;;
<xavierbot>
val unsort_array : 'a array -> unit = <fun>
<hsuh>
let unsort xs = let xs = Array.of_list xs in unsort_array xs; Array.to_list xs ;;
<xavierbot>
val unsort : 'a list -> 'a list = <fun>
<hsuh>
unsort (range 0 10);;
<xavierbot>
- : int list = [4; 5; 9; 7; 3; 0; 10; 8; 2; 1; 6]
<hsuh>
unsort (range 0 10);;
<xavierbot>
- : int list = [6; 10; 0; 9; 1; 3; 2; 8; 4; 7; 5]
<hsuh>
maybe we should not restrict the range of int which the Random.int can generate
<hsuh>
it was Random.int (n-i) before
<hsuh>
so the last elements could only be swapped with the nearest neighbors.. or something like that
piggybox has quit [Success]
yp3cjp has joined #ocaml
hsuh has quit [Remote closed the connection]
<bluestorm_>
let unsort_array xs = let n = Array.length xs in for i = n-1 downto 0 do swap xs i (Random.int i) done;;
<xavierbot>
val unsort_array : 'a array -> unit = <fun>
<bluestorm_>
let unsort xs = let xs = Array.of_list xs in unsort_array xs; Array.to_list xs ;;
<xavierbot>
val unsort : 'a list -> 'a list = <fun>