Giter VIP home page Giter VIP logo

ngn-k-tutorial's Introduction

ngn-k-tutorial

An ngn/k tutorial.

How to use this tutorial

This tutorial assumes you have some beginner programming knowledge, and a basic understanding of what arrays are.

Most chapters will have Vocabulary and Exercise sections at the end. I suggest getting familiar with the vocabulary and doing the exercises in each part before moving on the next lesson.

If you are feeling stuck with the tutorial's content or exercises, please come to the ngn/k room on matrix (recommended), the APL farm, or just open an issue here.

If you would like to check your exercise solutions with mine, you can check the code folder.

Progress

This tutorial is complete, and will not see the addition of any further chapters, unless ngn/k undergoes new changes.

Any other chapter ideas that were previously here may be covered in separate articles, outside this book. No promises though :)

Credits

  • ngn many tidbits and useful knowledge on the workings of ngn/k
  • @chrispsn, @Traws, @ColTim for ideas on the Prelude chapter, and primitive implementations
  • @HoosierEE for help with proofreading and many useful edits
  • @DiscoDoug for a lot of help with writing the sudoku chapter
  • /u/mandus for reminding me that I had exercises on this tutorial
  • @DestyNova for setting up the workflows required for the website, epub and pdf.
  • @yakubin for their help on correcting folds and scans

Finally, thanks to all the proofreaders who helped me ensure the quality of this book over the two years spent writing it.

ngn-k-tutorial's People

Contributors

destynova avatar dmsjms avatar gitonthescene avatar hmkemppainen avatar hoosieree avatar kidd avatar opfez avatar pitr avatar razetime avatar rdm avatar seitucbj avatar traws0 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ngn-k-tutorial's Issues

Consider describing how K treats whitespace

I was just reading about ': in more adverbs:

x ': y Window

Symbol: ':

Args: int ': array

Note how the syntax is written with a space separating the function/adverb from its args. I didn't pay much attention to this in the past, since I assumed it's just a stylistic thing and that the interpreter wouldn't care about it either way.

This assumption seems right for functions as well as function-adverb pairs where there is no space between the function and adverb:

 1-!5
1 0 -1 -2 -3

 1 - !5
1 0 -1 -2 -3
 1 2 3!'3 4 5
0 0 2

 1 2 3 !' 3 4 5
0 0 2

Window is different. I guess syntactically 3 is supposed to take the position of a function, and I guess the interpreter would expect the adverb (if any) to follow immediately, without a space:

 3':!5
(0 1 2
 1 2 3
 2 3 4)

 3 ': !5
'nyi
 3 ': !5
   ^

Trying other adverbs with a space also yields interesting results that are difficult to interpret with the information given so far:

 -\!5
0 -1 -3 -6 -10

 - \!5
0 1 2 3 4
0 -1 -2 -3 -4
 +\!5
0 1 3 6 10

 + \!5
0 1 2 3 4
,0 1 2 3 4
 2\123
1 1 1 1 0 1 1

 2 \123
123
'rank
 ��
   ^

 2 \ 123
123
'rank
 2 \ 123
  ^

Infocard is shown redundantly for range/odometer

In chapter 2:

!x Range

Symbol: !

Args: ! number

Description: Array with numbers from 0 to x-1.

Providing range with a single number gives an expected result:

!5
0 1 2 3 4
!3 3
(0 0 0 1 1 1 2 2 2
0 1 2 0 1 2 0 1 2)

But what does that second result mean? ! generalizes to arrays of numbers, and this is called odometer. What this does is generate all indices to index into an array that has the dimensions described in x. Each column denotes one element.

!x Odometer

Symbol: !

Args: ! numeric_array

Description: Generate an array where each column denotes an index into an array which has dimensions x,
in row-major order.

Odometer always generates an array of the length it is given, or in K's notation, #x.

In chapter 3:

! x Range / Odometer

Symbol: !

Arguments: ! number / numeric array

Description: range from 0 to x-1. Odometer is generalized range: generate indices for indexing into an array of shape x.

Scan examples give diffrent results in ngn/k

In 05-adverbs.md there are a couple examples, which give different results in ngn/k:

{x+y*z}\[1; 1 2 3; 4 5 6; 7 8 9]
(1 2 3
 4 5 6
 7 8 9
 29 42 57)

In ngn/k that's:

 {x+y*z}\[1; 1 2 3; 4 5 6;7 8 9]
(1 2 3
 4 5 6)

