Notes on “Thinking Forth”

Notes on “Thinking Forth”

I saw a mention of Thinking Forth
online sometime back, and downloaded it.
I finally got down to reading it this lazy Sunday afternoon,
and made some notes.
To quote from the homepage

Thinking Forth is a book about the philosophy of problem solving
and programming style, applied to the unique programming
language Forth).
What I feel after reading this book is that many concepts and tips
taught in the book can actually be applied to which ever
programming langugage you are using now.
Here are some general notes that I have made.

Component Programming

Component programming is about minimizing the impact of any change.
A component is a resource, which can be a piece of hardware or
a software resource (such as a queue or stack).
This idea is in contrast to Sequential or Hierachical Design.
An example was given by the author: updating a record.
However when designing by components, we think in terms of the
components which describe the structure of the records,
provide a set of editing commands, and provide read/write
methods to storage.

Analysis

Thinking Forth talks about the Nine Phases of the Programming Cycle,
I took special note of the first two: discovering requirements and constraints,
and building a conceptual model of the solution.
To build a conceptual model, we can follow these steps:
1. Define interfaces
By using Data Flow Diagrams for example.
2. Define rules
By using English, Decision Tree, Tables. And subsequentlhy simplify
the rules so that it can be easily understood and coded
3. Define data structures.
This is a description of the structures’ conceptual model, i.e.
what attributes to keep.

Problem Solving

These are general ways we can use to approach problem solving.
– Determine you goal
– Picture the problem as a whole
– Develop a plan (e.g. work backwards)
– Step back from the problem
– Believe

I found Believe to be exceptionally inspiring.
To achieve success you first need to believe you can succeed.

Naming

As a pretty well-known quote goes

The 2 hardest things in Computer Science are naming things,
cache-invalidation, and off-by-one errors.
Thinking Forth gives some tips on naming things, though some
may actually be more relevant for the Forth language itself.
– Choose names according to “what”, not “how”
– Choose names that work in phrases
– Spell names in full
– Favour shorter words

I feel that naming is a balancing act.
On one hand you want the names to be expressive,
but you also want them to be concise.
For certain local variables, especially array indices,
it makes sense not to give them long names; you will be
typing them a lot, and they will not matter once you return
from the function, so naming them i is fine.
For variables with a bigger scope,
the name data could mean a lot of different things.

Factoring

Factoring is about organizing code into useful fragments
by separating reusable parts from non-reusable parts.
We can work to factor out:
– Data
– Functions
– Code within Control Structures
– Names
After factoring, make sure that the factored code serves
a single purpose, if not the refactoring might just have
made things worse.

Minimizing Control Structures

The rationale behind this is that control structures introduce
complexity into the code, and makes it harder to read and maintain.
This rationale is clearly demonstrated in an example given by the
author, where there is a heptly-nested (6 levels) if-else flow.
The author suggests a few ways to minimize control structures:
– Do not test for something that has already executed
– Combine booleans of similar weight
– When conditions do not share similar weight, place conditions that
are easier to calculate, or least likely to be true on the outside.
– Choose control structures that closely matches the control-flow

Closing Thoughts

I read through the book quite quickly, and skipped the parts which
I felt was about Forth, because I was more interested in the concepts
taught than the language.
However many concepts introduced
are based on what Forth provides, and how Forth works.
As such, I think that learning Forth while you are going through
this book will greatly increase the value gained from reading.
Nonetheless, I gained a lot of valuable insights from Thinking Forth,
all of which I shared above, which will definitely help me in being
a better programmer.
I would like to thank the author,
Leo Brodie, and
Thinking Forth Project, for
taking time time and effort to make the book available for free,
and in high-quality format.

What I learned about fonts

I started on this because I wanted to use unicode characters for LemonBoy’s bar. I wanted to display funky symbols without installing a new font, so I decided to use Dejavu Sans Mono to display them.

I had to get bar to find my font, and that is the gist of this post. In X, a font is specified in a funny way. Here’s an example:

-misc-dejavu sans mono-medium-r-normal--17-120-100-100-m-100-iso10646-1

Most of the time though, we will not need to be so specific, can this will do:

-*-dejavu sans mono-medium-r-normal--17-*-*-*-*-*-iso10646-1

If you’re as curious as me you’ll probably want to find out what all those numbers as me, and if you’re as stupid too, you’ld probably spend an entire afternoon finding out.

If you’re just interested in the answer jump to the bottom, ’cos I will be going though steps to find this information yourself.

  1. xlsfonts is is the server font list displayer for X (man xlsfonts) This is the most crucial thing we need. Get it from your distribution’s package manager, I’m on arch so I just had to

pacman -S xlsfonts

  1. skim through

xlsfonts --help

  1. try this:

    xlsfonts

You should see a bunch of fonts displayed similar to what I showed you above.

  1. If you have DejaVu Sans already installed you can probably try this:

    xlsfonts -l -fn -misc-dejavu sans mono-medium-r-normal–17-120-100-100-m-100-iso10646-1

Make sure you type the backslash as well, you have to escape the spaces.

  1. If not you, pick a font from the list generated by xlsfonts, and do:

    xlsfonts -l -fn -the-font-you-picked-some-numbers-blah-blah111

Or if you’re lazy to type we can just select the first font in the list:

xlsfonts -l -fn $(xlsfonts | head -1)
  1. Now thats not very interesting, a bunch of short cryptic headings, so lets try this:

    xlsfonts -ll -fn $(xlsfonts | head -1)

Note the 2 l’s this time. So this gets much more interesting, lots of information, not that difficult to understand. Most are self-explanatory, such as direction, rows, columns. To go more in-depth, try these search terms: character encoding, character map, fonts in x.

  1. Now things become clearer, lets separate the full font name by hypens, and match them to the properties in the output:
misc FOUNDRY
dejavu sans mono FAMILY_NAME
medium WEIGHT NAME
r SLANT
normal SETWIDTH_NAME
ADD_STYLE_NAME
17 PIXEL_SIZE
120 POINT_SIZE
100 RESOLUTION_X
100 RESOLUTION_Y
m SPACING
100 AVERAGE_WIDTH
iso10646 CHARSET_REGISTRY
1 CHARSET_ENCODING

SLANT has to do with the italics, r stands for Roman, there is also o for Oblique. Mystery solved! I don’t know what every property means, but I guess you can probably find out if you want to

This worked for bar, with the specific full font name. I also learned that the PIXEL_SIZE and POINT_SIZE should not both be set at the same time. For example, looking at the information above, you can set PIXEL_SIZE and POINT_SIZE to 17 and 120 respectively, but 12 and 120 will fail.

I suppose this has something to do with font rasterizing, so setting one will optimize the other for you. As such I chose to fidget with PIXEL_SIZE and leave POINT_SIZE as *.

What I’m using now is this:

-misc-dejavu sans mono-medium-r-normal--11-*-*-*-m-*-iso10646-1"

Which ensures that I get the correct font family, weight, encoding, which I guess are the most important parts, just leave the rest to the computer!

Notes: xlsfonts has a -lll flag to, shows you every character in the font, pretty crazy and useful at the same time! And guess what I think I’ll be back to stlarch so it looks so much better.

Why are tools like VIM and Emacs still used for coding?

I saw this question on Quora and thought I’ld leave a reply:

1) No mouse functionality might actually be a good thing. You almost never need to get your fingers off the keyboard.
This leads to quite some benefits:
a) Speeds up your coding (no longer have to move you hand to the mouse, aim at some small button, or painstakingly highlight a block of text, then do whatever you want with your mouse, then move back to the keyboard and continue typing).
b) Potential healthy benefits because you don’t strain your hands as much by movement.

2) There are lots and lots of commands, but trust me you won’t use most of them. There are just that few (maybe 30?) which you need. And when you need more powerful commands, you find them and slowly add them to your arsenal. Besides, many commands are pretty intuitive. For example, this command

di(

will delete everything within the round brackets. How do I remember it? “DeleteInside (“. That’s not all, plenty of examples out there, its just a matter of whether you are willing to learn them.

3) Highly customizable. The number of plugins[1], color schemes, configuration files available is just crazy.

4) Ubiquity. Vim is more or less available on every single Linux machine. Like Anon mentioned below, vi is available when you ssh. So basically, learn once, use everywhere. Furthermore, you can store your config files in the cloud[2], so you get a a Vim that belongs to you whichever machine you are on.

5) You may think that the UI is irritating, but look at it from another angle, what do you actually see when you fire up Vim? Code, line numbers, row and column numbers. That’s all. What do you actually want to see when you code? I for one wouldn’t want to see a hundred buttons (scissors, clipboard, question mark icons etc etc.) Your screen is just code so you can focus!

These are just some reasons why I use Vim (I can’t give you the Emacs side of the story but mostly they are similar), and there are quite some discussions online about it too[3], so take a look!

So why not give Vim a try, you might like it! If you need some resources to help you get started just drop me a message I’ll be glad to help. 🙂

[1]: scripts : vim online (vim.org)vim-colorschemes (github.com),
[2]: GitHub does dotfiles – dotfiles.github.com (github.com)
[3]: Why, oh WHY, do those #?@! nutheads use vi? (viemu.com)

 

Thanksgiving

Who do you thank?

Thanksgiving is hardly, if not never, celebrated in Singapore, but it should be.
When you sit down, and spend time thinking of whom to thank, or what to thank,
it makes you feel so much more, happy. You feel more appreciative, of all the
experiences you have been through, all the people around you, all the things that you
have.

Thank you dear family

To the closest people in my life, thank you. To my parents for raising me, for
providing for me, for teaching me, nurturing me. There are just so many
things to thank you for. For supporting me, no matter my decisions. To my sister,
for your words of encouragement, for your questions about math and science and
wireless, because you make me feel privileged that I have someone to teach and guide.

Thank you darling

To the girl I love, thank you.
For showing me how to love, for loving me, for letting
me love you. For the good and bad times we share. For the many wonderful memories,
some of which I have forgotten, just because there were so many. For your constant
motivation, for your care, for being there.

Thank you friends

