Live data from Hacker News

A raytracer in 7 lines of 'K'

nsl.com

11–20 of 41 posts

Re: A raytracer in 7 lines of 'K'

#11
post #8
post #6

Earlier quoted context omitted.

Except that a machine language program would be thousands if not millions of times as long. Readability is relative. Java is unreadable to my grandmother. Turkish is unreadable to me. In other words, the opinion of someone who hasn't learned a language regarding its readability is irrelevant. It is true that the way you read a language like this is different from the way you read Java. You don't expect to grok a line…

> Readability is relative. Java is unreadable to my grandmother. Turkish is unreadable to me. In other words, the opinion of someone who hasn't learned a language regarding its readability is irrelevant. What a gem! I should start a collection of those, every now and then you get these things you can just frame and look at three times a day here.

I'm just glad somebody got my point. It seems so obvious, yet it must not be, because the contrary is much more common. It especially bears repeating in the context of a largely undiscovered gem like K.

Re: A raytracer in 7 lines of 'K'

#13

You know, 7 lines or not, name your goddamned variables. I'd fire someone who presented me with code like this.

I'd fire someone who presented me code from the IOCCC (http://www.ioccc.org/) as well. That's entirely beside the point. The point of code like this is that it is short - other things can and will be sacrificed.

Edit: It's like complaining that the Mona Lisa is a crappy painting, because you are insistent on looking at it as if you were a cubist. It's completely missing the point.

Re: A raytracer in 7 lines of 'K'

#14

You know, 7 lines or not, name your goddamned variables. I'd fire someone who presented me with code like this.

I worked at a K shop last summer, and when I was learning the language, I tried using sensible variable names. I was told in no uncertain terms that, when you write K, local variables should be one letter, globals should be two, and functions should be three.

It sounds like a total disaster, but after about two weeks (which is a long-ass time to learn a language), everything made perfect sense, and I really enjoyed using it for the rest of the time. It's very powerful and fast to execute, and after you get the hang of it, it's fast to write, too.

Re: A raytracer in 7 lines of 'K'

#15
post #10

OK, this is a mess. Lets try to get some new coolness metrics here. A. Number of lines of assembler it compiles to B. Time it took you to write it (no cheating) C. Number of symbols used (with names being one symbol)

K is an interpreted language.

Re: A raytracer in 7 lines of 'K'

#16
post #15
post #10

OK, this is a mess. Lets try to get some new coolness metrics here. A. Number of lines of assembler it compiles to B. Time it took you to write it (no cheating) C. Number of symbols used (with names being one symbol)

K is an interpreted language.

Then it's JIT interpreted to object code.

Re: A raytracer in 7 lines of 'K'

#18
post #2

Voted up in hope that someone can adequately explain that piece of code frighteningly close to line-noise.

It's better once you realize that % is the division operator, and math always has right-to-left precedence (3*2+5 yields 21).

edit: there's also \:/: which is a cross-product, and ' (single-quote) is like mapcar. More explanations here: http://news.ycombinator.com/item?id=266101

Re: A raytracer in 7 lines of 'K'

#19
post #3

As far as readability goes, this might have as well been written in machine language.

Breaking into many lines and renaming variables would go a long way to make this code look more like C.

For a transformation in the opposite direction, here's a first draft interpreter for J (a related language) in J-ish C.

http://www.jsoftware.com/jwiki/Essays/Incunabulum

It segfaults, because it's written in rather non-portable C that assumes sizeof(function pointer) == sizeof(int), among other things. It's an interesting piece of code, though.

I don't completely understand it - I'm confused by the concept of rank in J, and haven't worked with the language enough to get over that hump. (IIRC, K avoids "rank" entirely, and just uses either an array or an array-of-arrays for multiple dimensions.) I've only been a tourist in the APL family of languages, but what I've seen has left me quite impressed.

Re: A raytracer in 7 lines of 'K'

#20
post #3

As far as readability goes, this might have as well been written in machine language.

Breaking into many lines and renaming variables would go a long way to make this code look more like C.

You say that like it's a good thing.
Post reply on HN