(count-all str1 str2)

2011-07-19 Thread Tuba Lambanog
Thinking of operations on collections to replace iteration is
productive, but alas, I'm new to it. I'm looking for a way to count
the number of occurrences of each and every character in str1 that
occurs in str2, so that

(count-all "abc" "abracadabra")

will give

8

which is the count of characters in  "abacaaba".

Any suggestion? Thanks!
Tuba

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: (count-all str1 str2)

2011-07-19 Thread Meikel Brandmeyer
Hi,

how about this: (count (filter (set "abc") "abracadabra"))?

Sincerely
Meikel

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Re: (count-all str1 str2)

2011-07-19 Thread Tuba Lambanog
That works well. Thank you very much!
Tuba

On Jul 19, 1:47 am, Meikel Brandmeyer  wrote:
> Hi,
>
> how about this: (count (filter (set "abc") "abracadabra"))?
>
> Sincerely
> Meikel

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: (count-all str1 str2)

2011-07-20 Thread Payam Mahmoudian
Hi,

I am a newbie to clojure.

It's appreciated if you could help me to convert following C code into clojure:

float qTR(char *s1, char *s2)
{
intp,pl,q=0,r,s1l=strlen(s1),s2l=strlen(s2);

for (p=0;p wrote:
> That works well. Thank you very much!
> Tuba
>
> On Jul 19, 1:47 am, Meikel Brandmeyer  wrote:
>> Hi,
>>
>> how about this: (count (filter (set "abc") "abracadabra"))?
>>
>> Sincerely
>> Meikel
>
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clojure@googlegroups.com
> Note that posts from new members are moderated - please be patient with your
> first post.
> To unsubscribe from this group, send email to
> clojure+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en
>

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: (count-all str1 str2)

2011-07-20 Thread Baishampayan Ghose
On Wed, Jul 20, 2011 at 11:04 AM, Payam Mahmoudian
 wrote:
> It's appreciated if you could help me to convert following C code into 
> clojure:
>
> float qTR(char *s1, char *s2)
> {
>    int    p,pl,q=0,r,s1l=strlen(s1),s2l=strlen(s2);
>
>    for (p=0;p        for (pl=1;pl<=s1l-p;pl++)
>            for (r=0;r                if (!memcmp(s1+p,s2+r,pl)) q += pl;
>
>    
> returns1l*q)/(s1l*(s1l+1)*(s1l+2)/6.0))+((s2l*q)/(s2l*(s2l+1)*(s2l+2)/6.0)))/(s1l+s2l));
> }
>
> Actually, I have a performance issue O(n^3), so I want to know if I
> could use clojure concurrency feature in this case.

Welcome to Clojure! Ideally, you should have posted a new question
instead of replying to an existing post. It messes up threads in the
discussion, you know.

Coming to your problem, it'd be great if you explain the problem and
your proposed solution in English (or pseudo-code). It's hard for
someone to convert imperative code written in C to functional Clojure
by just looking at existing code. Approaches to solve the same problem
differ and understanding the problem instead of looking at code helps.

Besides, if the algorithm itself is O(N^3), I don't think concurrency
can help you. Parallelism might, but one can't say without (again,)
understanding the problem.

Regards,
BG

-- 
Baishampayan Ghose
b.ghose at gmail.com

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: (count-all str1 str2)

2011-07-20 Thread Ken Wesson
On Wed, Jul 20, 2011 at 7:32 AM, Baishampayan Ghose  wrote:
> On Wed, Jul 20, 2011 at 11:04 AM, Payam Mahmoudian
>  wrote:
>> It's appreciated if you could help me to convert following C code into 
>> clojure:
>>
>> float qTR(char *s1, char *s2)
>> {
>>    int    p,pl,q=0,r,s1l=strlen(s1),s2l=strlen(s2);
>>
>>    for (p=0;p>        for (pl=1;pl<=s1l-p;pl++)
>>            for (r=0;r>                if (!memcmp(s1+p,s2+r,pl)) q += pl;
>>
>>    
>> returns1l*q)/(s1l*(s1l+1)*(s1l+2)/6.0))+((s2l*q)/(s2l*(s2l+1)*(s2l+2)/6.0)))/(s1l+s2l));
>> }
>>
>> Actually, I have a performance issue O(n^3), so I want to know if I
>> could use clojure concurrency feature in this case.
>
> Welcome to Clojure! Ideally, you should have posted a new question
> instead of replying to an existing post. It messes up threads in the
> discussion, you know.
>
> Coming to your problem, it'd be great if you explain the problem and
> your proposed solution in English (or pseudo-code). It's hard for
> someone to convert imperative code written in C to functional Clojure
> by just looking at existing code.

At a glance, I can tell you that it's seeing if two strings s1 and s2
have any substring in common -- for example "zoboomafoo" and "foobar"
contain a common substring "foo". For each possible substring position
as indicated by an offset into each string and a length at least 1 and
not enough to run off the end of either string, it adds the length to
a running total q if the corresponding substrings *differ*. So q ends
up smaller the more and larger common substrings turn up.

After that, it does some complicated math with the strings' lengths and q. :)

The OP may want to read up in the Knuth-Morris-Pratt algorithm to
discover the substrings more efficiently. Then q can be computed from
what it would be if the strings had no single letter in common by
subtracting for each substring n+2(n-1)+3(n-2)+ ... + n(1) where n is
that substring's length. There's probably a more efficient way to
compute q, or even to compute the return value, though.

-- 
Protege: What is this seething mass of parentheses?!
Master: Your father's Lisp REPL. This is the language of a true
hacker. Not as clumsy or random as C++; a language for a more
civilized age.

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: (count-all str1 str2)

2011-07-20 Thread Ken Wesson
By the way, if I had to hazard a guess I'd say he's computing a
distance of some kind between the two strings, possibly the Hamming
distance, and possibly for a spellchecker or something similar. Though
spellcheckers generally do their comparisons on strings small enough
that O(n^3) asymptotic behavior shouldn't be a big issue ...

Certainly, the return value grows with q, which grows the less the two
strings have in common. That much is certain from looking at the code.

-- 
Protege: What is this seething mass of parentheses?!
Master: Your father's Lisp REPL. This is the language of a true
hacker. Not as clumsy or random as C++; a language for a more
civilized age.

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en