What does the following snippet do?

    I have defined : 



    n:25000;



    g:{1+(+\x)bin?y}



    what does this line do?


"acgtBDHKMNRSVWY"g[.27 .12 .12 .27,10#.02]3*n;

and this one:



    "actg"g[.3029549426680 .1979883004921 .1975473066391]5*n



    



I'm new to k and q, so please excuse this beginner-level query.



Thanks,

Kumar

I assume you took that from here: http://kparc.com/z/comp.k in which case note that this is k code

It’s a DNA sequence generator as per http://benchmarksgame.alioth.debian.org/u64q/fasta-description.html#fasta

Although I can’t seem to get the code to work (something missing there??) in essence it is giving those letters a non-uniform distribution, i.e. “a” is getting 27% of the distribution, “c” is getting the next 12%, etc etc.

The g function does a binary lookup into the distribution and the result is used to index the letters. A simpler example would be:

{“abcd”@1+ 2 9 50 bin x}0 100 12 3 1 8 5
“adcbabb”

(Above code will work in q and k)
 
i.e. any inputs below 2 return “a”, any inputs between 2 and 8 inclusive return “b”, any inputs between 9 and 49 inclusive return “c”, and any inputs greater than or equal to 50 return “d”.

If run correctly the code would output something like output TWO and THREE here: http://benchmarksgame.alioth.debian.org/download/fasta-output.txt

 
Terry

Yes, Terry, I got the code from the kparc site. I figured this is what is happening :

Taking a simpler example : g:{x+y}, 

Then g[1 2 3 4]10 gives 11 12 13 14, meaning x=[1 2 3 4] and y = 10

So for g:{1+(+\x)bin?y}, the x is [.27 .12 .12 .27,10#0.02], from the code above, for the first usage of g and y is 3*n, although I get a type’ error, for the bin?y part inside g. Yet to figure that one out.

I also found, that another way to write the same call would be "acgtBDHKMNRSVWY"g[.27 .12 .12 .27,10#.02;3*n] - which makes the idea of x and y as input more intuitive. And the final result is an indexing into the string to the left of g.

Exploring k alongside q is sincerely gratifying, to say the least. And addictive. 

Thanks , Terry. 

Kumar

Hi Krishna,

n:20
g:{1+((+)x) bin (y?1.)}

"acgtBDHKMNRSVWY"g[.27 .12 .12 .27,10#.02]3*n

This should work in q.  

On Thursday, March 17, 2016 at 8:08:42 PM UTC+1, TerryLynch wrote:

Although I can’t seem to get the code to work (something missing there??) in essence it is giving those letters a non-uniform distribution, i.e. “a” is getting 27% of the distribution, “c” is getting the next 12%, etc etc.

kparc uses a new version of k that is not (yet?) publicly available.