I have found in a forum the following J code that generates a Sierpinski
triangle, and am trying to understand how it works.
sierpinski =: {{ (, ((72#:~8#2){~3#.\0,],0:)@{:)^:y ,: 1 y} (>:+:y) $ 0 }}
' #'{~ sierpinski 15
#
# #
# #
# # # #
# #
# # # #
# # # #
# # # # # # # #
# #
# # # #
# # # #
# # # # # # # #
# # # #
# # # # # # # #
# # # # # # # #
# # # # # # # # # # # # # # # #
I am a J novice and what I have gathered so far is that
- {{ }} is an inline definition of a function. In this case a monadic one
with argument y.
- Calling sierpinski 15 yields a matrix with 0s and 1s making the shape of
a Sierpinski triangle.
- ' #'{~ uses the 0s and 1s in that matrix as indexes for the ' #' array,
thus converting zeros to spaces and 1s to #s.
- (>:+:y) is 2*y+1 (+: being 2* and >: being +1).
- (72#:~8#2) is a weird way of generating the first 8 digits of 72 in base
2. The digits are generated using the dyadic #: verb, with the arguments
switching sides with the ~ adverb.
The rest of the code is incomprehensible to me. In particular:
- I could not find the meaning of certain tokens using the dictionary. For
instance, what is (,? What is \0?
- This part of the program is so densely packed that I cannot even break it
into pieces: {~3#.\0,],0:)@{:)^:y
I would be very grateful if someone could help me understand this code.
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm