Live data from Hacker News

A raytracer in 7 lines of 'K'

nsl.com

21–30 of 41 posts

Re: A raytracer in 7 lines of 'K'

#21

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 u…

I haven't spent enough time with K to get to that point of naturalness. I do think a k like language that used slightly more natural nemonics could have much broader appeal with minimal impact on code length. IMHO what makes k (and j for that matter) concise is the primitives, how they combine naturally, and how iteration is subsumed to avoid any form "item at a time" code. That last bit is what separates it from the common map/fold idioms in other languages.

Example: http://www.nsl.com/papers/kisntlisp.htm

Re: A raytracer in 7 lines of 'K'

#22
post #6
post #3

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

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…

> Except that a machine language program would be thousands if not millions of times as long.

Oi, I seriously doubt that. Seriously, millions? It's all math. Take a couple of assembler instructions per operation. I bet there's 256 byte assembler demo's out there...

Re: A raytracer in 7 lines of 'K'

#24

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

That's golfing though. Most real code gets a bit more wordy.

I could, for instance, cut about 80% of my line count in the two Q projects I've open sourced if I used naming and spacing conventions as shown.

http://github.com/DannoHung/qutil http://github.com/DannoHung/qspec

[/Tooting Own Horn]

Re: A raytracer in 7 lines of 'K'

#25
post #17

Will one of you geniuses hurry up and write an open source K interpreter? It's not that complex of a language to parse..

I wrote a rather basic J-ish interpreter, but it was understand the mindset of the language, rather than to have a practical implementation. (I don't think I have it anymore, but it was only about two pages of Lua.) To do it well, real experience with J/K/APL would probably be necessary. It wouldn't need to be absolutely compatible with K, just a similar language.

FWIW, there's A+ (http://www.aplusdev.org/), but I would far rather stick to the ASCII character set.

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

Re: A raytracer in 7 lines of 'K'

#28
post #17

Will one of you geniuses hurry up and write an open source K interpreter? It's not that complex of a language to parse..

The source to Mr Whitney's prior language, A+, is available.

Unfortunately the source is rather poorly coded - it's Arthur's "K in every language" style of C - and A+ has a lot of assumptions that I'm not comfortable with. I'd love a modernized K interpreter suitable for use by stylish gentlemen. Which perhaps could be the name..

Re: A raytracer in 7 lines of 'K'

#30

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

Using single letters in a piece of code helps in outlining the structure, instead of the contents.

That is why mathematical formulas are written with x, y, and z instead of long names. The idea is that we are emphasizing the generic relations, not what might be fed to the function.

Similarly, when writing functional code it is good style to use single letters instead of long names.

Post reply on HN