简体   繁体   中英

Write 4 : 'x&{.&.;: y' tacitly

Conor Hoekstra recently solved a Leetcode problem in APL https://youtu.be/QtvvQ7MdwKY The problem is to take the first x words from a character string y

In J, using &. (Under) and ;: (Words) I can come up with a nice explicit one liner

solve =. 4 : 'x&{.&.;: y'         NB. Box words in y -> take first x-> unbox words in result while retaining spaces between
   s=. 'Hello how are you Contestant'
   4 solve s
Hello how are you

The trouble I am having is finding the tacit version that still includes &. , mainly because I believe x needs to be bound to {. during the creation of the verb. This is also an example where the magic 13: conversion is not helpful

   13 : 'x&{.&.;: y'
4 : 'x&{.&.;: y'

I can solve it tacitly by using ;:^:_1 to create the inverse of ;:

   solve2=. (;:^:_1)@:({. ;:)
   4 solve2 s
Hello how are you

But that is not nearly as pretty as the tacit version of 4: 'x&{.&.;: y' could be.
Anyone have a pretty tacit solution for 4: 'x&{.&.;: y' ?

With semiduals (introduced with J902 or J903?) you can write 4 {.&.(a:`;:) s . Then ;: gets only applied to the right argument, while still doing the inverse later. u&.(f`a:) would apply f only on the left argument.

mainly because I believe x needs to be bound to {. during the creation of the verb.

If creating a verb with an input is the key, shouldn't you use an adverb?

   solve =: 1 : 'm&{.&.;:'
   4 solve
4&{.&.;:
   (4 solve ,: 2 solve) 'Hello how are you Contestant'
Hello how are you
Hello how        

solve itself isn't tacit (or a verb), but 4 solve is tacit verb.

I don't think you can write this as a tacit verb, but it's easy to do as a tacit adverb: ]:&{.&.;: .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM