我正在尝试学习 J,而我正在使用的书说这是定义一元函数的正确方法
function =: 3:0 函数语句
所以我按照这种格式编写了折叠代码。你能告诉我为什么当我尝试用输入调用它时会抛出语法错误但是如果我只是调用 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