From: xy s <sxy62849013@yahoo.com.cn>
To: caml-list@inria.fr
Subject: [Caml-list] ask for help : how to increase coding efficiency of fun as below
Date: Tue, 15 May 2012 09:00:05 +0800 (CST) [thread overview]
Message-ID: <1337043605.43298.YahooMailClassic@web15601.mail.cnb.yahoo.com> (raw)
[-- Attachment #1: Type: text/plain, Size: 1832 bytes --]
We have defined types:
type position = {line : int; col : int };;
type 'a ast = {data : 'a; kids : 'a ast list; posf : position; post: position};;
And then we transform string to ast:
let construct_common_ast s=
let len = String.length s in
let rec aux i j=
if i>j then []
else (
let k = find_first s "(,)" i in
let str = trim (String.sub s i (k-i)) in
if k>j || s.[k]=')' then (
if str="" then []
else {data=str; kids=[]; posf=(calc_pos s i); post=(calc_pos s j)}::[]
)
else if s.[k]='(' then (
let l = next_matched s '(' ')' (k+1) in
let m = find_first_not s " \t\n\r" (l+1) in
if m<=j && s.[m]<>',' then
fail_ast (calc_pos s k) (calc_pos s l) "construct_common_ast: invalid input string"
else
{data=str; kids=aux (k+1) (l-1); posf=(calc_pos s i); post=(calc_pos s l)} :: aux (m+1) j
)
else {data=str; kids=[]; posf=(calc_pos s i); post=(calc_pos s k)} :: aux (k+1) j
)
in
let trees=aux 0 (len-1) in
List.hd trees;;
For construct_common_ast :
# construct_common_ast;;
- : string -> string ast = <fun>
# construct_common_ast "node (function, params(), returns())";;
- : string ast =
{data = "node";
kids =
[{data = "function"; kids = []; posf = {line = 1; col = 8};
post = {line = 1; col = 16}};
{data = "params"; kids = []; posf = {line = 1; col = 17};
post = {line = 1; col = 25}};
{data = "returns"; kids = []; posf = {line = 1; col = 27};
post = {line = 1; col = 36}}];
posf = {line = 1; col = 2}; post = {line = 1; col = 37}}.
But, it is intolerable to deal with long string. how to increase coding efficiency of this function?
Thank you anyway.
Best wishes!
[-- Attachment #2: Type: text/html, Size: 9030 bytes --]
next reply other threads:[~2012-05-15 1:00 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-05-15 1:00 xy s [this message]
2012-05-15 4:56 ` [Caml-list] " Hongbo Zhang
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1337043605.43298.YahooMailClassic@web15601.mail.cnb.yahoo.com \
--to=sxy62849013@yahoo.com.cn \
--cc=caml-list@inria.fr \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox