Friday, October 31, 2008

Ido

This is likely old news for Emacs old-timers, but today I read about ido.el at M-x all-things-emacs (which is now btw. officially the first blog I feel like subscribing to - yay!). It essentially gives you fuzzy search for C-x C-f and C-x b. The first comment mentions an EmacsWiki article that uses ido for M-x command completion - see here. ido.el comes with packaged with recent Emacsen, so you're most likely ready to just M-x ido-mode.

Sunday, October 19, 2008

Lisp exercises

Long time, no post... Or something like that. Life's been moving ridiculously fast these days. Anyway, I got started learning Lisp (Common Lisp, if you most know) and found some nice exercises here. I'll post some of my first solutions here. I don't feel like posting to the newsgroup with this, but if you happen to have some Lisp experience and time, I'd appreciate some comments.

Some solutions:
;;; P01 - Find the last box of a list.
(defun my-last (list-arg)
  "By checking the length"
  (let ((list-length (length list-arg)))
    (nth (1- list-length) list-arg)))

(defun my-last-twist (list-arg)
  "By destructing a copy"
  (let ((list-tmp list-arg) (last) (tmp))
    (loop while (setf tmp (pop list-tmp)) do
         (setf last tmp))
    last))

;;; P02 - Find the last but one box of a list.
(defun my-but-last-rec (list-arg)
  "By recursion"
  (cond
    ((null list-arg) nil)
    ((null (rest list-arg)) nil)
    ((null (nth 2 list-arg)) (first list-arg))
    (t (my-but-last (rest list-arg)))))

(defun my-but-last (list-arg)
  "By indexing"
  (let ((list-length (length list-arg)))
    (if (<= 2 list-length)
        (nth (- list-length 2) list-arg)
        nil)))

;;; P07 - Flatten a nested list structure.
(defun my-flatten-real (list-arg)
  (if (not (listp list-arg))
      (list list-arg)
      (let ((acc (my-flatten-real (first list-arg))))
        (loop for item in (rest list-arg) do
             (setf acc (append acc (my-flatten-real item))))
        (return-from my-flatten-real acc))))

(defun my-flatten (list-arg)
  "Wrapper to keep this from returning a list of non-list arguments"
  (if (and (not (null list-arg)) (listp list-arg))
      (my-flatten-real list-arg)
      nil))

(defun sol-flatten (list-arg)
  "Based on original solution"
  (if (and (not (null list-arg)) (listp list-arg))
      (let ((item (car list-arg)) (list-rest (cdr list-arg)))  ; (rest list) <==> (cdr list) ?
        (if (listp item)
            (append (sol-flatten item) (sol-flatten list-rest))
            (append (cons item nil) (sol-flatten list-rest))))   ; (cons item nil) <==> (list item)?
      nil))

Thursday, October 2, 2008

My very own number

'Buy your own number for free' is the latest hype on the 'net. Well, we have that too, right here in Hungary :)
And. My own number: 97179. A palindrome, with a nice big prime factor. Yeah right, my very own number. Well, whatever.