From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail4-relais-sop.national.inria.fr (mail4-relais-sop.national.inria.fr [192.134.164.105]) by walapai.inria.fr (8.13.6/8.13.6) with ESMTP id q4F109SH012604 for ; Tue, 15 May 2012 03:00:11 +0200 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AjoKAFKqsU9qCpf3Ymdsb2JhbAA3DYIbK7FIFwgMBxUogkMZAQI3ARSJOgEDCplwjw6Cc4UFAiMnAolXAQYKAQGRDgSIYo0ihW4tjRQ X-IronPort-AV: E=Sophos;i="4.75,590,1330902000"; d="scan'208";a="143808391" Received: from nm34-vm8.bullet.mail.sg3.yahoo.com ([106.10.151.247]) by mail4-smtp-sop.national.inria.fr with SMTP; 15 May 2012 03:00:09 +0200 Received: from [106.10.166.118] by nm34.bullet.mail.sg3.yahoo.com with NNFMP; 15 May 2012 01:00:06 -0000 Received: from [106.10.151.171] by tm7.bullet.mail.sg3.yahoo.com with NNFMP; 15 May 2012 01:00:06 -0000 Received: from [127.0.0.1] by omp1011.mail.sg3.yahoo.com with NNFMP; 15 May 2012 01:00:06 -0000 X-Yahoo-Newman-Property: ymail-3 X-Yahoo-Newman-Id: 781201.92524.bm@omp1011.mail.sg3.yahoo.com Received: (qmail 43919 invoked by uid 60001); 15 May 2012 01:00:06 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com.cn; s=s1024; t=1337043605; bh=0alZJSHeLqzo8KLLyXbDemvTdi/LVGvw3LNKP1bdTVg=; h=X-YMail-OSG:Received:X-Mailer:Message-ID:Date:From:Subject:To:MIME-Version:Content-Type; b=5xMWSpCrZZ/WiesiusQN40pr8pEcD1iYb+Xe/w0gJu+UqHOkKNwN70ljMIvvbmDPk03KFeJZ2fkWGC8l5ItVyQ+istjMTFLiLZ3ey1KPPazm2G75MW7pUJHpSKpxqeRGMRjgAvUiK+xOoLjQ+OhDIgIa26hne1h/KM+vRKihqAI= DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.com.cn; h=X-YMail-OSG:Received:X-Mailer:Message-ID:Date:From:Subject:To:MIME-Version:Content-Type; b=Xkxf+sJcMOLrVxR3V1l7ny0LS/xbRwJaMe8Q58Orfx+CXyQ8L/H/4OaoRAWXXD/dK1uzar2qVKhK6x7Jt7+L9c2lBqQI0Od8o7wXIvZ/+9YemlGv0AckTSuCBqvJ4GLkM7ZRG5i0Pb+3IdqFaE61jtAenEmLpZ5LCPIPIS6vI7I=; X-YMail-OSG: P77o704VM1kS07N4ON4Z1hs3gbeg1BShQlQxPGzxC3Zf1On 7MiS3OQ01AzcvatDNpHMRFZUK1auSysL5hAY7wyymvDqs015zKsJlMQN8nO. 33JaOT6kAUL5Pz7NwhP0IzgubIAqtD4kCoNpTDEiZ_OJFxOfEDWLfeikxxuv 0YQYlRSxSB5x.KDBOyOhjDVLOwNChbXFNYZ1ejn0VZQbKMDlOCRKEQjZu6i_ QCG1.L5B08Q.3JNMrJ8mMeU8yscTQgP6_svwlkrPqqkwTrL65N_zcykNiIGV xHMBEbfOQ.sv.f3Hm5c2EtdcfE.5.nATuC4BiCdJd.r_HLz4xqgTVq6_jt1q TAbuUj6WUz.sSZpzkqMoT7CikMv1Ox8QKf1LRK9lJfhFnEi5Q0uIMvM8isVH 5cB4SQBTvm74brA858siDG6dBmDSTwcG3OKfp_Bk- Received: from [218.240.37.226] by web15601.mail.cnb.yahoo.com via HTTP; Tue, 15 May 2012 09:00:05 CST X-Mailer: YahooMailClassic/15.0.6 YahooMailWebService/0.8.118.349524 Message-ID: <1337043605.43298.YahooMailClassic@web15601.mail.cnb.yahoo.com> Date: Tue, 15 May 2012 09:00:05 +0800 (CST) From: xy s To: caml-list@inria.fr MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="940083559-2007529960-1337043605=:43298" Subject: [Caml-list] ask for help : how to increase coding efficiency of fun as below --940083559-2007529960-1337043605=:43298 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: quoted-printable We have defined types: =A0 type position =3D {line : int; col : int };; type 'a ast =3D {data : 'a; kids : 'a ast list; posf : position; post: posi= tion};; =A0 =A0 And then we transform string to ast: =A0 let construct_common_ast s=3D =A0=A0=A0 let len =3D String.length s in =A0=A0=A0 let rec aux i j=3D =A0=A0=A0=A0=A0 if i>j then [] =A0=A0=A0=A0=A0 else ( =A0=A0=A0=A0=A0=A0=A0 let k =3D find_first s "(,)" i in =A0=A0=A0=A0=A0=A0=A0 let str =3D trim (String.sub s i (k-i)) in =A0=A0=A0=A0=A0=A0=A0 if k>j || s.[k]=3D')' then ( =A0=A0=A0=A0=A0=A0=A0=A0=A0 if str=3D"" then [] =A0=A0=A0=A0=A0=A0=A0=A0=A0 else {data=3Dstr; kids=3D[]; posf=3D(calc_pos s= i); post=3D(calc_pos s j)}::[] =A0=A0=A0=A0=A0=A0=A0 ) =A0=A0=A0=A0=A0=A0=A0 else if s.[k]=3D'(' then ( =A0=A0=A0=A0=A0=A0=A0=A0=A0 let l =3D next_matched s '(' ')' (k+1) in =A0=A0=A0=A0=A0=A0=A0=A0=A0 let m =3D find_first_not s " \t\n\r" (l+1) in =A0=A0=A0=A0=A0=A0=A0=A0=A0 if m<=3Dj && s.[m]<>',' then=20 =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 fail_ast (calc_pos s k) (calc_pos s= l) "construct_common_ast: invalid input string" =A0=A0=A0=A0=A0=A0=A0=A0=A0 else =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 {data=3Dstr; kids=3Daux (k+1) (l-1)= ; posf=3D(calc_pos s i); post=3D(calc_pos s l)} :: aux (m+1) j =A0=A0=A0=A0=A0=A0=A0 ) =A0=A0=A0=A0=A0=A0=A0 else {data=3Dstr; kids=3D[]; posf=3D(calc_pos s i); p= ost=3D(calc_pos s k)} :: aux (k+1) j =A0=A0=A0=A0=A0 ) =A0=A0=A0 in =A0=A0=A0 let trees=3Daux 0 (len-1) in List.hd trees;; =A0 For construct_common_ast :=20 # construct_common_ast;; - : string -> string ast =3D # construct_common_ast "node (function, params(), returns())";; - : string ast =3D {data =3D "node"; =A0kids =3D =A0 [{data =3D "function"; kids =3D []; posf =3D {line =3D 1; col =3D 8}; =A0=A0=A0 post =3D {line =3D 1; col =3D 16}}; =A0=A0 {data =3D "params"; kids =3D []; posf =3D {line =3D 1; col =3D 17}; =A0=A0=A0 post =3D {line =3D 1; col =3D 25}}; =A0=A0 {data =3D "returns"; kids =3D []; posf =3D {line =3D 1; col =3D 27}; =A0=A0 =A0post =3D {line =3D 1; col =3D 36}}]; =A0posf =3D {line =3D 1; col =3D 2}; post =3D {line =3D 1; col =3D 37}}. =A0 But, it is intolerable to deal with long string. how to increase coding eff= iciency of this function? =A0 Thank you anyway.=20 Best wishes!= --940083559-2007529960-1337043605=:43298 Content-Type: text/html; charset=iso-8859-1 Content-Transfer-Encoding: quoted-printable

We have defined type= s:

=  

type position =3D {line : int; col : int };;

type 'a ast =3D {data : 'a; kids : 'a ast list; po= sf : position; post: position};;

=  

=  

And then we transform string to ast:=

=  

let construct_common_ast s=3D

    let len =3D String.length s in<= /FONT>

    let rec aux i j=3D

      if i>j then []

      else (

        let k = =3D find_first s "(,)" i in

        let str= =3D trim (String.sub s i (k-i)) in

        if k>= ;j || s.[k]=3D')' then (

        &n= bsp; if str=3D"" then []

        &n= bsp; else {data=3Dstr; kids=3D[]; posf=3D(calc_pos s i); post=3D(calc_pos s= j)}::[]

        )

        else if= s.[k]=3D'(' then (

        &n= bsp; let l =3D next_matched s '(' ')' (k+1) in

        &n= bsp; let m =3D find_first_not s " \t\n\r" (l+1) in

        &n= bsp; if m<=3Dj && s.[m]<>',' then

        &n= bsp;     fail_ast (calc_pos s k) (calc_pos s l) "constr= uct_common_ast: invalid input string"

        &n= bsp; else

        &n= bsp;     {data=3Dstr; kids=3Daux (k+1) (l-1); posf=3D(c= alc_pos s i); post=3D(calc_pos s l)} :: aux (m+1) j

        )

        else {d= ata=3Dstr; kids=3D[]; posf=3D(calc_pos s i); post=3D(calc_pos s k)} :: aux = (k+1) j

      )

    in

    let trees=3Daux 0 (len-1) in

List.hd trees;;

 =

For construct_common_ast :

# construct_common_ast;;=

- : string -> string ast =3D= <fun>

# construct_common_ast "node (f= unction, params(), returns())";;

- : string ast =3D

{data =3D "node";=

 kids =3D

  [{data =3D "function"; k= ids =3D []; posf =3D {line =3D 1; col =3D 8};

    post =3D {li= ne =3D 1; col =3D 16}};

   {data =3D "params"= ; kids =3D []; posf =3D {line =3D 1; col =3D 17};

    post =3D {li= ne =3D 1; col =3D 25}};

   {data =3D "returns= "; kids =3D []; posf =3D {line =3D 1; col =3D 27};

    post =3D {li= ne =3D 1; col =3D 36}}];

 posf =3D {line =3D 1; col= =3D 2}; post =3D {line =3D 1; col =3D 37}}.

=  

But, it is intolerable to deal with long string. h= ow to increase coding efficiency of this function?

=  

Thank you anyway.

Best wishes!

= --940083559-2007529960-1337043605=:43298--