Programming ≈ Fun

Written by Krešimir Bojčić

Lisp vs. Smalltalk vs. Ruby

I took first part of example from Peter Siebel book ‘Practical Common Lisp’.To understand it better I’ve tried to port it to Ruby and Smalltalk and see what I like and dislike about each. Bare in mind that Lisp is straight from the book written from a crazy smart person as far as I am concerned, and that my knowledge of all three languages is minimal.

Lisp

(defun make-cd (title artist rating ripped)
  (list :title title :artist artist :rating rating :ripped ripped))

(defvar *db* nil)

(defun add-record (cd) (push cd *db*))

(defun dump-db()
  (dolist (cd *db*)
    (format t "~{~a:~10t~t~a~%~}~%" cd)))

(defun save-db (filename)
  (with-open-file (out filename
             :direction :output
             :if-exists :supersede)
    (with-standard-io-syntax
      (print *db* out))))

(defun load-db (filename)
  (with-open-file (in filename)
    (with-standard-io-syntax
      (setf *db* (read in)))))
  • What I like :

    • Property list syntax when making-cd
    • Cool trick while dumping-db on the screen (list in list)
    • Emacs doing auto indentation on my behalf
  • What I dislike:

    • Not used to Polish notation
    • That cool trick is cryptic
    • File open - way to many params for default scenario to do
    • add-record and make-cd two functions (It’s speculation, probably right but I like to stick to YAGN until proven otherwise). I get a feeling that this kind of speculation is part of Lisp culture (everything is generic), and I dislike it

Ruby

$db ||= []

def add_record(title, artist, rating, ripped)
  $db << { title:title, artist:artist, rating:rating, ripped:ripped }
end

def dump_db
  $db.each do |cd|
    cd.each {|key, value| puts "%-10s%s" % [key.to_s.upcase ,value] }
    print "\n"
  end
end

def save_db(file_name)
  open(file_name, 'w') { |file| file.puts $db.inspect }
end

def load_db(file_name)
  $db = eval(open(file_name) {|file| file.read})
end
  • What I like

    • syntax for hash in add_record that is emulating keyed params from Lisp/Smalltalk
    • concise (perlisms)
    • conventions (implicit self, no parentheses…)
  • What I dislike

    • def/end is kinda ugly and eating up space, most noticeable for one liners. (Only thing worse from this would be Python ‘indentation’, it feels weird looking at Python code)
    • I miss Smalltalk blocks [] that I could be able to end on same line

Smalltalk

Object subclass: #Database
  instanceVariableNames: 'storage'
  classVariableNames: ''
  poolDictionaries: ''
  category: 'Test'

addRecord: theTitle artist: anArtist rating: theRating ripped: isRipped
  |dict|
  dict := Dictionary  new.
   dict at: 'title' put: theTitle;
        at: 'artist' put: anArtist;
        at: 'rating' put: theRating;
        at: 'ripped' put: isRipped.
   self storage add: dict

dumpDb
  storage do:[ :cd |
      cd keys do: [:key |
          Transcript show: key.
          Transcript show: ': '.
          Transcript show: (cd at:key).
          Transcript cr.].
      Transcript cr.  ]

storage
  ^storage ifNil: [ storage := OrderedCollection new].
  • What I like

    • You have to go OO - no way around it
    • Auto complete/templates while editing (Pharo)
    • Keyed parameters in the message (addRecord) - great stuff
    • Readability
    • Nicer bock syntax than Ruby
    • Image persistence (notice no db saving to file - it’s all in image)
  • What I dislike

    • Editor is terribly slow
    • Editor is trying to be smart (sometimes to much)
    • Verbose (I know this is trade-off for easier reading)
    • Image - it’s freaking me out, don’t listen to the lunatic from above that says it’s cool to have image.

I am doing all my stuff in Ruby, but after checking out Smalltalk it seems like a nice language without Perl influence. I think Perl influence might be bad for readability later, but I sure like perlisms when I am banging out code. Also I think the balance of perlisms is quite appropriate. As for Lisp it’s just not clicking with me (I am ‘trying’ to like it for third time). I am certain that I get the point of Lisp it’s just that I think that one property of being ultimately flexible is not that much of a deal that Lispers make of it.

All in all I think all three languages are great, but I will probably be sticking with Ruby. It’s not ideal, but it has pretty good balance, and probably is nicer to ‘normal’ people as Matz once said.

« Enterprise menu What's Special About Lisp? »

Comments

Copyright © 2019 - Kresimir Bojcic (LinkedIn Profile)