<pdhborges>
Does anyone have an example with batteries?
<orbitz>
temoto: Ocaml's stdlib is fairly aenmic, you should probably choose Core or Battery's fairly early
bzzbzz has joined #ocaml
<flux>
something like this can be helpful (untested): let array f chan xs = ignore (Array.fold_left (fun sep x -> Printf.fprintf chan "%s%s" sep (f x); " ") "" xs)
<orbitz>
I don't think Core has anythign in partifular for this
<flux>
then this might work: Printf.printf "array of integers: %a" (array string_of_int) [|1; 2|]
<temoto>
What's ignore?
<orbitz>
'a -> unit
<flux>
it ignores the return value, making the function return unit instead of a string
<thelema>
pdhborges: you want an example of printing an int array using batteries?
<flux>
(similar could be achieved with %t as well, I often go with that, or just converting into a string)
<temoto>
flux, yes, it finds an insertion point to maintain order
<pdhborges>
watch out that is the overflowing mid version!
<temoto>
'overflowing mid' ?
<pdhborges>
mid = (low + high) / 2
<pdhborges>
:(
<temoto>
should be (low/2) + (high/2) ?
<flux>
pdhborges, not really an issue in ocaml..
<thelema>
temoto: see "extra credit" at the end of the intro on that page.
<flux>
I guess it gets more complicated
<thelema>
flux: overflow is an issue for ocaml still.
<flux>
you need to handle the last bit separately at least
<pdhborges>
low + ((high - low) / 2 temoto
<flux>
thelema, but you can only have 16M elements on 32-bit platforms, and on 64 bit platforms it is extremely likely you are able to find hardware to overflow it with
<flux>
UNlikely
<thelema>
or on most platforms: (low + high) lsr 1
<temoto>
:)
<thelema>
flux: true.
<flux>
heh, my version didn't work I guess, or atleast it just raised Not_found if low = high
<pdhborges>
looks like it is also true on 64bits
<pdhborges>
max_int is 4611686018427387903
<pdhborges>
maxarrlen is 18014398509481983
<flux>
actualyl maybe not
<flux>
that version uses a version where high indicates the last position, while mine indicates the last+1
<pdhborges>
I must be doing something wrong
<pdhborges>
I installed batteries 1.3
<pdhborges>
then require it on the top level
<pdhborges>
opened Batteries but then I can't open, for example Array
<temoto>
flux, when you said that set gives that as well, did you mean Set.split?
<flux>
temoto, yes
<pdhborges>
actually I can open Array
<pdhborges>
but when I try to use a function from array it spits Reference to undefined global `Batteries'
<flux>
temoto, well, actually, a set only has one of each equal value
<flux>
temoto, so if you have multiple equal values in the original set, you are in trouble
<temoto>
Yeah, that's fine for me.
<flux>
oh, ok
<temoto>
How to load my module into interpreter and play with function i've defined via 'let'?
<flux>
there are two ways
<flux>
either, you can do exactly what you wanted, with #load "foo.cmo";;
<flux>
or, you can include the source code with "#use "foo.ml";;
<flux>
in the former situation, your module is called Foo
<flux>
temoto, btw, you don't happen to use emacs perchance?
<flux>
there is some vim support as well, but I don't know if they have "run toplevel, copy-paste current function into toplevel"-kind of functionality
<flux>
temoto, in any case, it is a good idea to look into vim support. at least it has the type throwback-feature, ie. if you've compiled the source code with -dtypes, you can go over any expression and retrieve its type.
<flux>
it'll be useful at times.
<thelema>
pdhborges: the right way to use batteries in the toplevel is to put the code in batteries' ocamlinit in your own ~/.ocamlinit, and batteries will autoload
<pdhborges>
thelema: it worked
<pdhborges>
thanks
lopex has quit []
larhat has quit [Quit: Leaving.]
trch has joined #ocaml
trch has left #ocaml []
ChristopheT has joined #ocaml
Cyanure has quit [Remote host closed the connection]
hnrgrgr has quit [Remote host closed the connection]
Rolands has quit [Read error: Operation timed out]
pdhborges has left #ocaml []
hnrgrgr has joined #ocaml
jderque has joined #ocaml
__marius__ has joined #ocaml
<temoto>
How to use Map module?
<temoto>
Map.Make Int gives error
<temoto>
unbound constructor
<rixed>
Int is not a module from stdlib, is it?
ChristopheT has quit [Ping timeout: 260 seconds]
<rixed>
Why not "Map.Make(struct type t=int let compare=compare end)
<rixed>
module IntMap = Map.Make(struct type t=int let compare=compare end);; -> works allright
<temoto>
Thank you.
<temoto>
Amazing how much boilerplate is required for a simple dictionary.
<rixed>
temoto: yes. Would be simpler if there were actualy an Int module. Maybe in batteries?
<temoto>
Batteries claim to have Map.IntMap already defined.
<temoto>
There should be some way to write something to ~/.ocamlsomething to autoload batteries, right?
joewilliams_away is now known as joewilliams
<temoto>
okay i found it
<temoto>
Is there something like import Map.IntMap as IntMap ?
<temoto>
Map.IntMap.add x y m does not seem nice
<rossberg>
structure IntMap = Map.IntMap
ulfdoz has joined #ocaml
<temoto>
unbound value structure
<rossberg>
sorry, "module" instead of "structure" 8-}
<temoto>
Thanks.
jderque has quit [Ping timeout: 248 seconds]
yezariaely has joined #ocaml
<temoto>
I need to fill Map inside a side-effect for-loop. Do i need to use 'ref' or something? Can't find an example.
yezariaely has quit [Client Quit]
<temoto>
okay, wires <- IntMap.add x y !wires seems to work
jderque has joined #ocaml
munga has quit [Quit: Ex-Chat]
<f[x]>
thelema, I guess it was a temporary problem and I blame java :)
<f[x]>
now it fails some unit test
eikke has quit [Ping timeout: 246 seconds]
<temoto>
Is there a shortcut for result <- result + ... ?
<temoto>
result <- !result + ...
<f[x]>
let (+=) x y = x := !x + y
<temoto>
How := is different from <- ?
<f[x]>
# (:=);;
<f[x]>
- : 'a ref -> 'a -> unit = <fun>
<f[x]>
<- is syntax to specify modification of mutable record fields
<f[x]>
:= is a function over one specific record type - 'a ref
<flux>
and ref is type 'a ref = { mutable contents : 'a }
<f[x]>
let (:=) x y = x.contents <- y;;
<temoto>
So where should i use each of them?
<flux>
when you have your own records that have mutable fields (rare)
<flux>
then you use <-
<flux>
if you use the ref-type, you use :=
<temoto>
Thanks.
<flux>
(or, when you have objects with mutable fields, those use <- as well)
<temoto>
Is there a syntax for partial application of operators?
PiepScuim has quit [Quit: Ex-Chat]
bzzbzz has quit [Quit: leaving]
zorun_ has joined #ocaml
ftrvxmtrx has quit [Quit: Leaving]
bzzbzz has joined #ocaml
<thelema>
temoto: (+) 3
pdhborges has joined #ocaml
mnabil has quit [Remote host closed the connection]
<temoto>
thelema, thanks.
<thelema>
be aware that [(-) 4] is (fun x -> 4 - x)
Cyanure has joined #ocaml
jderque has quit [Quit: Leaving.]
<temoto>
of course
<temoto>
Haskell has neat syntax for specifying operands at particular side of infix operators.
<temoto>
afair
<bitbckt>
Haskell allows `` around any prefix fn to allow use as an infix operator.
<bitbckt>
and () around infix operators to convert to prefix operators.
<temoto>
And what is said as a third option.
<temoto>
s/is/i/
* bitbckt
nods
<temoto>
Okay, i actually need it to be second operand. Is there builtin flip?
<temoto>
flip (f a b) -> f b a
jderque has joined #ocaml
<pdhborges>
temoto: what are you doing?
<temoto>
pdhborges, counting elements <= x in array.
<temoto>
Hey, i can use (>=)x
Modius has joined #ocaml
avsm2 has quit [Quit: avsm2]
zorun_ is now known as zorun
<thelema>
temoto: you mean (>) x
<temoto>
yeah
<thelema>
there's only flip in batteries, not in the stdlib
lopex has joined #ocaml
emmanuelux has joined #ocaml
Cyanure has quit [Ping timeout: 248 seconds]
eikke has joined #ocaml
<_habnabit>
When I get an error like "this expression has type X but an expression was expected of type Y", how can I tell /why/ it's supposed to be type Y?
<flux>
_habnabit, no, but that would be great. infact there is research on the area.
<_habnabit>
Dang. :(
<flux>
_habnabit, the basic strategy for debugging such issues is as follows:
<flux>
1) go through the source and use emacs (or vim) type throwback
<flux>
2) when you find a type that isn't what you think it should be, annotate the value
<flux>
3) compile
<flux>
4) 1()
<temoto>
How to unpack value from constructor?
<flux>
temoto, in a generic way: match Foo 42 with Foo a -> a | Bar b -> b
<temoto>
let Just x in some := x is syntax error
<temoto>
ah match, thanks.
<flux>
if there is only one constructor (rare), you can use: let Foo x = Foo 42
<flux>
it works with multiple constructors as well, but you get a warning about non-exhaustiveness
<temoto>
Yeah, exactly what i need.
<temoto>
except it is still a syntax error
<temoto>
let Just idx = Just (array_find pair bx) in idx_b := idx
<flux>
hmm
<flux>
if Just your own type?
<flux>
(constructor)
<flux>
and just not something you've mixed up with Haskell..
<flux>
because in O'Caml land it's called Some
<temoto>
type maybe_int = None | Just of int;;
<flux>
oh, ok
<temoto>
But yeah, i should do as romans.
<flux>
it is not a syntax error in my toplevel
<flux>
it fails in "array_find" with me (not a syntax error)
<temoto>
Yeah i skipped ; in previous line.
<_habnabit>
flux, do you personally use emacs?
<temoto>
Compile-time printf is very nice.
avsm has quit [Quit: Leaving.]
<temoto>
i mean % args checking
<pdhborges>
also a bit compiler hack
<pdhborges>
:X
pdhborges has left #ocaml []
<flux>
_habnabit, yes
<_habnabit>
flux, with tuareg-mode ? How are you seeing the type of an expression?
<thelema>
_habnabit: compile with -annot anc then do C-c C-t
<thelema>
s/anc/and/
vivanov has quit [Ping timeout: 240 seconds]
<_habnabit>
thelema, hm, but will it still -annot if the types are disagreeing?
<flux>
_habnabit, yes. 1) compile with -dtypes 2) have ocaml-mode around as well (its caml-types.el is used) 3) go over an expression, press C-c C-t
<thelema>
yes, up to the point that types agree
<thelema>
flux: -dtypes is the old way
<flux>
hmph
<flux>
what is the new way? I haven't manually written that for a while..
<flux>
-annot?
<thelema>
yes
pdhborges has joined #ocaml
eikke has quit [Ping timeout: 258 seconds]
impy has quit [Read error: No route to host]
philtor has joined #ocaml
<temoto>
How to use String.concat with BatEnum?
<pdhborges>
temoto: I guess you could use List.of_enum
<temoto>
pdhborges, works, thanks.
pdhborges has quit [Quit: Leaving.]
ankit9 has quit [Read error: Connection reset by peer]
ygrek has quit [Ping timeout: 246 seconds]
<temoto>
ocamlfind ocamlc -package batteries main.ml gives Reference to undefined global `Batteries` ( i have open Batteries line )
<adrien>
missing -linkpkg ?
<temoto>
ocamlfind ocamlc -package batteries -linkpkg main.ml gives same error
<temoto>
(That's Ubuntu 10.10 with batteries from apt)
ankit9 has joined #ocaml
<temoto>
Also, i'd like to just run the file, without explicit compilation.
<temoto>
Is it possible with batteries?
ecc has joined #ocaml
pdhborges has joined #ocaml
ymasory has joined #ocaml
hto has joined #ocaml
oriba_ has joined #ocaml
<_habnabit>
Hm, neither tuareg-mode nor caml-mode has a C-c C-t.
oriba has quit [Ping timeout: 258 seconds]
oriba_ is now known as oriba
sku has joined #ocaml
ulfdoz has quit [Ping timeout: 260 seconds]
nantralien has joined #ocaml
<nantralien>
adrien i could'nt parametrize my parser with a module
<nantralien>
so i had to throw away my functors to go with plain modules