I realise this thread is about experimenting with AI coding in J, so
forgive me for
merely suggesting just another function for a shortest derived
palindrome formed
by prepending a string to a given string:
NB. Test palindrome-ness with a hook:
ispal =: -:|.
NB. Form palindrome given character string (or numeric vector) y
mdpal =: {{
y,~ |.y}.~ (>:@i:&1) ispal \ y
}}
mdpal 'abcd'
dcbabcd
mdpal 'aacecaaa'
aaacecaaa
Appending a string might be better:
mdpal 'abcbadefg'
gfedabcbadefg
mdpal&.|. 'abcbadefg' NB. simulate appending by working on reverse...
abcbadefgfedabcba
... though not in this case!
Just for fun,
Mike
PS - I am not a Robot !?
On 07/05/2023 10:30, Thomas McGuire wrote:
Forgive me for keeping my own thread alive.
This article at Hackernoon put together a comparison of Coding AI bots using
Leetcode questions as the stem:
https://hackernoon.com/how-ai-bots-code-comparing-bing-claude-co-pilot-gpt-4-and-bard
How AI Bots Code: Comparing Bing, Claude+, Co-Pilot, GPT-4 and Bard
hackernoon.com
Co-pilot was the easiest one for me to access so that’s why I chose it.
So following hackernoon’s lead here are the results on Leetcode question 214:
STEM:
NB. find the shortest palindrome for a string by adding letters to the front of
it
shortpal =: 3 : 0
Copilot code:
NB. find the shortest palindrome for a string by adding letters to the front of
it
shortpal =: 3 : 0
's'=.y
n=. #s
for_i. n do.
if. (s i.~ |. s) = s do.
return. s
end.
s=. (i{a.) , s
end.
)
Now the copilot code doesn’t work but the structure of this very un-J-like code
is workable. I made some changes and got it working:
Copilot code fixed to work:
shortpal1 =: 3 : 0
's'=.y
n=. #s
for_i. s do.
if. s -: |. s do.
return. s
end.
s=. (|.(_1-i_index){.s) , y
end.
)
the changes were
- fixed the palindrome test to exit the function.
- fixed the building of the palindrome alway starting from the original string,
catenating the reverse of the next increment of letters from the end of the
string.
My J way of handling this problem is as follows:
NB. fpal - filter for palindromes, return the palindrome else return
NB. empty atom if not a palindrome
fpal=: 0&$`]@.(] -: |.)
NB. mkpalindrome - make palindrome by catenating reversed suffixes
NB. filter out the palindromes, remove empty boxed
NB. elements. Then get the shortest one created which
NB. is the tail of the boxed list. unbox the answer
mkpalindrome =: {{>@{:@-.&(<'') <@fpal@(,&y)@|.\.y}}
Running this on the leetcode simple examples:
mkpalindrome 'abcd'
dcbabcd
mkpalindrome 'aacecaaa'
aaacecaaa
Interestingly, in my previous email ,Copilot was able to discern the fork for
average. In the palindrome case the fairly well known fork for palindrome
testing was not discovered by Copilot. Yet when I was developing my 'improved
'J example. I happened to type the following:
NB. test if string is a palindrome
Copilot responded:
pal =: 3 : 0
s=.y
s -: |. s
)
This is close to the fork implementation.
I won’t post the code, but by using the {{}} method of function calls Copilot
produced a child like attempt at a J one liner. When I used an open paren ‘('
to see if I could force a fork implementation it again gave a child like
response for a J fork. Neither of the answers were working code.
Copilot is supposed to be pair programming with an AI bot. The github site
indicates that in Python about 40% of the Copilot suggested Python code gets
used. In other languages it is about 20%-25%. In my limited use, I find that,
rather than peers. I am the Senior programmer and Copilot is the High School
intern just learning J. Still it’s a fun toy to play with when your programming.
The major question at this point is will these AI bots ever write good J code
(or any array language code for that matter) or is the terseness of an array
language too abstracted for AI to become proficient in it?
On May 5, 2023, at 12:07 AM, Thomas McGuire <tmcguir...@gmail.com> wrote:
GitHub Copilot works under Visual Studio Code
GitHub user tikkanz published an extension for VS Code to edit and run J.
Fairly easy to install.
VSCode then needs to have the Copilot extensions installed. It will ask you to
login into your account and then you are all set.
I typed in the following into a new .ijs file:
NB. average list of numbers in language-j
ave =:
copilot then finished the line with and offered an example run of the function,
so after I pressed the tab key I had the following:
NB. average list of numbers in language-j
ave =: +/ % #
ave 1 2 3 4 5
Now I know the fork for average is in about 20 different electronic documents.
Still I was surprised that Copilot worked for J at all and more surprised that
it picked up the fork paradigm from the language. I was expecting more of a
functional approach such as:
ave1 =: 3 : 0
(+/ y) % #y
)
It costs 100 bucks per year unless you are a student or educator (then it’s
free). I don’t actively program for a living so I don’t know how many
developers are actively using AI tools for coding. But from a beginner
perspective having a tool that produces relatively good J code with a prompt
and a function header would be a great way to come up to speed in the language
in a short period of time.
Is it enough just to have J code examples on GitHub or is there a more direct
way to curate the AI tool so that it produces better J code?
Throwing it out there to see if anyone else has played with this.
Tom McGuire
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm