4 ms·
The same code in k: +/!100 4950 Or if you insist on 1-indexing: +/1+!100 5050
by kcl 19y ago
The same code in k:
+/!100
4950
Or if you insist on 1-indexing:
+/1+!100
5050
- Excedrin 19y agoWhat does '/' mean? Also, why does '+' seem to do different things in the first and second expressions? I suppose '!n' means "array of integers ending at n" which seems like an odd choice since it's neither 'not' nor factorial.
- kcl 19y ago'/' in this case is the adverb 'over'. When applied to a function it causes that function to fold over a list. This is the same as inject as far as I know. You are correct in supposing that !n generates an array of integers from 0 to n-1. It is somewhat of an unusual choice for a symbol. I've always thought of it as a factorial 'on the other side'. I don't know if that's what was intended. The k language compresses many old APL functions into symbols like this. Some make sense, some make less sense. You are after all dependent on your keyboard for the range of symbols you can type. In APL the same function was indicated by a lowercase Greek iota. '+' is being used in two different ways. The first way shows it being used in conjunction with an adverb, so '+/' is like a one-argument function that gets applied to the list at the right, but then '+' is inserted between each of the elements of the list because of '/'. '\' is a very similar adverb. It's called 'scan', and it's like 'over', except intermediate output is produced. It can help illustrate what's going on. !100 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 .. +/!100 4950 +\!100 0 1 3 6 10 15 21 28 36 45 55 66 78 91 105 120 136 153 171 190 210 231 253 276.. In the second case, '+' is being used as an infix operator, taking two arguments, 1 and !100, so '+' adds 1 to each element in the list 0 to 99, inclusive. This is simply what happens when you add an atom to a list. Each element in the list gets incremented by the value of the atom. Most verbs in k are like this, in that they do what you want, or maybe, they do what they should do if your preference is for something to occur rather than for an error to be thrown. 5 + 0 1 2 5 6 7 5 + !3 5 6 7 1 2 3 + 4 5 6 5 7 9 1 2 + 4 5 6 'length (an error)
- deleted 19y ago[deleted]
- deleted 19y ago[deleted]
- mdemare 19y agoWasn't k the only language that solved the sudoku problem in under 100 characters?