Skip to search.

Breaking News Visit Yahoo! News for the latest.

×Close this window

ocaml_beginners · Ocaml Beginners

The Yahoo! Groups Product Blog

Check it out!

Group Information

? Already a member? Sign in to Yahoo!

Yahoo! Groups Tips

Did you know...
Message search is now enhanced, find messages faster. Take it for a spin.

Messages

Advanced
Messages Help
Messages 963 - 992 of 13887   Oldest  |  < Older  |  Newer >  |  Newest
Messages: Show Message Summaries Sort by Date ^  
#963 From: Stalkern 2 <stalkern2@...>
Date: Wed Apr 2, 2003 6:46 am
Subject: A bunch of tutorials
stalkern2
Send Email Send Email
 
Hello to everybody

I have just finished putting online a bunch of tutorials either by me or
(mostly) by Stefano Zacchiroli. You can find them under the name "OcamLearn"
here
	 http://www.connettivo.net/rubrique.php3?id_rubrique=7

Ciao
Ernesto

#964 From: Johann Spies <jspies@...>
Date: Wed Apr 2, 2003 9:50 am
Subject: Re: "ocaml_beginners"::[] A bunch of tutorials
jspies@...
Send Email Send Email
 
On Wed, Apr 02, 2003 at 08:46:51AM +0200, Stalkern 2 wrote:
> Hello to everybody
>
> I have just finished putting online a bunch of tutorials either by me or
> (mostly) by Stefano Zacchiroli. You can find them under the name "OcamLearn"
> here
>  http://www.connettivo.net/rubrique.php3?id_rubrique=7

Thank you for the effort you put in to help others like me.

Regards.
Johann
--
Johann Spies          Telefoon: 021-808 4036
Informasietegnologie, Universiteit van Stellenbosch

      "If the Son therefore shall make you free, ye shall be
       free indeed."         John 8:36

#965 From: "billspight" <billspight@...>
Date: Thu Apr 3, 2003 7:19 pm
Subject: Graphics error message
billspight
Send Email Send Email
 
Hi!

Using OCamlWin I tried entering the following from the Chailloux,
Manoury, Pagano book, and got these responses.

# let from_rgb (c : Graphics.color) =
   let r = c / 65536 and g = c / 256 mod 256 and b = c mod 256
   in (r,g,b);;
val from_rgb : Graphics.color -> int * int * int = <fun>
# let inv_color (c : Graphics.color) =
   let (r,g,b) = from_rgb c
   in Graphics.rgb (255-r) (255-g) (255-b);;
Reference to undefined global `Graphics'

What is going on?

(I set the correct environmental variables for OCAML lib and PATH.)

Many thanks,

Bill

#966 From: Issac Trotts <ijtrotts@...>
Date: Thu Apr 3, 2003 10:18 pm
Subject: Re: "ocaml_beginners"::[] Graphics error message
issac_trotts
Send Email Send Email
 
How about #load "graphics.cma"?

Issac

billspight wrote:

>Hi!
>
>Using OCamlWin I tried entering the following from the Chailloux,
>Manoury, Pagano book, and got these responses.
>
># let from_rgb (c : Graphics.color) =
>  let r = c / 65536 and g = c / 256 mod 256 and b = c mod 256
>  in (r,g,b);;
>val from_rgb : Graphics.color -> int * int * int = <fun>
># let inv_color (c : Graphics.color) =
>  let (r,g,b) = from_rgb c
>  in Graphics.rgb (255-r) (255-g) (255-b);;
>Reference to undefined global `Graphics'
>
>What is going on?
>
>(I set the correct environmental variables for OCAML lib and PATH.)
>
>Many thanks,
>
>Bill
>
>
>
>To unsubscribe from this group, send an email to:
>ocaml_beginners-unsubscribe@yahoogroups.com
>Archives up to 11 October 2002 are also at
http://membres.lycos.fr/ipotesi/OCAML/ocaml-beginners_archive/index.html
>The archives of the very official ocaml list (the seniors' one) can be found at
http://caml.inria.fr
>Attachments are banned and you're asked to be polite, avoid flames etc. etc.
>
>Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
>
>
>
>
>

#967 From: Bill Spight <billspight@...>
Date: Thu Apr 3, 2003 11:10 pm
Subject: Re: "ocaml_beginners"::[] Graphics error message
billspight
Send Email Send Email
 
Dear Issac,

--- Issac Trotts <ijtrotts@...> wrote:
> How about #load "graphics.cma"?
>

That works. :-)

Many thanks,

Bill

__________________________________________________
Do you Yahoo!?
Yahoo! Tax Center - File online, calculators, forms, and more
http://tax.yahoo.com

#968 From: Daniel Andor <Daniel.Andor@...>
Date: Fri Apr 4, 2003 12:22 am
Subject: Dynamic Programming
daniel_andor
Send Email Send Email
 
Hi,

I'm wondering about implementation of dynamic programming algorithms in
OCaml -- essentially, how does it relate to recursion?  Does anyone have a
small example or good explanation?

At the moment I'm trying to implement Neville's algorithm, but of course
there are many other practical examples from bioinformatics etc.

There's an interesting webpage about dynamic programming in Haskell (which
is lazy):
http://www.csse.monash.edu.au/~lloyd/tildeStrings/Alignment/92.IPL.html
How would that approach translate to OCaml without losing all efficiency?

Thanks,
Daniel.

#969 From: Manos Renieris <er@...>
Date: Fri Apr 4, 2003 12:47 am
Subject: Re: "ocaml_beginners"::[] Dynamic Programming
er@...
Send Email Send Email
 
On Fri, Apr 04, 2003 at 01:22:18AM +0100, Daniel Andor wrote:
> Hi,
>
> I'm wondering about implementation of dynamic programming algorithms in
> OCaml -- essentially, how does it relate to recursion?  Does anyone have a
> small example or good explanation?

Using the imperative features of Ocaml, dynamic programming algorithms
can be copied almost exactly from standard table-filling pseudocode.
The memoization approach is also feasible, and you can code it as two
mutually recursive functions.

What are you looking for exactly?

> There's an interesting webpage about dynamic programming in Haskell (which
> is lazy):
> http://www.csse.monash.edu.au/~lloyd/tildeStrings/Alignment/92.IPL.html
> How would that approach translate to OCaml without losing all efficiency?

As far as I can tell, there is no extra efficiency coming from the
laziness. Instead, the author is showing how to achieve the same
complexity in Haskell that you would easily get from the
imperative features in a language like Ocaml.

-- Manos

#970 From: james woodyatt <jhw@...>
Date: Fri Apr 4, 2003 1:12 am
Subject: Re: "ocaml_beginners"::[] Dynamic Programming
dr_strychnine
Send Email Send Email
 
On Thursday, Apr 3, 2003, at 16:22 US/Pacific, Daniel Andor wrote:
>
> I'm wondering about implementation of dynamic programming algorithms in
> OCaml -- essentially, how does it relate to recursion?  Does anyone
> have a
> small example or good explanation?

Recursion is just the traditional pure functional way of achieving what
imperative programmers use loop constructs to do.  The "dynamic
programming algorithm" mentioned in the article you linked, i.e.
<http://www.csse.monash.edu.au/~lloyd/tildeStrings/Alignment/
92.IPL.html>, can be implemented in Ocaml without going insane.  You
just need to define the appropriate recursive function, and you need to
use the 'lazy' keyword to get the lazy evaluation that this algorithm
calls out.

Have a close look at the Lazy module in the Ocaml standard library, for
more details about how to get lazy evaluation.


--
j h woodyatt <jhw@...>

#971 From: "lambdawannabe" <lambdawannabe@...>
Date: Fri Apr 4, 2003 1:17 pm
Subject: functional way of generating text
lambdawannabe
Send Email Send Email
 
Hello Everyone!

I made a program that contains a lot of Printf's to a file. While
programming i did a lot of (camlp4r syntax) do { ... }. This is a
very imperative style of programming. What way would be a more
functional solution to this sort of programs? Is there any sourcecode
available where i should take a look at?

