RSS

Tag Archives: emacs lisp

HOW-TO: Poor Man’s Quiz Scorer in Emacs Lisp


Sometimes it’s about small data.

Recently I’ve been studying a topic using a book, and at the end of each chapter there are quizzes of 20-25 questions. My method was to open a text buffer in Emacs, and vertically note down the question number, my answers, and after finishing this, go to the correct answer list, and mark correct answers with Y, and wrong ones with N:

Y 1- A,B,C
Y 2- C
N 3- D
N 4- C
Y 5- A
Y 6- B,C,D
Y 7- B

That was all fine, but I found myself counting the number of correct answers, and calculate my score in terms of percentage manually. I could of course quickly run the `M-x count-matches` (or `how-many`) to see how many correct / wrong answers I had, but doing this more than a few tests seemed to become tedious. Therefore, Emacs Lisp to the rescue!

With this simple Emacs Lisp function, assigned to F12 function key, I can now simply hit F12 and see my score percentage in Emacs:

“Your test score percentage: 71.42857142857143”

There are of course alternative methods to solve this straightforward problem, e.g. you can run some shell scripts on your Emacs (or VIM) buffer, or on a simple text file. But I like this solution being self-contained in Emacs, as well as the fact that it’ll continue to run as intended 30 years from now, in a newer Emacs version probably. This, and the fact that, my preferred tool for dealing with textual data gives me the flexibility to program it any way I want. It might have its drawbacks, it might be showing its age in its architecture, there are newer, shiny tools, there are specialized IDEs for various programming languages I use, but all of these notwithstanding, I still like to think it’s a beautiful thing that an editor released when I was born, that helped me with many tasks for decades, will still be with me for decades to come.

 
Leave a comment

Posted by on February 26, 2017 in Programlama, Emacs

 

Tags: ,

How to detect Friday the 13th with full moon using Emacs


nrAlmost 21 years ago, in 1994, I bought the brand new edition of Numerical Recipes in C: The Art of Scientific Computing. As an engineering and mathematics student who was much into programming, I still remember my excitement then, as well as the bright red hard-cover.

The first algorithm presented in the book (and its implementation in C programming language) was about calculating the dates that were Friday the 13th and had full moon. Now that I’m approaching the 21st anniversary of that day, I wanted to share a similar program to detect if the current date is Friday the 13th with full moon. This time, my preference is for Emacs Lisp, and instead of re-implementing the same algorithm, I simply use the calculations already provided by Emacs. In other words, I rely on the lunar-phase-list function that returns the lunar phases of the upcoming dates given a month and a year.

It’s been ages since I’ve done Common Lisp programming and many years since I’ve practiced Emacs Lisp, so the code below is probably not optimal and idiomatic, but nevertheless it follows:

You can used the function defined above to see whether it is your “unlucky” day:

This will return “Good luck!” most of the time, luckily! 🙂

Can you come up with other ways of detecting Friday the 13th with full moon using existing utilities, such as GNU calendar program? Or, how would you calculate the next 10 Friday the 13ths that had full moon, using Emacs Lisp or some existing utility?

 

 
Leave a comment

Posted by on November 12, 2015 in Emacs, Lisp, Programlama

 

Tags: , ,

How to subtract a number from the current word and replace the result (in Emacs Lisp)


I needed to do some Emacs Lisp hacking for a little project (there are more conventional methods but they don’t have the sheer pleasure of hacking Lisp ;-)). Basically I wanted to come up with a function that takes the current word, converts it into an integer, subtracts some number from it, converts the resulting number into a string again and then replaces the original “word” with the new value:

 
Leave a comment

Posted by on December 20, 2011 in Emacs, Lisp, Programlama

 

Tags: , ,