记者:自引用冒泡排序默契实施 [英] J: Self-reference in bubble sort tacit implementation

查看:195
本文介绍了记者:自引用冒泡排序默契实施的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于我初学者为J,我决定来解决使用这种语言的一个简单的任务,特别是实现冒泡排序算法。我知道这不是习惯用法来解决这样那样的问题,在功能性语言,因为它的自然解决使用数组元素换位命令式语言,如C,而不是构建在声明性语言修改列表。然而,这是code我已经写了:

(((小于./@(2及{)),$:@((大于./@(2及{)),2安培;}。 ))^(1&其中;#))^:#
 

下面的语句的结构:

<$p$p><$c$c>┌────────────────────────────────────────────────────────────────────────────┬──┬─┐ │┌───────────────────────────────────────────────────────────────┬──┬───────┐│^:│#│ ││┌───────────────────┬─┬───────────────────────────────────────┐│^:│┌─┬─┬─┐││ ││ │││┌──────┬─┬────────┐│,│┌──┬─┬────────────────────────────────┐││ ││1│&LT;│#│││││ ││││┌──┬─┐│@│┌─┬─┬──┐││ ││$:│@│┌───────────────────┬─┬────────┐│││ │└─┴─┴─┘││││ │││││&LT;.│/││││2│和放大器;│{.││││││ ││┌──────┬─┬────────┐│,│┌─┬─┬──┐││││ │││││ ││││└──┴─┘││└─┴─┴──┘││││││││┌──┬─┐│@│┌─┬─┬──┐││││2 │及放大器;│}.││││││││││ │││└──────┴─┴────────┘││││││││&GT;.│/││││2│和放大器;│{.││││ └─┴─┴──┘│││││││││ ││││││││││└──┴─┘││└─┴─┴──┘││││││││││││ │││││││││└──────┴─┴────────┘│││││││││││ │││││││ │└───────────────────┴─┴────────┘│││ │││││ ││││ │└──┴─┴────────────────────────────────┘││ │││││ ││└───────────────────┴─┴───────────────────────────────────────┘│ │││││ │└───────────────────────────────────────────────────────────────┴──┴───────┘│ ││ └────────────────────────────────────────────────────────────────────────────┴──┴─┘

让我们把它应用到一个数组:

(((小于./@(2及{)),$:@((大于./@(2及{)),2安培;}。 ))^:(1&LT;#))^:#5 3 8 7 2
2 3 5 7 8
 

这是混淆了我的事情是 $ 指的是最外面的括号内的语句。 帮助说:

  

$ 表示最长的动词   包含它。

的另一本书(〜300昆明植物研究所)说:

  3 + 4
7
    5 * 20
100
 

  

像+和符号*为的以及的和   在上述短语的的被称为   动词和再present功能。你可以   有一个以上的动词在第j短语   在这种情况下它等构成   一句简单的英语通过阅读   从左至右下,也就是    4 + 6%2 办法 4 添加到任何如下,即 6 2 分。

让我们重写我的code段省略最外面的() S:

 ((小于./@(2及{)),$:。@((&GT; ./@(2及{)),2及})。 )^:(1&LT;#)^:#5 3 8 7 2
2 3 5 7 8
 

Reuslts是相同的。我无法解释自己为什么这个作品,为什么只有((小于./@(2及{)),$:。@((&GT; ./@(2及{)) ,2安培;}))^:(1&LT;#)被视为最长的动词 $ 而不是整个前pression ((小于./@(2及{)),$:@((大于./@(2及{)),2及})。。)^:( 1&LT;#)^:#而不仅仅是(小于./@(2及{)),$:@((&GT; ./@(2及),因为如果((小于./@(2及{)),$ {)),2和。}。@((&GT; ./ @(2及{)),2安培;}))^:(1&LT;#)是一个动词,它也应该形成一道又一个动词与,我。即人们可能把整个句子(首段)作为动词。也许有一些限制的动词长度的限制由一个结合。

请看下面的code(从这里

 因子=:(*阶乘@&LT; :) ^:(1安培;≤)
    阶乘4
24
 

因子中的前pression指的是整体功能,我。即(*阶乘@&LT; :) ^:(1安培;≤)

下面我用,而不是一个函数名这个例子 $

 冒泡=(((小于./@(2及{)),冒泡@((&GT; ./@(2及{)),2及。 }))^:(1&其中;#))^:#
    冒泡5 3 8 7 2
2 3 5 7 8
 

我预计冒泡引用的整体功能,但它似乎并没有真正适合我,因为结果是正确的。

另外,我想看看其他的实现,如果你有那些,甚至略有重构。

感谢。

解决方案

根据这个参考(175昆明植物研究所)的一起的是:

  

发言中需要两个一部分   参数和通常导致   动词。例如, *:^:3 是一个   功能迭代平方3   时间( ^ 一起的)。

由于 ^ 属于上述范畴,把它应用到的参数结果在较长的动词:

 ((小于./@(2及{)),$:。@((&GT; ./@(2及{)),2及})。 )^(1&其中;#)
 

由于 $ 表示包含它的最长的动词,它是指上面只是写了code。

同样,下一 ^ 使从一个新的较长的动词((小于./@(2及{)),$。 :@((&GT; ./@(2及{)),2安培;}))^:(1&LT;#)

(((小于./@(2及{)),$:@((大于./@(2及{)),2安培;}。 ))^(1&其中;#))^:#
 

这又是 $简称:,因为它比previous一篇较长

由于它的不良行为,可能是唯一的解决办法就是分裂整个动词,使 $ ((LT ;. / @(2及{)),$:@((大于./@(2及{)。),2安培;}))^:(1&其中;#)尽管它不是oneliner:

  bubbleiter =:((小于./@(2及{)),$:@((&GT; ./@(2及{)),2及}))^(1&其中;#)
    冒泡=:bubbleiter ^:#
    冒泡3 1 2 9 2 9 86 5 9 6 9 6 45
1 2 2 3 5 6 6 9 9 9 9 45 86
 

本文有一些意见,以 $

  

解释什么 $ (自参考)   ,以及如何使用它竟然是   一个相当不幸的起点   对于其中一些完全陌生的   的语言,因为这是一种先进的   特征和是非典型的方式   事情是在J.约翰通常是   提到罗杰评论   他不会,如果包括这一点,他   现在在设计语言。

再次,要总结:

  • ^ 一起的,使一个新的动词从它的参数;
  • $:表示最长的动词包含它

由于飞出 estanford 的数据集 3 1 2 9 2 9 86 5 9 6 9 6 45 在他的回答。

Since I'm beginner in J I've decided to solve a simple task using this language, in particular implementing the bubblesort algorithm. I know it's not idiomatically to solve such kind of problem in functional languages, because it's naturally solved using array element transposition in imperative languages like C, rather than constructing modified list in declarative languages. However this is the code I've written:

    (((<./@(2&{.)), $:@((>./@(2&{.)),2&}.)) ^: (1<#)) ^: #

Here's the structure of the statement:

┌────────────────────────────────────────────────────────────────────────────┬──┬─┐
│┌───────────────────────────────────────────────────────────────┬──┬───────┐│^:│#│
││┌───────────────────┬─┬───────────────────────────────────────┐│^:│┌─┬─┬─┐││  │ │
│││┌──────┬─┬────────┐│,│┌──┬─┬────────────────────────────────┐││  ││1│<│#│││  │ │
││││┌──┬─┐│@│┌─┬─┬──┐││ ││$:│@│┌───────────────────┬─┬────────┐│││  │└─┴─┴─┘││  │ │
│││││<.│/││ ││2│&│{.│││ ││  │ ││┌──────┬─┬────────┐│,│┌─┬─┬──┐││││  │       ││  │ │
││││└──┴─┘│ │└─┴─┴──┘││ ││  │ │││┌──┬─┐│@│┌─┬─┬──┐││ ││2│&│}.│││││  │       ││  │ │
│││└──────┴─┴────────┘│ ││  │ ││││>.│/││ ││2│&│{.│││ │└─┴─┴──┘││││  │       ││  │ │
│││                   │ ││  │ │││└──┴─┘│ │└─┴─┴──┘││ │        ││││  │       ││  │ │
│││                   │ ││  │ ││└──────┴─┴────────┘│ │        ││││  │       ││  │ │
│││                   │ ││  │ │└───────────────────┴─┴────────┘│││  │       ││  │ │
│││                   │ │└──┴─┴────────────────────────────────┘││  │       ││  │ │
││└───────────────────┴─┴───────────────────────────────────────┘│  │       ││  │ │
│└───────────────────────────────────────────────────────────────┴──┴───────┘│  │ │
└────────────────────────────────────────────────────────────────────────────┴──┴─┘

Let's apply it to an array:

    (((<./@(2&{.)), $:@((>./@(2&{.)),2&}.)) ^: (1<#)) ^: # 5 3 8 7 2
2 3 5 7 8

The thing that confuses me is $: referring to the statement within the outermost parentheses. Help says that:

$: denotes the longest verb that contains it.

The other book (~ 300 KiB) says:

    3+4 
7  
    5*20  
100  

Symbols like + and * for plus and times in the above phrases are called verbs and represent functions. You may have more than one verb in a J phrase, in which case it is constructed like a sentence in simple English by reading from left to right, that is 4+6%2 means 4 added to whatever follows, namely 6 divided by 2.

Let's rewrite my code snippet omitting outermost ()s:

    ((<./@(2&{.)), $:@((>./@(2&{.)),2&}.)) ^: (1<#) ^: # 5 3 8 7 2
2 3 5 7 8

Reuslts are the same. I couldn't explain myself why this works, why only ((<./@(2&{.)), $:@((>./@(2&{.)),2&}.)) ^: (1<#) is treated as the longest verb for $: but not the whole expression ((<./@(2&{.)), $:@((>./@(2&{.)),2&}.)) ^: (1<#) ^: # and not just (<./@(2&{.)), $:@((>./@(2&{.)),2&}.), because if ((<./@(2&{.)), $:@((>./@(2&{.)),2&}.)) ^: (1<#) is a verb, it should also form another verb after conjunction with #, i. e. one might treat the whole sentence (first snippet) as a verb. Probably there's some limit for the verb length limited by one conjunction.

Look at the following code (from here):

    factorial =: (* factorial@<:) ^: (1&<)
    factorial 4
24

factorial within expression refers to the whole function, i. e. (* factorial@<:) ^: (1&<).

Following this example I've used a function name instead of $::

    bubblesort =: (((<./@(2&{.)), bubblesort@((>./@(2&{.)),2&}.)) ^: (1<#)) ^: #
    bubblesort 5 3 8 7 2
2 3 5 7 8

I expected bubblesort to refer to the whole function, but it doesn't seem true for me since the result is correct.

Also I'd like to see other implementations if you have ones, even slightly refactored.

Thanks.

解决方案

According to this reference (175 KiB) conjunction is:

A part of speech that takes two arguments and typically results in a verb. For example, *:^:3 is a function that iterates squaring three times (^: is a conjunction).

As ^: falls into above-mentioned category, applying it to the arguments results in the longer verb:

((<./@(2&{.)), $:@((>./@(2&{.)),2&}.)) ^: (1<#)

Because $: denotes the longest verb that contains it, it refers to the code just written above.

Similarly, the next ^: makes a new longer verb from ((<./@(2&{.)), $:@((>./@(2&{.)),2&}.)) ^: (1<#) and #:

(((<./@(2&{.)), $:@((>./@(2&{.)),2&}.)) ^: (1<#)) ^: #

which in turn is referred to by $: because it's longer than the previous one.

Since it's the undesirable behaviour, probably the only solution is to split the whole verb so that $: refers to the ((<./@(2&{.)), $:@((>./@(2&{.)),2&}.)) ^: (1<#) though it's not a oneliner:

    bubbleiter =: ((<./@(2&{.)), $:@((>./@(2&{.)),2&}.)) ^: (1<#)
    bubblesort =: bubbleiter ^: #
    bubblesort 3 1 2 9 2 9 86 5 9 6 9 6 45
1 2 2 3 5 6 6 9 9 9 9 45 86

This article has some comments as to $::

Explaining what $: (self-reference) is and how it's used turned out to be a rather unfortunate starting point for some of those completely new to the language as this is an advanced feature and is atypical of the way things are usually done in J. John mentioned that Roger has commented that he wouldn't include this if he were designing the language now.

Once more, to sum up:

  • ^: is a conjunction and makes a new verb from its arguments;
  • $: denotes the longest verb that contains it.

Thanks fly out to estanford for dataset 3 1 2 9 2 9 86 5 9 6 9 6 45 in his answer.

这篇关于记者:自引用冒泡排序默契实施的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