caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] newbie questions
@ 2003-03-29  8:48 Dr.Dr.Ruediger M.Flaig
  2003-03-29  9:15 ` Basile STARYNKEVITCH
                   ` (3 more replies)
  0 siblings, 4 replies; 9+ messages in thread
From: Dr.Dr.Ruediger M.Flaig @ 2003-03-29  8:48 UTC (permalink / raw)
  To: caml-list

Good morning to all you bedouins out there :-) ,

as I am a neophyte to CAML, forgive me if my questions have been asked (and answered) a hundred times before...


1.: Is there any means of doing list-type pattern matching (style "| h::t -> ...") for recursion on strings? OK., I have realized that not having a decent string type is one of the reason why programs in Haskell or Erlang are much slower than in CAML, and resorted to a "roll-your-own" for dealing with this:

let ht x = (String.sub x 1 ((String.length x)-1)), String.make 1 (compl x.[0]);;

because I think that

let compl = function 'g' -> 'c' | 'c' -> 'g' | 'a' -> 't' | 't' -> 'a' | _ -> ' ';;
let rec complement = function 
    "" -> ""
  | dna -> let h, t = ht dna in (complement h) ^ t;;

is much smarter than the iterative version

let compl = function 'g' -> 'c' | 'c' -> 'g' | 'a' -> 't' | 't' -> 'a' | _ -> ' ';;
let complement dna =
  let cdna = ref "" in
    for i = 0 to String.length( dna )-1 do 
      let cha = String.get dna i in
	cdna :=  (String.make 1 (compl cha)) ^ !cdna 
    done;
    !cdna;;

(Yes! Confess guilty! Learned Pascal in 1986...)

but -- and this is my point -- there should certainly be some more elegant way of dealing with this than the horrible "ht" definition, shouldn't it?


2.: A question on language design: why do recursive functions have to be marked explicitly as such? My personal feeling is that this rather spoils the nifty inference system... from Algol to Java, procedural compilers require explicit type definitions but are able to work out for themselves if a routine is recursive, and Haskell also has both type inference and implicit recursion, so this should not be too difficult. Or has there anything escaped my attention?


3.: I have followed the thread about speed with considerable interest... my personal impression is that on a PPC processor, OCaml code is sometimes even faster than C code! (Does not hold true for dft, I admit.) This may have to do with the queer gcc supplied by Apple, which does not appear to make good use of the PPC's wealth of registers... hence I would like to have some output in legible form but the -S option does not seem to work properly. Does anybody know about this?


Yours sincerely,
   Ruediger




Dr. Dr. Ruediger Marcus Flaig
Institute for Immunology
University of Heidelberg
Im Neuenheimer Feld 305
D-69120 Heidelberg
<flaig@cirith-ungol.sanctacaris.net>
Tel. +49-172-7652946
Fax  +49-4075110-17171

_____________________________________________________________
Free eMail .... the way it should be....
http://www.hablas.com

_____________________________________________________________
Select your own custom email address for FREE! Get you@yourchoice.com w/No Ads, 6MB, POP & more! http://www.everyone.net/selectmail?campaign=tag

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Caml-list] newbie questions
  2003-03-29  8:48 [Caml-list] newbie questions Dr.Dr.Ruediger M.Flaig
@ 2003-03-29  9:15 ` Basile STARYNKEVITCH
  2003-03-29 11:04 ` Sylvain LE GALL
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 9+ messages in thread
From: Basile STARYNKEVITCH @ 2003-03-29  9:15 UTC (permalink / raw)
  To: flaig, flaig; +Cc: caml-list

>>>>> "Ruediger" == Dr Ruediger M Flaig <flaig@hablas.com> writes:

    Ruediger> Good morning to all you bedouins out there :-) , as I am
    Ruediger> a neophyte to CAML, forgive me if my questions have been
    Ruediger> asked (and answered) a hundred times before...

You might have checked in the mailing list archive before asking...

    Ruediger> 1.: Is there any means of doing list-type pattern
    Ruediger> matching (style "| h::t -> ...") for recursion on
    Ruediger> strings? [...]

unfortunately no. You cannot have pattern matching on strings.
But the Str module might help (it provides regular expressions).

See also recent postings on this issue:
http://caml.inria.fr/archives/200303/msg00289.html
http://caml.inria.fr/archives/200303/msg00399.html

    Ruediger> 2.: A question on language design: why do recursive
    Ruediger> functions have to be marked explicitly as such?  [...]

Any (syntactically non-recursive) *expression* can appear on the right
hand side of let bindings, but this is not true for let rec bindings
which are restriced (syntactically) to functions and constant
constructors

