[Jprogramming] Feedback on beginner's code
robert therriault
bobtherriault at mac.com
Sun Mar 13 16:43:33 UTC 2016
Hi Adam,
I agree with Henry. That is impressive for a first program. To start with it works, which is a lot more than I can say for my first attempts!
The only adjustment that I came up with in addition to the use of -: instead of (*/)@(=&(...)) was getting rid of the ] because I like my verbs to do more work than that if I have a choice. Since you are going to /:~@:tolower the arguments anyway, I moved that in place of the ] to get this.
isAnagramOf=: -:&:(' ' -.~ /:~@:tolower)
'Nag a Ram ' isAnagramOf 'Anagram'
1
'ab' isAnagramOf 'aa'
0
I look forward to more of your ideas on the forum.
Cheers, bob
> On Mar 13, 2016, at 9:15 AM, Henry Rich <henryhrich at gmail.com> wrote:
>
> Wow, that's amazing for a first program. You'll go far with this language.
>
> There are easier ways to do what you did:
> (' ' & i.#])
> removes spaces. Easier is
> -.&' '
>
> (*/)@(=&(...))
> will cause trouble if the left and right arguments have different length, which is why you had to discard that case. The better way to check for identical arguments is
> -:
> which doesn't have that problem.
>
> Applying these changes leaves
>
> isAnagramOf =: -:&:(/:~@tolower)&:(-.&' ')
>
> which uses your ideas, and gives the same result except that '' isAnagramOf '' will be 1.
>
> Notice I used &: instead of & . & would work here, but don't use @ and & until you understand how they differ from @: and &: .
>
> Henry Rich
>
>
>
>
>
> ' ' & i.#])
> On 3/13/2016 11:21 AM, Adam Tornhill wrote:
>> Hi all,
>> I finally decided to try to understand this fascinating language. So far I've spent some evenings reading and experimenting with the language. J is fun. Real fun. I also like how J forces me to re-consider how I view programming. Of course, that also means I'm still at a complete beginner's stage, so I'd like to ask for some feedback and suggestions on my first J code. I'm sure there's a lot I can simplify in my code.
>> Here's the code I put together by digging around in the J Dictionary:
>> isAnagramOf=:(0:`((*/)@(=&((/:~@tolower)@(' ' & i.#])))))@.(=&(#@:(' ' & i.#])))
>>
>> Some examples:
>> 'Nag a Ram ' isAnagramOf 'Anagram'1
>> 'ab' isAnagramOf 'aa'0
>> All tips and hints on both the code and learning J in general would be much welcome.
>> Thanks!/Adam-- Homepage: www.adamtornhill.com Twitter: @AdamTornhill
>> Your Code as a Crime Scene: https://pragprog.com/book/atcrime/your-code-as-a-crime-sceneLisp for the Web: https://leanpub.com/lispwebPatterns in C: https://leanpub.com/patternsinc
>> ----------------------------------------------------------------------
>> For information about J forums see http://www.jsoftware.com/forums.htm
>
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
More information about the Programming
mailing list