Thank you for your advise!

The pa_extend syntax can be fitted into an expression, so could i
introduce variables in the scope by doing: let ... in EXTEND ... END?

Best Regards!

PS.
In my MingW build in the lib/camlp4 directory the file gramlib.lib
should be named gramlib.a, however this renaming is easely done by
hand. All the other library files are named *.a (correctly).

#972 From: Stalkern 2 <stalkern2@...>
Date: Fri Apr 4, 2003 2:07 pm
Subject: Re: "ocaml_beginners"::[] functional way of generating text
stalkern2
Send Email Send Email
 
Il  Friday 04 April 2003 15:17, lambdawannabe ha scritto:
> <html><body>
>
>
> <tt>
> Hello Everyone!<BR>
> <BR>
> I made a program that contains a lot of Printf's to a file.

Do a lot of Format.sprintf instead and fetch the strings around in a
functional way, ouputting it only at a key-passage that you can control.

Imperative actions are like open doors, if you have too many you can't watch
who's getting in an out of all of them. BTW, printing arguments can be
gathered in a functional way, but printing itself is imperative.

Ciao
Ernesto

#973 From: "lambdawannabe" <lambdawannabe@...>
Date: Sat Apr 5, 2003 6:04 pm
Subject: Re: "ocaml_beginners"::[] functional way of generating text
lambdawannabe
Send Email Send Email
 
Hi Ernesto,

I can't seem to get it working by using Format.sprintf while
Format.fprintf works. How do i keep the "formatting stream" when i
use sprintf?

Example:

value items = ["one"; "two"; "tree"; "four"];

value rec rest items first =
   match items with
   [ [] -> ""
   | [ item :: more ] ->
       (if first then (Format.sprintf "%s" item) else
          (Format.sprintf ",@ %s" item)) ^ (rest more False)
   ]
;

value text = Format.sprintf "ident here>@[<b>%s@]@?" (rest items
True);

Printf.printf "%s\n" text;

I would actually like to do something like:

(* doesn't work *)

value text = Format.sprintf ("ident here>@[<b>" ^
         (String.concat ",@ " items) ^ "@]@?");

How do these printf functions convert their constant string argument
into something else? Is this done by camlp4?

I've read about the Monad technique is this something i can use?

Thank you for your reply!

Best regard,
Sander

#974 From: Stalkern 2 <stalkern2@...>
Date: Sat Apr 5, 2003 10:50 pm
Subject: Re: "ocaml_beginners"::[] functional way of generating text
stalkern2
Send Email Send Email
 
Il  Saturday 05 April 2003 20:04, lambdawannabe ha scritto:
> <html><body>
>
>
> <tt>
> Hi Ernesto,<BR>
> <BR>
> I can't seem to get it working by using Format.sprintf while <BR>
> Format.fprintf works. How do i keep the "formatting stream" when
> i <BR> use sprintf?<BR>
> <BR>
> Example:<BR>

Hi, I have stopped developing Camlp4 since Daniel De Rauglaudre, the inventor
of Camlp4, broke with Ocaml and so Camlp4 seems to be unmantained at present.

In any case, start with a clean plain ocaml function.

You seem for instance to use True as a boolean: true is, not "True".

Then, you don't use an accumulator for your recursive function: you should.
Have tail-recursive functions, where the last operation is a call to the
function itself (if the last operation is <a string>^<a call to the function
itself>, the last operation is ^, not <a call to the function itself>).

Reading your code I still don't have a clear perception of what result you
want to achieve. I guess

ident here>one,
two,
tree,
four

but the code is still blur, work it out a (little) bit and it will pay you
off.

Then, to know how to fit it with Camlp4, you'd better knock at Daniel's door (
daniel.de_rauglaudre@... ) to know to what extent it advises you to go
on with Camlp4. No matter how precious Camlp4 is for you and for everybody,
if he does not answer or tells you that Camlp4 is now unmantained, I'd ask
for confirmation in the caml-list and if there still is no answer or bad
answer, turn to ocamllex and ocamlyacc.

Sorry but sailing with unmantained software is a risky activity, that I
consider unsuitable for beginners like us.

Ciao
Ernesto

#975 From: Remi Vanicat <vanicat+egroups@...>
Date: Sat Apr 5, 2003 11:53 pm
Subject: Re: "ocaml_beginners"::[] functional way of generating text
dl_ens
Send Email Send Email
 
Stalkern 2 <stalkern2@...> writes:

> Il  Saturday 05 April 2003 20:04, lambdawannabe ha scritto:
>> <html><body>
>>
>>
>> <tt>
>> Hi Ernesto,<BR>
>> <BR>
>> I can't seem to get it working by using Format.sprintf while <BR>
>> Format.fprintf works. How do i keep the "formatting stream" when
>> i <BR> use sprintf?<BR>
>> <BR>
>> Example:<BR>
>
> Hi, I have stopped developing Camlp4 since Daniel De Rauglaudre, the inventor
> of Camlp4, broke with Ocaml and so Camlp4 seems to be unmantained at
> present.

This is false. bug fix are still apply to camlp4, even after Daniel De
Rauglaudre broke with the rest of the team... The only problem may be
that as none know camlp4 as Daniel was, the bug might be fix latter,
but camlp4 is still there, and will stay there. Several member of the
ocaml team (even Xavier Leroy) have said that.

[...]

--
Rémi Vanicat
vanicat@...
http://dept-info.labri.u-bordeaux.fr/~vanicat

#976 From: Remi Vanicat <vanicat+egroups@...>
Date: Sun Apr 6, 2003 12:29 am
Subject: Re: "ocaml_beginners"::[] functional way of generating text
dl_ens
Send Email Send Email
 
"lambdawannabe" <lambdawannabe@...> writes:

> Hi Ernesto,
>
> I can't seem to get it working by using Format.sprintf while
> Format.fprintf works. How do i keep the "formatting stream" when i
> use sprintf?
>
> Example:
>
> value items = ["one"; "two"; "tree"; "four"];
>
> value rec rest items first =
>   match items with
>   [ [] -> ""
>   | [ item :: more ] ->
>       (if first then (Format.sprintf "%s" item) else
>          (Format.sprintf ",@ %s" item)) ^ (rest more False)
>   ]
> ;
>
> value text = Format.sprintf "ident here>@[<b>%s@]@?" (rest items
> True);
>
> Printf.printf "%s\n" text;
>
> I would actually like to do something like:
>
> (* doesn't work *)
>
> value text = Format.sprintf ("ident here>@[<b>" ^
>         (String.concat ",@ " items) ^ "@]@?");
>
> How do these printf functions convert their constant string argument
> into something else? Is this done by camlp4?

this have nothing to do with camlp4. This a type thing, not a
lexical/syntaxical thing :

# Format.sprintf;;
- : ('a, unit, string) format -> 'a = <fun>

its why you cant use String.concat for concataining argument to
Format.sprintf. You should try to use Format.bprintf or Format.fprintf
with a formater coming from formatter_of_buffer.
--
Rémi Vanicat
vanicat@...
http://dept-info.labri.u-bordeaux.fr/~vanicat

#977 From: Stalkern 2 <stalkern2@...>
Date: Sun Apr 6, 2003 11:54 am
Subject: Re: "ocaml_beginners"::[] functional way of generating text
stalkern2
Send Email Send Email
 
Il  Sunday 06 April 2003 01:53, Remi Vanicat ha scritto:
> This is false. bug fix are still apply to camlp4, even after Daniel De
> Rauglaudre broke with the rest of the team... The only problem may be
> that as none know camlp4 as Daniel was, the bug might be fix latter,
> but camlp4 is still there, and will stay there. Several member of the
> ocaml team (even Xavier Leroy) have said that.

OK, that's good to hear. Who's the current mantainer BTW? Do you know whether
Camlp4 will be part of the next core distribution?

T I A
Ernesto

#978 From: "Nicolas Cannasse" <warplayer@...>
Date: Mon Apr 7, 2003 1:10 am
Subject: Re: "ocaml_beginners"::[] functional way of generating text
ncannasse
Send Email Send Email
 
> > This is false. bug fix are still apply to camlp4, even after Daniel De
> > Rauglaudre broke with the rest of the team... The only problem may be
> > that as none know camlp4 as Daniel was, the bug might be fix latter,
> > but camlp4 is still there, and will stay there. Several member of the
> > ocaml team (even Xavier Leroy) have said that.
>
> OK, that's good to hear. Who's the current mantainer BTW? Do you know
whether
> Camlp4 will be part of the next core distribution?

I'm pretty sure it will.

Nicolas Cannasse

#979 From: "DUBOIS Fabrice FTRD/DTL/LAN" <fabrice.dubois@...>
Date: Thu Apr 10, 2003 10:42 am
Subject: About Str.global_substitute
fabrice.dubois@...
Send Email Send Email
 
Hi all,

There's something I don't wonder about with that "Str.global_substitute"
function. Here's the signature:

val global_substitute : regexp -> (string -> string) -> string -> string

The documentation explains that the function takes as arguments:
- a regular expression ("regexp"),
- a function "subst",
- a string "s" on which to operate the replacements.

and returns the new string once replacements are done.

My concern is about the "subst" argument. According to the doc, "The function
subst is called once for each matching substring, and receives s (the whole
text) as argument."
Maybe I'm missing something, but I do not see the benefit of passing THE WHOLE
TEXT ("s") to that function.
I would see a clear advantage in passing it THE CURRENT MATCHED SUBSTRING
instead!

Concretely, what I could need to do is specified as follows:
- replace all substrings that match a number (ie. regexp = "[0123456789]+") by
the string "hello" BUT ONLY if the number is greater than 100...

The subst function could make the test and return either "hello" or the original
string. Do i have combine some other Str primitives to do such a thing, or is
there a more direct solution?

Thanks, best regards

Fabrice Dubois
France Télécom R&D/DTL/TAL
2 avenue Pierre Marzin
22307 Lannion cedex
Tel : +33 (0)2 96 05 19 29
Fax : +33 (0)2 96 05 39 45
<http://www.francetelecom.com/rd>

#980 From: "DUBOIS Fabrice FTRD/DTL/LAN" <fabrice.dubois@...>
Date: Thu Apr 10, 2003 10:44 am
Subject: RE: "ocaml_beginners"::[] About Str.global_substitute
fabrice.dubois@...
Send Email Send Email
 
Sorry, in my previous message, you must read "There's something I ___DO___
wonder about..." :-)

-----Message d'origine-----
De : DUBOIS Fabrice FTRD/DTL/LAN
Envoyé : jeudi 10 avril 2003 12:43
À : ocaml_beginners@yahoogroups.com
Objet : "ocaml_beginners"::[] About Str.global_substitute


Hi all,

There's something I don't wonder about with that "Str.global_substitute"
function. Here's the signature:

#981 From: Remi Vanicat <vanicat+egroups@...>
Date: Thu Apr 10, 2003 11:31 am
Subject: Re: "ocaml_beginners"::[] About Str.global_substitute
dl_ens
Send Email Send Email
 
"DUBOIS Fabrice FTRD/DTL/LAN" <fabrice.dubois@...> writes:

> Hi all,
>
> There's something I don't wonder about with that "Str.global_substitute"
function. Here's the signature:
>
> val global_substitute : regexp -> (string -> string) -> string -> string
>
> The documentation explains that the function takes as arguments:
> - a regular expression ("regexp"),
> - a function "subst",
> - a string "s" on which to operate the replacements.
>
> and returns the new string once replacements are done.
>
> My concern is about the "subst" argument. According to the doc, "The function
subst is called once for each matching substring, and receives s (the whole
text) as argument."
> Maybe I'm missing something, but I do not see the benefit of passing THE WHOLE
TEXT ("s") to that function.
> I would see a clear advantage in passing it THE CURRENT MATCHED
> SUBSTRING instead!

well, you can easily find it by using the functions matched_string,
match_beginning, match_end, matched_group, group_beginning, group_end.

[...]

--
Rémi Vanicat
vanicat@...
http://dept-info.labri.u-bordeaux.fr/~vanicat

#982 From: "Nicolas Cannasse" <warplayer@...>
Date: Fri Apr 11, 2003 1:47 am
Subject: Re: "ocaml_beginners"::[] About Str.global_substitute
ncannasse
Send Email Send Email
 
> Maybe I'm missing something, but I do not see the benefit of passing THE
WHOLE TEXT ("s") to that function.
> I would see a clear advantage in passing it THE CURRENT MATCHED SUBSTRING
instead!

Creating the substring has as cost, and for example sometimes you don't care
about which substring is matched ( comments deletion in a preprocessor for
example ). But you always can retreived it using Str.matched_string.

let your_global_subst r f = Str.global_substitute r (fun s -> f
(Str.matched_string s))

Nicolas Cannasse

#983 From: "Eray Ozkural" <erayo@...>
Date: Wed Apr 16, 2003 7:10 pm
Subject: Re: "ocaml_beginners"::[] Dynamic Programming
examachine
Send Email Send Email
 
--- In ocaml_beginners@yahoogroups.com, Manos Renieris <er@c...> wrote:
> As far as I can tell, there is no extra efficiency coming from the
> laziness. Instead, the author is showing how to achieve the same
> complexity in Haskell that you would easily get from the
> imperative features in a language like Ocaml.

Good point!!

For bonus points: if you can come up with a generic functor for a few
dynamic programming approaches (such as filling a 2d table, etc.) then it
would come really handy.

A really nice thing you can do in Haskell was using infinite (=arbitrarily
extensible...) data structures like infinite graphs to specify higher order
dynamic/greedy algorithms. With that I remember having written something
like a generic simulated annealing function.

It should be pretty interesting to see how general and flexible such a thing
could be in ocaml without sacrificing efficiency.

See you,

__
Eray

#984 From: "Walter C. Reel III" <blitzfeuer@...>
Date: Thu Apr 17, 2003 6:18 am
Subject: Reading to end of file
waltercreel
Send Email Send Email
 
I have a little learning program does simple processing on a report style
file.  I build a list using the 'input_line input_channel' in a recursive
function until an End_of_file is raised (the end condition).  Is there
another way of dertimining the end of the file without using the exception?

I come from the C++ world of avoiding to use exceptions as regular control
structures, does that translate to OCaml?

cheers,
  - Walt

#985 From: Didier Le Botlan <lebotlan@...>
Date: Thu Apr 17, 2003 8:56 am
Subject: Re: "ocaml_beginners"::[] Reading to end of file
lebotlan
Send Email Send Email
 
Walter C. Reel III wrote:
> I have a little learning program does simple processing on a report style
> file.  I build a list using the 'input_line input_channel' in a recursive
> function until an End_of_file is raised (the end condition).  Is there
> another way of dertimining the end of the file without using the exception?

Probably, but you WANT to use exceptions. (As far as I know, it is the Right
Way to read a file).

> I come from the C++ world of avoiding to use exceptions as regular control
> structures, does that translate to OCaml?

In OCaml you are welcome to use exceptions as much as you wish.
I would be interested to know why exceptions are so bad in C++.

#986 From: "DUBOIS Fabrice FTRD/DTL/LAN" <fabrice.dubois@...>
Date: Thu Apr 17, 2003 9:24 am
Subject: RE: "ocaml_beginners"::[] Reading to end of file
fabrice.dubois@...
Send Email Send Email
 
Yes, exception handling is really clean is OCaml.
Too often, exceptions are considered synonyms of "nasty error " or
"imminent-crash" indicators.
But in fact, what they litteraly mean is just "a case different from the
general case", no matter if it's a malfunction (faulty case) or just a
particular but NORMAL case.
Reaching the end of a file is a perfectly normal case :), but is
considered exceptional in that it happens only once.

Fabrice

-----Message d'origine-----
De : Didier Le Botlan [mailto:lebotlan@...]
Envoye : jeudi 17 avril 2003 10:57
A : ocaml_beginners@yahoogroups.com
Objet : Re: "ocaml_beginners"::[] Reading to end of file


Walter C. Reel III wrote:
> I have a little learning program does simple processing on a report
style
> file.  I build a list using the 'input_line input_channel' in a
recursive
> function until an End_of_file is raised (the end condition).  Is there
> another way of dertimining the end of the file without using the
exception?

Probably, but you WANT to use exceptions. (As far as I know, it is the
Right
Way to read a file).

> I come from the C++ world of avoiding to use exceptions as regular
control
> structures, does that translate to OCaml?

In OCaml you are welcome to use exceptions as much as you wish.
I would be interested to know why exceptions are so bad in C++.





To unsubscribe from this group, send an email to:
ocaml_beginners-unsubscribe@yahoogroups.com
Archives up to 11 October 2002 are also at
http://membres.lycos.fr/ipotesi/OCAML/ocaml-beginners_archive/index.html
The archives of the very official ocaml list (the seniors' one) can be
found at http://caml.inria.fr
Attachments are banned and you're asked to be polite, avoid flames etc.
etc.

Your use of Yahoo! Groups is subject to
http://docs.yahoo.com/info/terms/

#987 From: Stalkern 2 <stalkern2@...>
Date: Thu Apr 17, 2003 9:38 am
Subject: Re: "ocaml_beginners"::[] Reading to end of file
stalkern2
Send Email Send Email
 
Il  Thursday 17 April 2003 08:18, Walter C. Reel III ha scritto:
> <html><body>
>
>
> <tt>
> I have a little learning program does simple processing on a report
> style<BR> file.  I build a list using the 'input_line input_channel'
> in a recursive<BR> function until an End_of_file is raised (the end
> condition).  Is there<BR> another way of dertimining the end of the
> file without using the exception?<BR> <BR>
> I come from the C++ world of avoiding to use exceptions as regular
> control<BR> structures, does that translate to OCaml?<BR>

Use exceptions, but be aware both of the fact that try...with... constructs
break tail-recursiveness, and of the kind of incorrect nesting described
below (quoting from a post by David Brown in Caml-list, 9/4/2003).
--------------------------------------------------------------------------------\
--------------
A common mistake is to
add a try clause inside of a tail recursive call.  The call is no longer
tail-recursive, and makes a chain of exception handlers for each
invocation.  e.g.

    let rec loop () =
      try let line = input_line () in
      ...;
      loop ()
      with End_of_file -> ...

instead of

    let rec loop () =
      let line = input_line () in
      ...;
      loop ()
    in
    try loop ()
    with End_of_file -> ...
--------------------------------------------------------------------------------\
--------------

Ciao
Ernesto

#988 From: Eray Ozkural <erayo@...>
Date: Thu Apr 17, 2003 11:52 am
Subject: Re: "ocaml_beginners"::[] Reading to end of file
examachine
Send Email Send Email
 
On Thursday 17 April 2003 11:56, Didier Le Botlan wrote:
>
> In OCaml you are welcome to use exceptions as much as you wish.
> I would be interested to know why exceptions are so bad in C++.

severe performance penalty: each activation record gets code for unrolling
exceptions IIRC

--
Eray Ozkural (exa) <erayo@...>
Comp. Sci. Dept., Bilkent University, Ankara  KDE Project: http://www.kde.org
www: http://www.cs.bilkent.edu.tr/~erayo  Malfunction: http://mp3.com/ariza
GPG public key fingerprint: 360C 852F 88B0 A745 F31B  EA0F 7C07 AE16 874D 539C

#989 From: Eray Ozkural <erayo@...>
Date: Thu Apr 17, 2003 11:53 am
Subject: Re: "ocaml_beginners"::[] Reading to end of file
examachine
Send Email Send Email
 
On Thursday 17 April 2003 12:38, Stalkern 2 wrote:
> Use exceptions, but be aware both of the fact that try...with... constructs
> break tail-recursiveness, and of the kind of incorrect nesting described
> below (quoting from a post by David Brown in Caml-list, 9/4/2003).

For functional code right now I'm using haskell style
type 'a maybe = None | Just of 'a


--
Eray Ozkural (exa) <erayo@...>
Comp. Sci. Dept., Bilkent University, Ankara  KDE Project: http://www.kde.org
www: http://www.cs.bilkent.edu.tr/~erayo  Malfunction: http://mp3.com/ariza
GPG public key fingerprint: 360C 852F 88B0 A745 F31B  EA0F 7C07 AE16 874D 539C

#990 From: Stalkern 2 <stalkern2@...>
Date: Thu Apr 17, 2003 1:09 pm
Subject: Re: "ocaml_beginners"::[] Reading to end of file
stalkern2
Send Email Send Email
 
Il  Thursday 17 April 2003 13:53, Eray Ozkural ha scritto:
>  For functional code right now I'm using haskell style
>  type 'a maybe = None | Just of 'a

You know that's like 'a option in Ocaml, don't you

Ernesto

#991 From: "Walter C. Reel III" <blitzfeuer@...>
Date: Thu Apr 17, 2003 2:22 pm
Subject: RE: "ocaml_beginners"::[] Reading to end of file
waltercreel
Send Email Send Email
 
> In OCaml you are welcome to use exceptions as much as you wish.
> I would be interested to know why exceptions are so bad in C++.

There's a lot of overhead associated to C++ exception (so much as to cause
Trolltech Inc. to not use them in their Qt product, e.g.
the --no-g++-exceptions build options).  Scott Meyers even devotes an item
to proper use of exceptions in his higly acclaimed "Effective C++" series,
IIRC.

#992 From: "Walter C. Reel III" <blitzfeuer@...>
Date: Thu Apr 17, 2003 2:22 pm
Subject: RE: "ocaml_beginners"::[] Reading to end of file
waltercreel
Send Email Send Email
 
Wow... I'm certainly getting a lot of interesting input.  Thanks everyone!

What if the recursive function is constructing a return?  Like a 'val ::
mk_more_rec acc_list' would it still pay off to try to hoist the exception
handling to the entry level call?

I'm still trying to understand last call optimisations.  If I understand
correctly, it would be better to do something like:

     let rec foo state acc_list =
       ... (* get value for 'val' *)
       foo state (val :: acc_list)

than:

     let rec foo state acc_list =
       ... (* get value for 'val' *)
       val :: foo state acc_list

Is that right or am I off base?

cheers
  - Walt

p.s.  if the ordering is significant than the return should be reversed in
first version.  That seems to be the LISP way from the material I read.

Il  Thursday 17 April 2003 08:18, Walter C. Reel III ha scritto:
> <html><body>
>
>
> <tt>
> I have a little learning program does simple processing on a report
> style<BR> file.  I build a list using the 'input_line input_channel'
> in a recursive<BR> function until an End_of_file is raised (the end
> condition).  Is there<BR> another way of dertimining the end of the
> file without using the exception?<BR> <BR>
> I come from the C++ world of avoiding to use exceptions as regular
> control<BR> structures, does that translate to OCaml?<BR>

Use exceptions, but be aware both of the fact that try...with... constructs
break tail-recursiveness, and of the kind of incorrect nesting described
below (quoting from a post by David Brown in Caml-list, 9/4/2003).
----------------------------------------------------------------------------
------------------
A common mistake is to
add a try clause inside of a tail recursive call.  The call is no longer
tail-recursive, and makes a chain of exception handlers for each
invocation.  e.g.

    let rec loop () =
      try let line = input_line () in
      ...;
      loop ()
      with End_of_file -> ...

instead of

    let rec loop () =
      let line = input_line () in
      ...;
      loop ()
    in
    try loop ()
    with End_of_file -> ...
----------------------------------------------------------------------------
------------------

Ciao
Ernesto

Messages 963 - 992 of 13887   Oldest  |  < Older  |  Newer >  |  Newest
Add to My Yahoo!      XML What's This?

Copyright © 2010 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Guidelines NEW - Help