see http://caml.inria.fr/ocaml/htmlman/manual015.html for more

(Historical note; many other languages require syntactic marking of
recursions: Scheme, Fortran90, PL1, ...)


-- 

Basile STARYNKEVITCH         http://starynkevitch.net/Basile/ 
email: basile<at>starynkevitch<dot>net 
aliases: basile<at>tunes<dot>org = bstarynk<at>nerim<dot>net
8, rue de la Faïencerie, 92340 Bourg La Reine, France

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [Caml-list] newbie questions
  2003-03-29  8:48 [Caml-list] newbie questions Dr.Dr.Ruediger M.Flaig
  2003-03-29  9:15 ` Basile STARYNKEVITCH
@ 2003-03-29 11:04 ` Sylvain LE GALL
  2003-03-30  9:53 ` Damien Doligez
  2003-03-30 14:23 ` Wolfgang Lux
  3 siblings, 0 replies; 9+ messages in thread
From: Sylvain LE GALL @ 2003-03-29 11:04 UTC (permalink / raw)
  To: flaig; +Cc: caml-list

On Sat, Mar 29, 2003 at 12:48:39AM -0800, Dr.Dr.Ruediger M.Flaig wrote:
> Good morning to all you bedouins out there :-) ,
> 
> as I am a neophyte to CAML, forgive me if my questions have been asked (and answered) a hundred times before...
> 
> 
> 1.: Is there any means of doing list-type pattern matching (style "| h::t -> ...") for recursion on strings? OK., I have realized that not having a decent string type is one of the reason why programs in Haskell or Erlang are much slower than in CAML, and resorted to a "roll-your-own" for dealing with this:
> 
 
As i see your code, i think you maybe miss something in caml : typing
help you to program !
 If i have understood, you work on DNA code. In my mind DNA base type
 should be :

in dna.ml

 type dna = A | C | G | T

 ( and arn = A | C | G | U )

type gene = dna list

let ht x =
 	match x with
	A -> T
	| T -> A
	| G -> C
	| C -> G
	
(* give you the ht transform of you DNA list *)
let complement lst =
	List.iter ht lst 

If you want more pattern matching :

let ht_exp lst =
	match lst with
	A :: A :: A :: tl_lst -> T :: ( ht_exp tl_lst )
	| A :: x :: A :: tl_lst -> T :: (ht x) :: (ht_exp tl_lst)
	...usw

Iy you want to convert your string to dna type :
in parser_dna.mly
%token A
%token T
%token C
%token G
%token EOF
%start main
%type <Dna.gene> main
%%
main:
A main	 { Dna.A :: $2 }
|C main  { Dna.C :: $2 }
|T main  { Dna.T :: $2 }
|G main  { Dna.G :: $2 }
|EOF	 { [] }
;

in lexer_dna.mll
{
	open Parser_dna
}

rule token = parse
'a'	{ A }
| 't'   { T }
| 'c'   { C }
| 'g'   { G }
| eof   { EOF }


and then you can transform a string with
let lexbuf = Lexing.from_string "acgtacgt" 
in
let result = Parser_dna.main Lexer_dna.token lexbuf

and result will contain [ A ; C ; G ; T;  A; C; G; T ]

I am not sure that is what you want. But when you program in Ocaml, i
think it is better to use it as ocaml ( not trying to use techniques you
should have used in other languages ). I hope it will help you. 

I have just typed the code as it comes, it is surely full of bug.

Kind regard
Sylvain LE GALL

ps : i am surely not a genetic expert... there is maybe a lot of
biological non sense.

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [Caml-list] newbie questions
  2003-03-29  8:48 [Caml-list] newbie questions Dr.Dr.Ruediger M.Flaig
  2003-03-29  9:15 ` Basile STARYNKEVITCH
  2003-03-29 11:04 ` Sylvain LE GALL
@ 2003-03-30  9:53 ` Damien Doligez
  2003-03-30 14:23 ` Wolfgang Lux
  3 siblings, 0 replies; 9+ messages in thread
From: Damien Doligez @ 2003-03-30  9:53 UTC (permalink / raw)
  To: caml-list

On Saturday, March 29, 2003, at 09:48 AM, Dr.Dr.Ruediger M.Flaig wrote:

> let ht x = (String.sub x 1 ((String.length x)-1)), String.make 1 
> (compl x.[0]);;

Any program that uses this function is going to be horribly inefficient.
You should do something like this instead:

let string_map f s =
   let result = String.copy s in
   for i = 0 to (String.length s - 1) do
     result.[i] <- f s.[i];
   done;
   result
