# Learning Forth by working on a raytracer

After following a twitter feed about programming, I got overwhelmed by FORTH related posts. I had already read something about forth before (stack-based, somewhat fast, good for embedded devices), but so many bit.ly links pointing to webs of implementations of FORTH and FORTH things made me decide to, well, take a deeper look.

Looks like a nice language, having something I enjoy about Lisp (interactivity) and something I like about PostScript (stack based). Thus... time for real learning.

I have been thoroughly thinking about "What". You can't learn a programming language per-se, you need a project to work on.

I shuffled several things (besides implementing my own Forth in my HP49G... still don't know how to do it, and know too little Saturn Assembly... Someday!) and finally decided to make a new instance of my (unfinished) Lisp raytracer, in Forth. It will be quite a lengthy feat (will take at least 3 months, as I knew Lisp when I started that), but I hope it ends working and thus makes me happy and "forthy". For now, I have just one function, to calculate the euclidean norm of a 3-vector with components in the (floating point) stack:

Looks like a nice language, having something I enjoy about Lisp (interactivity) and something I like about PostScript (stack based). Thus... time for real learning.

I have been thoroughly thinking about "What". You can't learn a programming language per-se, you need a project to work on.

I shuffled several things (besides implementing my own Forth in my HP49G... still don't know how to do it, and know too little Saturn Assembly... Someday!) and finally decided to make a new instance of my (unfinished) Lisp raytracer, in Forth. It will be quite a lengthy feat (will take at least 3 months, as I knew Lisp when I started that), but I hope it ends working and thus makes me happy and "forthy". For now, I have just one function, to calculate the euclidean norm of a 3-vector with components in the (floating point) stack:

: norm3 ( X Y Z -- X Y Z R, returns norm of X, Y, Z on stack )

fover \ X Y Z Y

3 fpick \ X Y Z Y X

2 fpick \ X Y Z Y X Z

fdup f* fswap \ X Y Z Y Z^2 X

fdup f* frot \ X Y Z Z^2 X^2 Y

fdup f* f+ f+ \ X Y Z Z^2+X^2+Y^2

fsqrt ;

Probably is not the best implementation (I still have a tendency to C-ify things when stack-working), if someone around here knows a better way to do this, please drop me a line. By the way, the header image was found in Amazon... Strange cover for a programming book!

You can also check progress in this post, FORTH's "CREATE .. DOES>" Maybe I'm amazed? where I discuss what has been labelled "the jewel of FORTH", the "CREATE .. DOES>" structure.

You may also like

You can also check progress in this post, FORTH's "CREATE .. DOES>" Maybe I'm amazed? where I discuss what has been labelled "the jewel of FORTH", the "CREATE .. DOES>" structure.

You may also like