"ocaml_beginners"::[] using a list instead of option

View: New views
6 Messages — Rating Filter:   Alert me  

"ocaml_beginners"::[] using a list instead of option

by Martin DeMello :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Is there any disadvantage (performance, e.g.) to having a function
return [] or [string], and consume it via

let collecting traversal =
  let retval = ref [] in
  let add_word = function [] -> () | s::_ -> retval := s :: !retval in
  traversal add_word;
  List.rev !retval

rather than

  let add_word = function None -> () | Some s -> retval := s :: !retval in...

?

I just think the former looks neater, but of course if I'm taking a
performance hit by using this in my inner loops I'll switch to using
an option.

martin

Re: "ocaml_beginners"::[] using a list instead of option

by Lukasz Stafiniak :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I think performance-wise the difference is very small. But you should
use the option type to express your program's semantics correctly!

On Sat, Oct 31, 2009 at 10:09 AM, Martin DeMello
<martindemello@...> wrote:

>
>
>
> Is there any disadvantage (performance, e.g.) to having a function
> return [] or [string], and consume it via
>
> let collecting traversal =
> let retval = ref [] in
> let add_word = function [] -> () | s::_ -> retval := s :: !retval in
> traversal add_word;
> List.rev !retval
>
> rather than
>
> let add_word = function None -> () | Some s -> retval := s :: !retval in...
>
> ?
>
> I just think the former looks neater, but of course if I'm taking a
> performance hit by using this in my inner loops I'll switch to using
> an option.
>
> martin

Re: "ocaml_beginners"::[] using a list instead of option

by Martin DeMello :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

thinking about it, you're  right, i was being silly :) code changed to use
option type.

martin

On Sat, Oct 31, 2009 at 3:06 PM, Lukasz Stafiniak <lukstafi@...>wrote:

>
>
> I think performance-wise the difference is very small. But you should
> use the option type to express your program's semantics correctly!
>
>
> On Sat, Oct 31, 2009 at 10:09 AM, Martin DeMello
> <martindemello@... <martindemello%40gmail.com>> wrote:
> >
> >
> >
> > Is there any disadvantage (performance, e.g.) to having a function
> > return [] or [string], and consume it via
> >
> > let collecting traversal =
> > let retval = ref [] in
> > let add_word = function [] -> () | s::_ -> retval := s :: !retval in
> > traversal add_word;
> > List.rev !retval
> >
> > rather than
> >
> > let add_word = function None -> () | Some s -> retval := s :: !retval
> in...
> >
> > ?
> >
> > I just think the former looks neater, but of course if I'm taking a
> > performance hit by using this in my inner loops I'll switch to using
> > an option.
> >
> > martin
>
>  
>


[Non-text portions of this message have been removed]


Re: "ocaml_beginners"::[] using a list instead of option

by Lukasz Stafiniak :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

You may need some general functions that lack from the basic standard library:

let unsome default = function
  | None -> default
  | Some v -> v


let map_some f l =
  let rec maps_f accu = function
    | [] -> accu
    | a::l -> maps_f (match f a with None -> accu
        | Some r -> r::accu) l
  in
  List.rev (maps_f [] l)

etc. (see Batteries).

On Sun, Nov 1, 2009 at 11:04 AM, Martin DeMello <martindemello@...> wrote:

>
>
>
> thinking about it, you're right, i was being silly :) code changed to use
> option type.
>
> martin
>
> On Sat, Oct 31, 2009 at 3:06 PM, Lukasz Stafiniak <lukstafi@...>wrote:
>
> >
> >
> > I think performance-wise the difference is very small. But you should
> > use the option type to express your program's semantics correctly!

Re: "ocaml_beginners"::[] using a list instead of option

by Martin DeMello :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

How would I use batteries along with OCamlMakefile? (I tried the
ocamlbuild way and got nowhere; I've not been able to get mikmatch and
ledit working with ocamlbuild at all.)

I have batteries installed in /usr/lib/ocaml/site-lib/batteries

Here's my makefile:

# ----------------------------------------------------
RESULT = varix

SOURCES = \
                                        utility.ml\
                                        dawg.ml bag.ml sset.ml\
                                        search.ml \
                                        debug.ml \
                                        ledit/cursor.ml \
                                        ledit/ledit.mli ledit/ledit.ml \
                                        varix.ml

