gpt4 book ai didi

j - 阅读 J 代码的最佳策略

转载 作者:行者123 更新时间:2023-12-04 01:37:18 26 4
gpt4 key购买 nike

我已经使用 J 几个月了,我发现阅读不熟悉的代码(例如,不是我自己写的)是该语言最具挑战性的方面之一,尤其是在默认情况下。过了一会儿,我想出了这个策略:

1)将代码段复制到word文档中

2)从(1)中取出每个运算符并将其放在单独的行上,以便它垂直读取

3) 将每个运算符替换为其在词汇页面中的口头描述

4)从J语法到英文语法做一个粗略的翻译

5)使用翻译来识别概念上相关的组件并用换行符将它们分开

6) 用简单的英文散文描述 (5) 中的每个组件应该做什么

7) 根据 (6) 写出整个程序应该做什么的描述

8) 解释为什么(1)中的代码可以说是代表(7)中的设计概念。

虽然我从这个过程中学到了很多东西,但我发现它相当艰巨和耗时——尤其是如果有人使用我以前从未遇到过的概念来设计他们的程序。所以我想知道:J 社区中的其他人有没有最喜欢的方法来找出晦涩的代码?如果是这样,这些方法的优点和缺点是什么?

编辑:

我需要分解的代码类型示例如下:

binconv =: +/@ ((|.@(2^i.@#@])) * ]) @ ((3&#.)^:_1)

我自己写了这个,所以我碰巧知道它需要一个数字输入,将它重新解释为一个三元数组,并将结果解释为一个以 2 为底的数字的表示,最多重复一次。 (例如,binconv 5 = (3^1)+2*(3^0) -> 1 2 -> (2^1)+2*(2^0) = 4。)但如果我没有偶然发现它任何先前的历史或文件,弄清楚这就是它所做的将是一项重要的练习。

最佳答案

只是想添加到Jordan's Answer :如果您没有打开框显示,您可以使用 5!:2 以这种方式明确地设置格式

   f =. <.@-:@#{/:~
5!:2 < 'f'
┌───────────────┬─┬──────┐
│┌─────────┬─┬─┐│{│┌──┬─┐│
││┌──┬─┬──┐│@│#││ ││/:│~││
│││<.│@│-:││ │ ││ │└──┴─┘│
││└──┴─┴──┘│ │ ││ │ │
│└─────────┴─┴─┘│ │ │
└───────────────┴─┴──────┘

还有一个树显示:
   5!:4 <'f'
┌─ <.
┌─ @ ─┴─ -:
┌─ @ ─┴─ #
──┼─ {
└─ ~ ─── /:

查看 5!: Representation 的词汇页面还有 9!: Global Parameters用于更改默认值。

此外,对于它的值(value),我自己阅读 J 的方法是手动重新键入表达式,从右到左构建它,并在我进行时查找碎片,并在需要时使用恒等函数来形成临时火车到。

例如:
   /:~ i.5
0 1 2 3 4
NB. That didn't tell me anything
/:~ 'hello'
ehllo
NB. Okay, so it sorts. Let's try it as a train:
[ { /:~ 'hello'
┌─────┐
│ehllo│
└─────┘
NB. Whoops. I meant a train:
([ { /:~) 'hello'
|domain error
| ([{/:~)'hello'
NB. Not helpful, but the dictionary says
NB. "{" ("From") wants a number on the left.
(0: { /:~) 'hello'
e
(1: { /:~) 'hello'
h
NB. Okay, it's selecting an item from the sorted list.
NB. So f is taking the ( <. @ -: @ # )th item, whatever that means...
<. -: # 'hello'
2
NB. ??!?....No idea. Let's look up the words in the dictionary.
NB. Okay, so it's the floor (<.) of half (-:) the length (#)
NB. So the whole phrase selects an item halfway through the list.
NB. Let's test to make sure.
f 'radar' NB. should return 'd'
d
NB. Yay!

附录:
   NB. just to be clear:
f 'drara' NB. should also return 'd' because it sorts first
d

关于j - 阅读 J 代码的最佳策略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2754164/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com