为什么这个 J 函数没有运行?

发布于 2024-08-03 16:58:29 字数 841 浏览 6 评论 0原文

我正在尝试学习 J,我正在使用的书说这是定义一元函数的正确方法

function =: 3:0 
    function statements

,所以我遵循这种格式并编写了折叠代码。你能告诉我为什么当我尝试使用输入调用它时会抛出语法错误,但如果我只调用 p 它会返回 3

h=:>:@i.@<.@-: :[: NB. gets all integers less than half of the input :[: forces error if used dyadicly
d=:(0&=|)~ h :[: NB. gets list where if one is set that index from h was a factor of the input y  :[: forces error if used dyadicly
p=: 3:0 NB. tells us p is a monadic function
   t =: d y 
   a =: i. 1
   while. 1<#t
      if. t~:0
         a =: a, #t
      end.
      t=: _1 }. t NB. found first mistake wrong bracket but fixing that doesn't fix it
   end.
   a*1
) 

NB. p gets a list of all integers that are factors of y
p 4
| syntax error
| p 4
p
3
NB. h and d run fine
h 4
    1 2
h 7
    1 2 3
d 7
    1 0 0
d 4
    1 1

I am attempting to learn J and the book I am using says this is the proper way to define a monadic function

function =: 3:0 
    function statements

so I followed this format and wrote the folding code. Can you tell me why this is throwing a syntax error when I try to call it with input but if I just call p it returns 3

h=:>:@i.@<.@-: :[: NB. gets all integers less than half of the input :[: forces error if used dyadicly
d=:(0&=|)~ h :[: NB. gets list where if one is set that index from h was a factor of the input y  :[: forces error if used dyadicly
p=: 3:0 NB. tells us p is a monadic function
   t =: d y 
   a =: i. 1
   while. 1<#t
      if. t~:0
         a =: a, #t
      end.
      t=: _1 }. t NB. found first mistake wrong bracket but fixing that doesn't fix it
   end.
   a*1
) 

NB. p gets a list of all integers that are factors of y
p 4
| syntax error
| p 4
p
3
NB. h and d run fine
h 4
    1 2
h 7
    1 2 3
d 7
    1 0 0
d 4
    1 1

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心不设防 2024-08-10 16:58:29

首先,3:0 解析类似于 (3:) (0),即单子“3:”应用于名词“0”。那不是你想要的;对于定义,您希望使用二元组“:”,因此需要用空格将其与 3 分隔开。

其次,您应该在定义中使用 =. 而不是 =:,因为 ta 是局部变量。

有几个部分可以简化:

d =: 0 = h | [             NB. does h y divide y
p =: d # h                 NB. select d y from h y

功能与以前相同,但更清晰、更快。

Firstly, 3:0 parses like (3:) (0), i.e. the monad "3:" applied to the noun "0". That's not what you want; for definitions, you want to use the dyad ":", so you need to separate it from the 3 with a space.

Secondly, you should use =. instead of =: inside the definition, as t and a are local variables.

Several parts can be simplified:

d =: 0 = h | [             NB. does h y divide y
p =: d # h                 NB. select d y from h y

Same functionality as before, but clearer and faster.

奈何桥上唱咆哮 2024-08-10 16:58:29

我发现我得到了一个堆栈错误,而不是使用 monad Define 而不是使用 3:0 的语法错误。我仍然需要解决一些问题,但我正在取得进步。

h =:>:@i.@<.@-:
d =:(0&=@|)~ h
p =: monad define
t =: d y 
a =: i.0
while. 1<#t do.
   if. {:t~:0 do.
      a=:a, #t
   end.
   t=: _1 }. t 
end.
a
)

我最近的尝试很接近,现在出现了值错误。仍然不确定为什么会失败,但我很快就会得到它。我发现我忘记了所需要做的事情。添加条件后可以解决所有问题。

I figured it out sort of I get a stack error instead of a syntax error with monad define instead of using 3:0. I still have to work out a few kinks but I'm making progress.

h =:>:@i.@<.@-:
d =:(0&=@|)~ h
p =: monad define
t =: d y 
a =: i.0
while. 1<#t do.
   if. {:t~:0 do.
      a=:a, #t
   end.
   t=: _1 }. t 
end.
a
)

my latest attempt is a good deal close getting a value error now. Still not sure why its failing but I'll get it soon. I figured it out I was forgetting the required do. after the conditionals adding them fixes everything.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文