emacs elisp first attempts

For a long time I have wanted to learn elisp in emacs. I tried the various tutorials but I get bored of them too quickly. Too many words not enough action for me. I was reminded of Project Euler by Daniel Silverstone so I thought I’d have a crack at one of the problems. No. 1, Multiples of 3 and 5 seemed like a good place to start.

Here are my first attempts and questions I came up against. Many thanks to the people of #emacs on freenode for answering my questions.

Q: Why doesn’t the elisp doco within emacs have examples?

A: Often it does. But if not, check the elisp info pages. info-apropos. Or use s in an info buffer to search.

Q: is there a printf equivalent in lisp?
A: format – at first this confused me as I thought it would just format a string and not print it. I think it was a fundamental misunderstanding of what print actually does.

Q: in a defun, what should you do if the caller supplies an invalid parameter? like supplying a float instead of an int?
A: (error “Fail!”), check with integerp
(error ‘wrong-type-argument “Foo is bad, and you should feel bad.”)

Q: how would I check if a number is divisible by another number? (to give an integer result)
A: ijp | k-man: (defun (divisiblep x y) "is x divisible by y?" (zerop (mod x y)))

My final hurdle was having too many brackets around an if statement. I had written

if (or ((divisibleby i 3))

instead of

if (or (divisibleby i 3)

Once I had fixed that my program worked.

Leave a comment

Your email address will not be published. Required fields are marked *