;;

let compl = function 'g' -> 'c' | 'c' -> 'g' | 'a' -> 't'
                    | 't' -> 'a' | _ -> ' ';;

let complement s = string_map compl s;;

This should be able to handle the complete human genome (a 3 gigabyte 
string)
without problems (on a 64-bit machine with enough memory).


> 2.: A question on language design: why do recursive functions have to 
> be marked explicitly as such? My personal feeling is that this rather 
> spoils the nifty inference system... from Algol to Java, procedural 
> compilers require explicit type definitions but are able to work out 
> for themselves if a routine is recursive,

All these language have different syntax for defining 
functions/procedures
and for defining values, and make functions implicitly recursive.  In 
effect,
you are "marking" your functions as recursive by defining them.  You can
get the same effect in O'Caml by always using "let rec" for your 
functions.

>  and Haskell also has both type inference and implicit recursion, so 
> this should not be too difficult. Or has there anything escaped my 
> attention?

Haskell is a lazy language, and has no problem with arbitrary recursive
values.  In a strict language like O'Caml, we cannot make all values
recursive by default.


> hence I would like to have some output in legible form but the -S 
> option does not seem to work properly. Does anybody know about this?

Do you mean gcc -S or ocamlopt -S ?  They both work on my MacOS X.

-- Damien

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [Caml-list] newbie questions
  2003-03-29  8:48 [Caml-list] newbie questions Dr.Dr.Ruediger M.Flaig
                   ` (2 preceding siblings ...)
  2003-03-30  9:53 ` Damien Doligez
@ 2003-03-30 14:23 ` Wolfgang Lux
  3 siblings, 0 replies; 9+ messages in thread
From: Wolfgang Lux @ 2003-03-30 14:23 UTC (permalink / raw)
  To: flaig; +Cc: caml-list


Ruediger M.Flaig wrote:

> 1.: Is there any means of doing list-type pattern matching (style "| 
> h::t -> ...") for recursion on strings? OK., I have realized that not 
> having a decent string type is one of the reason why programs in 
> Haskell or Erlang are much slower than in CAML, and resorted to a 
> "roll-your-own" for dealing with this:
>
> let ht x = (String.sub x 1 ((String.length x)-1)), String.make 1 
> (compl x.[0]);;
>
> because I think that
>
> let compl = function 'g' -> 'c' | 'c' -> 'g' | 'a' -> 't' | 't' -> 'a' 
> | _ -> ' ';;
> let rec complement = function
>     "" -> ""
>   | dna -> let h, t = ht dna in (complement h) ^ t;;
>
> is much smarter than the iterative version
>
> let compl = function 'g' -> 'c' | 'c' -> 'g' | 'a' -> 't' | 't' -> 'a' 
> | _ -> ' ';;
> let complement dna =
>   let cdna = ref "" in
>     for i = 0 to String.length( dna )-1 do
>       let cha = String.get dna i in
> 	cdna :=  (String.make 1 (compl cha)) ^ !cdna
>     done;
>     !cdna;;
>
> (Yes! Confess guilty! Learned Pascal in 1986...)
>
> but -- and this is my point -- there should certainly be some more 
> elegant way of dealing with this than the horrible "ht" definition, 
> shouldn't it?

Actually, in many cases you would not want to implement such recursions 
yourself.
Many recursion patterns can implemented by using the standard higher 
order functions
unit, map, fold, etc. For instance, your complement function should be 
as simple as

   let complement dna = String.map compl dna

To my surprise, the String module doesn't provide a map function (it 
does
implement iter, though).

If you were using an algebraic data type

   type Base = A | C | G | T

you could make use of arrays and the implement complement using 
Array.map.

Wolfgang

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [Caml-list] newbie questions
  2001-12-11  0:25 Jose A. Ortega Ruiz
  2001-12-11  2:32 ` Mike Leary