To the people surrounding me, thank you. For the times we worked hard together,
for the times we enjoyed our success, and failures. To MAD,
thank you for the nights
of practice. Being able to work hard with a group of people sharing a common goal
was really a joy. To the BBoys especially,
for showing me what true friends are like.
For sticking up for one another, for entertaining one another, for wingman-ing one
another. Cheers to everlasting brotherhood. To the people in NUS,
I have not known you for long, but thank you for the study sessions, for the
lunch breaks together. If not for all of you, school will really be lonely.

What I am thankful for

I’m thankful for being a person of good health. For being a person who is able to
pursue his interests. To not be constrained by lack of finances. To be brought
up in a non-discriminatory environment. To be surrounded by people whom I enjoy
being with, and hopefully they enjoy being around me too. I’m thankful for
being able to write and read, and surf the net. And I thank you
for reading this.

Bus Factor

Thanks to NUS Hackers’ Friday Hacks, I was able to learn some practices that will help me become a better programmer.

In short,

1) We need to care, care about the team, care about yourself, care about users, care about the product, and care about the code.

2) Humility, there’s always more to learn, so don’t stop learning

3) Communication, no one will know what you are doing if you don’t tell anyone clearly

4) Simplicity, don’t over-complicate things!

5) Flexibility over rigidity

6) Pragmatism over perfection, better make something imperfect that exists rather than and imaginary perfect item.

How to Go On in Life

I wanted to title this post
“How to be successful in life”, but I realized I wasn’t in a position
to give such advice. I am barely successful (compared to the
many stories I have heard). But I guess I can at least go on in life.

I suck.

I read this post recently, saying that sometimes you have
to tell yourself its okay that you suck. That really brought
some perspectives to me.
For me? I suck at a lot of things.
I thought I was good at math, until I went to University and realized,
damn, math is freaking hard, and I’m getting owned by the other students.
I thought I was decent at basketball, because I played a lot when I was younger;
but oh man I played yesterday and I was BEAT after half an hour.
I thought I was a good runner, trained pretty hard and completed 2 marathons
thus far; but I dropped out halfway through my last marathon.
I thought I could dance not bad; but I lost passion halfway, didn’t practice
anymore, and I’m just whack now.
And I’m going to stop here in case my ego collapse on itself.

Think about someone who is the best at something. Maybe the fastest
man on Earth under 100m: Usain Bolt? There has to be something he isn’t good at.
Maybe dancing? Maybe not? How about Lance Armstrong? He was the best. Until
they found out he took drugs. That sucked. The point is, everyone has
to suck at SOMETHING.

I guess admitting that you suck at something is important. Yet at the same
time it’s trivial. What is more important is what happens after you
admit that you suck. What are you going to do about it?
Two ways to go about:

1) Continue sucking
2) Suck less

Life is short. There’s only so many things that we can do
in this few decades. That’s why for some things, we can only continue
to suck at, but for others, we can try to suck less.

For things that we would like to continue to suck at, we should just
not do anything about it. It will be a waste of our effort if we tried.
For me, I am willing to suck at running, with the knowledge that I have
had some achievements in that activity. I’m happy I have completed
marathons before, and just tell myself that I will be satisfied
not breaking that 5-hr mark.

But for things that we would like to suck less in, work needs to be done.
How do you get yourself to suck less?
Do it one. Step. At. A. Time.

Here’s my plan. I want to suck less at two things: Dancing, and Programming.
For Dance, the first step is to, well, dance! The great thing is that
I have friends who are still dancing. I can just easily hop on and join
them. I believe I will have a fantastic time.
For Programming, I’ll need to hack something up. I really like reading
about programming, but all reading and no hands-on makes me a dull boy.
No wait I mean… The main point is that I should work on something.

Those two sentences don’t exactly sound like small steps. Those are really
goals; it is me beginning with an end in mind. Somewhere on some piece of paper
or post-it, I will have to sketch out my steps again. Sketch out steps that I will take
one. Step. At. A. Time.

And maybe it will look something like this.
Dance:
Buy shoes. (Not going to break in my Tigers)
Train some six-steps. (Because foundation is important as hell)
Get my swipes back. (‘Cos those things were what I did best)
Halo. (Old vid but great vid)
Get a beanie because Halo’s causing me to bald. (And to look cool)
More halos. (Rinse)
More swipes. (Repeat)
Find more moves. (Keep learning)
Learn the moves. (Keep improving)
Invent moves. (Be original)
Talk to my bros. (They are my inspiration)

Hack:
Code a reverse polish calculator.
Code an infix calculator.
Hack together something that talks to GDrive. (That’s my project in mind)

G+ post here.

Wireless charging

Image courtesy of The Verge

Lumia 920 and 820 brings to us wireless charging, and I was just thinking about how it can bring little conveniences into our lives if wireless charging is to become more common. Here’s a little idea:

A laptop that has an ejection disk drive, but you don’t put disks inside. Instead you place your phone on a little platform that slides out, and your phone is charged via induction.

And to dream a little bigger, NFC sharing with your laptop!

There, no more USB-microUSB-19-pin-etc mess, less chargers to bring around and less worry about insufficient wall sockets.