Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

What is Lisp good for?

Name: Anonymous 2007-07-26 4:56 ID:oSdzs5zo

When I hear Bash/Perl/Ruby/Java/C/ML/Erlang/Smalltalk/etc I know what I'm dealing with, I know what they are good for and when to use them, but what about List? Don't get me wrong, I like Lisp, I just don't see for what I could use it.

So, what are you doing with Lisp and why exactly Lisp over any other PL?

Name: Anonymous 2007-07-26 10:18 ID:AYkKzNh1

Lisp is also excellent for abstraction and good at eradicating code duplication

(Really simple example)

Let's say I have a function print-customer-info that takes a list containing customer information and prints it out nicely.

(define (print-customer-info customer-info)
  (let ((name (car customer-info))
        (age (cadr customer-info))
        (street (caddr customer-info))
        (sex (cadddr customer-info)))
    (display (string-append "Name: " name "\n"
                            "Age: " (number->string age) "\n"
                            "Street: " street "\n"
                            "Sex: " sex "\n"))))


And I call it like this:

(print-customer-info '("Dave" 23 "12 Elm Street" "male"))

And it prints out:
Name: Dave
Age: 23
Street: 12 Elm Street
Sex: male


But this stuff is annoying:


  ...
  (let ((name (car customer-info))
        (age (cadr customer-info))
        (street (caddr customer-info))
        (sex (cadddr customer-info)))
   ...


In a general sense, what I am doing is mapping the values of a list to some variables. But I really don't want to manually type out that for every single variable, do I?

So I can write a macro let-from-list that does this for me, so the code then looks more like this:

(define (print-customer-info customer-info)
  (let-from-list (customer-info) (name age street sex)
     (display (string-append "Name: " name "\n"
                             "Age: " (number->string age) "\n"
                             "Street: " street "\n"
                             "Sex: " sex "\n"))))


Which is a lot nicer. Of course, there's already a simple way to do this in Scheme. But the point is, if it wasn't built-in, I could add it myself.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List