Translating Java Idioms.
I've found it very frustrating to come to Lisp with a procedural background. "All you know is wrong!" is that kind of feeling you get when you confront the fact that you are perfectly clear about what you want to do, only that Lisp has you saying it in an entirely different way which you're not really used to.
It feels like learning a foreign language, where you know perfectly well what you want to say, only that you're lacking both the grammar and the vocabulary.
This page is not about the vocabulary nor is it about the syntax, on the grounds that Lisp has close to none at all, but on the grammar, or how to turn your sentence differently to basically express the same idea.
Many, if not most of the looping constructs we are accustomed to in Java land are probably better expressed in Lisp using recursion, especially tail recursion. Yet there are times when recursion just won't do it, because of considerations such as efficiency, or naturalness, or simply because we can't expect to get everything Lisp right in a few days. As for most things, it takes time to develop mastery in a given endehavour.
| Java | ![]() |
Lisp |
public class Java2Lisp {
public static void show(String s) {
for (int i = 0, len = s.length() ; i < len ; ++i) {
char c = s.charAt(i) ;
System.out.println("char at position "
+ i + " in " + s + " is " + c) ;
}
}
public static void main(String[] args) {
show("This is a string") ;
}
}
|
(defun show (s)
(do ; next 3 lines define (var init step)
((len (length s)) ; (var init) step omitted
(c) ; (var) init and step omitted
(i 0 (+ i 1))) ; (var init step)
((= i len) nil) ; (exit-condition . result) no result: explicit nil
(setf c (aref s i)) ; body is these two forms
(format t "char at position ~S in string ~S is ~S~%" i s c)))
|
|
[PB-JFB:~/workspace/worksheets] verec% javac Java2Lisp.java
[PB-JFB:~/workspace/worksheets] verec% java -cp . Java2Lisp
char at position 0 in This is a string is T
char at position 1 in This is a string is h
char at position 2 in This is a string is i
char at position 3 in This is a string is s
char at position 4 in This is a string is
char at position 5 in This is a string is i
char at position 6 in This is a string is s
char at position 7 in This is a string is
char at position 8 in This is a string is a
char at position 9 in This is a string is
char at position 10 in This is a string is s
char at position 11 in This is a string is t
char at position 12 in This is a string is r
char at position 13 in This is a string is i
char at position 14 in This is a string is n
char at position 15 in This is a string is g
[PB-JFB:~/workspace/worksheets] verec%
|
CL-USER 2 > (show "this is a string")
char at position 0 in string "this is a string" is #\t
char at position 1 in string "this is a string" is #\h
char at position 2 in string "this is a string" is #\i
char at position 3 in string "this is a string" is #\s
char at position 4 in string "this is a string" is #\Space
char at position 5 in string "this is a string" is #\i
char at position 6 in string "this is a string" is #\s
char at position 7 in string "this is a string" is #\Space
char at position 8 in string "this is a string" is #\a
char at position 9 in string "this is a string" is #\Space
char at position 10 in string "this is a string" is #\s
char at position 11 in string "this is a string" is #\t
char at position 12 in string "this is a string" is #\r
char at position 13 in string "this is a string" is #\i
char at position 14 in string "this is a string" is #\n
char at position 15 in string "this is a string" is #\g
NIL <- comes from the explicit nil above
CL-USER 3 >
|
20051113 JFB first release
I would love to hear from you!