1

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
4

2 に答える 2

2

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.

于 2009-09-03T19:53:06.367 に答える
0

3:0を使用する代わりにmonad defineを使用すると、構文エラーではなくスタックエラーが発生することがわかりました。私はまだいくつかのねじれを解決する必要がありますが、私は進歩しています。

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
)

私の最近の試みは、今すぐ値エラーを取得することです。なぜ失敗するのかはまだわかりませんが、すぐにわかります。必要なことを忘れていたことがわかりました。条件文を追加すると、すべてが修正されます。

于 2009-09-03T14:06:45.467 に答える