To get a similar last line I need to pass 3 as the first argument:

 {x+y*z}\[3; 1 2 3; 4 5 6;7 8 9]
(1 2 3
 4 5 6
 7 8 9
 29 42 57)

It seems that the 4-argument (n+1-argument variant for functions taking n arguments) variant of scan in ngn/k interprets the first arguments as a recursion limit. If the first argument is n, then it prints an n+1-element array, where the first rows are arguments passed to scan except the initial one. Subsequent rows are calculated recursively by using previous rows as arguments to the function passed to scan.

I think it would also be worthwhile to mention the variant of scan that doesn't use the extra first argument:

 {x+y*z}\[1 2 3; 4 5 6;7 8 9]
(29 30 31
 69 70 71
 123 124 125)

This one seems to interpret the first argument as an array of initial values. The output then has the same number of columns as there are elements in the first argument. Subsequent arguments are interpreted as as arrays of values to be interpreted as each argument. x always holds the accumulator. For 3-argument functions then the second argument to scan holds values of y, and the third argument holds values of z. The second and third arguments need to have equal lengths. The number of rows in output is equal to the length of second and third arguments to scan.

I think most people who are aware of scan from functional languages expect the second variant. So far the tutorial mentioning only the variant used for building up sequences recursively seems a bit confusing (as well as the fact that trying it out in REPL gives different results).

The same applies to fold:

 {x+y*z}/[1;1 2 3; 4 5 6;7 8 9]
4 5 6
 {x+y*z}/[3;1 2 3; 4 5 6;7 8 9]
29 42 57
 {x+y*z}/[1 2 3; 4 5 6;7 8 9]
123 124 125

Dealig with data from the internet - example

Hi,

It would be great If you could add to the IO section an example about the use of data from the internet.
I found that it supports the form host:protocol but cant find it working for URL adresses.
I found that I can use my shell commans like:
\curl https://example.com
but can't find a way to make K deal with the data (I'm to newbie in K)
#\curl https://example.com
doesn't count the length of the data...

Thank you,

"more than three arguments" for For/For scan?

I'm not sure what's meant by this bit in the More Adverbs chapter:

When called with more than three arguments, For and For scan both use the first argument as initial value, and the rest of the arguments as input arrays.

 {x+y*z}/[1; 1 2 3; 4 5 6; 7 8 9]
29 42 57
 {x+y*z}\[1; 1 2 3; 4 5 6; 7 8 9]
(1 2 3
 4 5 6
 7 8 9
 29 42 57)

(The two examples are the same as those in the first adverbs chapter and don't make sense here. Also they don't produce the same results for me, so we should replace them in that chapter too.)

I'm not quite sure what's meant by calling For or For scan with more than three arguments. Giving it an array on the right-hand side just produces one column of results given each element as a starting value:

 8{2*x}\1 2 3 4
(1 2 3 4
 2 4 6 8
 4 8 12 16
 8 16 24 32
 16 32 48 64
 32 64 96 128
 64 128 192 256
 128 256 384 512
 256 512 768 1024)

Thoughts on mixed base conversion

Looking at the first exercise under 9.3:

Write a function to convert seconds into hours, minutes and seconds, using \.

My first instinct would be something like {60 60\x}. However, this does not preserve hours:

 60 60\3600
0 0

After some trial and terror, I discovered that a zero allows overflowing digits to carry over without a limit, thus we can count any number of hours:

 0 60 60\3600
1 0 0
 0 60 60\172800
48 0 0

I don't think this is documented in the book or in the built-in help.

I disagree with the given code solution ..

bs:24 60 60\ / mixed base conversion.

.. because 172800 seconds most certainly isn't equal to zero hours, zero minutes, and zero seconds!

 bs 172800
0 0 0

I guess the exercise was meant to mirror the example from a few paragraphs above:

For mixed radix, we can try some time conversion: hours, minutes and seconds to seconds:

 24 60 60/2 46 40
10000

But this one at least allows inputting multiples of 24 hours without overflowing them to zero, so there's a bit of asymmetry:

 24 60 60/48 0 0
172800
 24 60 60\24 60 60/48 0 0
0 0 0

I guess the 24 here isn't serving any purpose unless you're actually trying to convert days. Then perhaps the example should be adjusted to input days:

 0 24 60 60\0 24 60 60/2 0 0 0
2 0 0 0
 0 24 60 60\0 24 60 60/0 48 0 0
2 0 0 0

This would be a proper mixed radix conversion and not just base-60 :)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.