Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6174863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:49:26+00:00 2026-05-23T23:49:26+00:00

How can express this imperative function in a functional, array-based language like K (or

  • 0

How can express this imperative function in a functional, array-based language like K (or Q)?

In sloppy C++:

vector<int> x(10), y(10); // Assume these are initialized with some values.

// BTW, 4 is just a const -- it's part of the algorithm and is arbitrarily chosen.

vector<int> result1(x.size() - 4 + 1); // A place to hold a resulting array.
vector<int> result2(x.size() - 4 + 1); // A place to hold another resulting array.

// Here's the code I want to express functionally.
for (int i = 0; i <= x.size() - 4; i++) {
    int best = x[i + 0] - y[i + 0];
    int bad = best;
    int worst = best;
    for(int j = 0; j < 4; j++) {
        int tmp = x[i + j] - y[i + 0];
        bad = min(bad, tmp);
        if(tmp > best) {
            best = tmp;
            worst = bad;
        }
    }
    result1[i] = best
    result2[i] = worst
}

I would most like to see this in kdb and Q but other functional languages are welcome.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T23:49:26+00:00Added an answer on May 23, 2026 at 11:49 pm

    In Kona (an open-source K dialect):

    First, set some example values (using same as the Clojure solution):

    a:1+!8;b:8#0        / a is 1..8, b is eight 0s
    

    Then:

    {(|/x;&/x)}@+{4#y _ x}[a+b;]'!#a
    

    Where a and b are your x and y variables above. (K makes a special case for the variables x, y, and z.)

    To break that up a bit more:

    maxmin:{(|/x;&/x)}  / (max;min) pairs of x
    get4:{4#y _ x}      / next 4 from x, starting at y
                        / with <4 remaining, will repeat; doesn't matter for min or max
    / maxmin applied to flipped results of get4(a-b) at each index 0..(length a)-1
    maxmin@+get4[a-b;]'!#a
    
    / result
    (4 5 6 7 8 8 8 8
     1 2 3 4 5 6 7 8)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In VB.Net, I can declare a variable in a function as Static, like this:
In SQL you can express multiple aggregates in a single database query like this:
With Oracle (or DB2, HSQLDB), I can express things like this: MERGE INTO [target_table]
Can I deploy SQL Server Express with my desktop application just like builtin database?
Hopefully I can express this in a coherent and meaningful way: Two of our
I have an existing data model. I would like to express this data model
Can I express this in an XSD? For example: One element is a required
In winbase.h, DeviceIoControl function is defined like this. BOOL WINAPI DeviceIoControl( __in HANDLE hDevice,
This is a somewhat long question, but I hope I can express it clearly.
In SQLAlchemy, we can declare tables and their relations like this: user = Table(

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.