@ 2001-12-11 10:23 ` Sven
  1 sibling, 0 replies; 9+ messages in thread
From: Sven @ 2001-12-11 10:23 UTC (permalink / raw)
  To: Jose A. Ortega Ruiz; +Cc: caml-list

On Tue, Dec 11, 2001 at 01:25:48AM +0100, Jose A. Ortega Ruiz wrote:
> 
> hi,
> 
> i'm learning OCaml (coming from a professional C/C++, Java background,
> with a little bit of elisp and scheme (read SICP)), and own a copy of
> Cousineau & Mauny's book (O'Reilly's Objective Caml is on the way). i
> would really appreciate your advice on the following topics:
> 
> - C&M uses Caml (light?): is it worth reading while learning OCaml? do
>   you know of any comparison of the two languages i should read if
>   using C&M?

Ocaml is based on caml light, there are some syntactic changes, and some
additional stuff (like the object system and the modules).

If you can make abstraction of this synctactic changes, most of the C&M book
should apply as well to ocaml. Since it shows many functional programming
tricks, i gues syou could as well use it with other functional languages as
sml or even lisp based ones :)))

> - my ultimate goal is using OCaml for real, useful projects: any idea
>   about specially needed/useful applications in OCaml i could help on?

There surelly are many things you can help on, myself, i am planning a small
work in a landscape building sysytem, a bit like terraform, but in ocaml.

I don't really have that many time for it though, write to me if you are
interrested.

Friendly,

Sven Luther
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [Caml-list] newbie questions
  2001-12-11  0:25 Jose A. Ortega Ruiz
@ 2001-12-11  2:32 ` Mike Leary
  2001-12-11 10:23 ` Sven
  1 sibling, 0 replies; 9+ messages in thread
From: Mike Leary @ 2001-12-11  2:32 UTC (permalink / raw)
  To: Jose A. Ortega Ruiz; +Cc: caml-list

a number of people liked the notes for this course:
http://www.cs.caltech.edu/cs134/cs134b/

On Tue, Dec 11, 2001 at 01:25:48AM +0100, Jose A. Ortega Ruiz wrote:
> 
> hi,
> 
> i'm learning OCaml (coming from a professional C/C++, Java background,
> with a little bit of elisp and scheme (read SICP)), and own a copy of
> Cousineau & Mauny's book (O'Reilly's Objective Caml is on the way). i
> would really appreciate your advice on the following topics:
> 
> - C&M uses Caml (light?): is it worth reading while learning OCaml? do
>   you know of any comparison of the two languages i should read if
>   using C&M?
> 
> - my ultimate goal is using OCaml for real, useful projects: any idea
>   about specially needed/useful applications in OCaml i could help on?
> 
> - any suggestion/recommendation/roadmap for my journey learning OCaml?
> 
> as i said before, your comments would be highly appreciated. 
> 
> thanks,
> 
> -- 
> jao
> -------------------
> Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
> To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr

-- 
"You mean, I'm the bad guy?"  --Bill Foster, aka D-FENS
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Caml-list] newbie questions
@ 2001-12-11  0:25 Jose A. Ortega Ruiz
  2001-12-11  2:32 ` Mike Leary
  2001-12-11 10:23 ` Sven
  0 siblings, 2 replies; 9+ messages in thread
From: Jose A. Ortega Ruiz @ 2001-12-11  0:25 UTC (permalink / raw)
  To: caml-list


hi,

i'm learning OCaml (coming from a professional C/C++, Java background,
with a little bit of elisp and scheme (read SICP)), and own a copy of
Cousineau & Mauny's book (O'Reilly's Objective Caml is on the way). i
would really appreciate your advice on the following topics:

- C&M uses Caml (light?): is it worth reading while learning OCaml? do
  you know of any comparison of the two languages i should read if
  using C&M?

- my ultimate goal is using OCaml for real, useful projects: any idea
  about specially needed/useful applications in OCaml i could help on?

- any suggestion/recommendation/roadmap for my journey learning OCaml?

as i said before, your comments would be highly appreciated. 

thanks,

-- 
jao
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Caml-list] newbie questions
@ 2001-08-22 17:58 Collin Monahan
  0 siblings, 0 replies; 9+ messages in thread
From: Collin Monahan @ 2001-08-22 17:58 UTC (permalink / raw)
  To: caml-list

[-- Attachment #1: Type: text/plain, Size: 267 bytes --]

 
Hi,
 
I'm new to the list, and to Caml and to ML in general. I'm having stupid
problems one after the other trying to learn this, and was wondering if
anyone had some extra time to field questions, without my bothering the
whole list.
 
Thank you,
Collin Monahan
 

[-- Attachment #2: Type: text/html, Size: 4278 bytes --]

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2003-03-30 14:23 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-03-29  8:48 [Caml-list] newbie questions Dr.Dr.Ruediger M.Flaig
2003-03-29  9:15 ` Basile STARYNKEVITCH
2003-03-29 11:04 ` Sylvain LE GALL
2003-03-30  9:53 ` Damien Doligez
2003-03-30 14:23 ` Wolfgang Lux
  -- strict thread matches above, loose matches on Subject: below --
2001-12-11  0:25 Jose A. Ortega Ruiz
2001-12-11  2:32 ` Mike Leary
2001-12-11 10:23 ` Sven
2001-08-22 17:58 Collin Monahan

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).