LIBS = unix bigarray str pcre run_mikmatch_pcre
PACKS = unix bigarray str mikmatch_pcre pcre
INCDIRS = /usr/lib/ocaml/camlp5 /usr/lib/ocaml/pcre-ocaml
/usr/lib/ocaml/site-lib/mikmatch_pcre
CREATE_LIB = yes
PRE_TARGETS = ledit/pa_local.cmo ledit/pa_def.cmo
USE_CAMLP4 = yes
PP = ./camlp4find $(PACKS)
export PP

OCAMLMAKEFILE = OCamlMakefile
include $(OCAMLMAKEFILE)
# ----------------------------------------------------

On Sun, Nov 1, 2009 at 5:18 PM, Lukasz Stafiniak <lukstafi@...> wrote:

>
>
>
> You may need some general functions that lack from the basic standard library:
>
> let unsome default = function
> | None -> default
> | Some v -> v
>
> let map_some f l =
> let rec maps_f accu = function
> | [] -> accu
> | a::l -> maps_f (match f a with None -> accu
> | Some r -> r::accu) l
> in
> List.rev (maps_f [] l)
>
> etc. (see Batteries).
>
> On Sun, Nov 1, 2009 at 11:04 AM, Martin DeMello <martindemello@...> wrote:
> >
> >
> >
> > thinking about it, you're right, i was being silly :) code changed to use
> > option type.
> >
> > martin
> >
> > On Sat, Oct 31, 2009 at 3:06 PM, Lukasz Stafiniak <lukstafi@...>wrote:
> >
> > >
> > >
> > > I think performance-wise the difference is very small. But you should
> > > use the option type to express your program's semantics correctly!
>
>

Re: "ocaml_beginners"::[] using a list instead of option

by Peng Zang :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

I'm not sure how to do it on your system, but with a GODI based OCaml
distribution all you have to do is add "batteries" into your "PACKS"
definition.

In my opinion, the only way to use OCaml is through GODI.  It makes installing
and using packages easy.  If you haven't tried GODI, I strongly urge you to
give it a try.  It makes a world of difference.

Peng


On Monday 02 November 2009 06:26:22 am Martin DeMello wrote:

> How would I use batteries along with OCamlMakefile? (I tried the
> ocamlbuild way and got nowhere; I've not been able to get mikmatch and
> ledit working with ocamlbuild at all.)
>
> I have batteries installed in /usr/lib/ocaml/site-lib/batteries
>
> Here's my makefile:
>
> # ----------------------------------------------------
> RESULT = varix
>
> SOURCES = \
> utility.ml\
> dawg.ml bag.ml sset.ml\
> search.ml \
> debug.ml \
> ledit/cursor.ml \
> ledit/ledit.mli ledit/ledit.ml \
> varix.ml
>
> LIBS = unix bigarray str pcre run_mikmatch_pcre
> PACKS = unix bigarray str mikmatch_pcre pcre
> INCDIRS = /usr/lib/ocaml/camlp5 /usr/lib/ocaml/pcre-ocaml
> /usr/lib/ocaml/site-lib/mikmatch_pcre
> CREATE_LIB = yes
> PRE_TARGETS = ledit/pa_local.cmo ledit/pa_def.cmo
> USE_CAMLP4 = yes
> PP = ./camlp4find $(PACKS)
> export PP
>
> OCAMLMAKEFILE = OCamlMakefile
> include $(OCAMLMAKEFILE)
> # ----------------------------------------------------
>
> On Sun, Nov 1, 2009 at 5:18 PM, Lukasz Stafiniak <lukstafi@...> wrote:
> > You may need some general functions that lack from the basic standard
> > library:
> >
> > let unsome default = function
> >
> > | None -> default
> > | Some v -> v
> >
> > let map_some f l =
> > let rec maps_f accu = function
> >
> > | [] -> accu
> > | a::l -> maps_f (match f a with None -> accu
> > | Some r -> r::accu) l
> >
> > in
> > List.rev (maps_f [] l)
> >
> > etc. (see Batteries).
> >
> > On Sun, Nov 1, 2009 at 11:04 AM, Martin DeMello <martindemello@...>
wrote:
> > > thinking about it, you're right, i was being silly :) code changed to
> > > use option type.
> > >
> > > martin
> > >
> > > On Sat, Oct 31, 2009 at 3:06 PM, Lukasz Stafiniak
<lukstafi@...>wrote:
> > > > I think performance-wise the difference is very small. But you should
> > > > use the option type to express your program's semantics correctly!
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.7 (GNU/Linux)

iD8DBQFK7uaCfIRcEFL/JewRAvVSAJ0alMIV9FfjqMh/CxIwIJLbHqHmlQCgglBY
YHLgW6QiwdVpzZ8NjE89uVg=
=fXUt
-----END PGP SIGNATURE-----