Best program to use lisp on?

Discussion of Common Lisp

Re: Best program to use lisp on?

Postby Geoff Wozniak » Sun Aug 03, 2008 6:09 am

Brew wrote:Hi im new to using lisp and need to do some assignments, unfortunately ive not got any software on my computer to do so, Im wondering whats the most user friendly software that i could use. As a beginner ive got some pretty complex code to try get my head around, so any links would be greatly appreciated.


As mentioned earlier, you have Lispworks and CLISP for Windows, as well as SBCL (with the caveat that it may not be stable). There is also Allegro CL with an IDE for Lisp in Windows, but I find that Lispworks is a little more user-friendly, especially for beginners. One benefit of Allegro is that it doesn't have a time limit, although it still has a heap limit, but you'll probably never hit that if you're writing small programs.
Geoff Wozniak
 
Posts: 9
Joined: Wed Jul 16, 2008 5:38 pm
Location: Canada

Re: Best program to use lisp on?

Postby dlweinreb » Mon Aug 04, 2008 7:40 am

My survey paper at http://common-lisp.net/~dlw/LispSurvey.html describes all eleven currently-maintained Common Lisp implementations, and has lots and lots of pointers to textbooks, papers, resources, and so on.
Last edited by dlweinreb on Sat Aug 09, 2008 8:31 pm, edited 1 time in total.
dlweinreb
 
Posts: 41
Joined: Tue Jul 01, 2008 5:11 am
Location: Lexington, MA

Re: Best program to use lisp on?

Postby metageek » Mon Aug 04, 2008 8:41 am

Or, for a more functional approach, there's:

Code: Select all
(defun price-order-total (item-list)
(apply #'+ (mapcar #'third item-list)))
metageek
 
Posts: 10
Joined: Fri Jul 25, 2008 8:01 am

Re: Best program to use lisp on?

Postby reuben.cornel » Mon Aug 04, 2008 12:31 pm

Perhaps this might the more accurate functional version?

Code: Select all
(defun price-order-total (item-list)
  (apply #'+
    (mapcar #'(lambda(item)
           (destructuring-bind (i quantity unit-price) item
             (declare (ignore i))
             (* unit-price quantity)))
       item-list)))
reuben.cornel
 
Posts: 7
Joined: Sun Jun 29, 2008 9:48 am
Location: Sterling, Virginia, USA

Re: Best program to use lisp on?

Postby Kompottkin » Mon Aug 04, 2008 2:06 pm

reuben.cornel wrote:
Code: Select all
(defun price-order-total (item-list)
  (apply #'+
    (mapcar #'(lambda(item)
           (destructuring-bind (i quantity unit-price) item
             (declare (ignore i))
             (* unit-price quantity)))
       item-list)))


Oh, come on! You functional types surely can do better than that.

Code: Select all
(defun price-order-total (item-list)
  (reduce #'+ (mapcar #'*
                      (mapcar #'second item-list)
                      (mapcar #'third item-list))))


Now to transform this into points-free style... Wait, this isn't #haskell? Damn, I knew there was something wrong with the colours.
User avatar
Kompottkin
 
Posts: 94
Joined: Mon Jul 21, 2008 7:26 am
Location: München, Germany

Re: Best program to use lisp on?

Postby Brew » Thu Aug 07, 2008 11:09 am

thanks guys for the help, ive a problem though
I need to be able to add more function to the question.
for example im trying to figure out how to get this
LISP> (price-order-total '((bolts 7 2.99)(nuts 7 1.19)(screws 20 0.79))) 45.06

The above multiplys the order by the the price of each item, adding them along the way and returnin a total, in this case the totals 45.06


Ive got the code worked out im just wondering if anyone would be able to make it more neater for me?
i'll paste it here.

Code: Select all
(setf boltprice 2.99)
(setf nutprice 1.19)
(setf screws .79)
  (setf boltquantity 7)
(setf nutquantity 7)
(setf screwquantity 20)
 (setf bolttotal ( * boltprice boltquantity))
(setf nuttotal ( * nutprice nutquantity))
(setf screwtotal (* screws screwquantity))
(setf total ( + screwtotal nuttotal bolttotal))


I hope im posting this right, the code i have eventually brings me the total number. but it seems like perhaps it could be summarised?


Thanks in advance.
Brew
 
Posts: 5
Joined: Fri Aug 01, 2008 6:25 am

Re: Best program to use lisp on?

Postby Paul Donnelly » Thu Aug 07, 2008 2:07 pm

You should use DEFVAR or DEFPARAMETER to create global variables rather than SETF. Use DEFVAR when you want subsequent re-evaluations to leave the current value alone, and DEFPARAMETER when you want them to take effect. In this case, you want DEFPARAMETER.

When you find yourself using variables to represent tables of data, it's time to introduce some sort of data structure, the advantage being that data structures can be iterated over when they contain items that can all be treated in the same way.
Code: Select all
;; All orders will be created with this function, so I can change
;; the data structure I use for an order any time I like. I can add
;; optional and keyword parameters to it without disturbing the
;; legacy interface.
(defun order (item-name quantity item-price)
  (list item-name quantity item-price))

;; All legal operations on orders are defined here; if I change
;; the data structure I can simply update these functions
;; and legacy code will continue to work properly.
(defun name (order) (first order))
(defun quantity (order) (second order))
(defun price-per (order) (third order))
(defun subtotal (order) (* (quantity order) (price-per order)))

;; Since all orders have the same interface, I can simply iterate
;; when I want to deal with a lot of them.
(defun total-bill (bill)
  (loop for o in bill summing (subtotal o)))

Whenever you find yourself using a programming language as an excessively verbose calculator, it's time to take a step back and factor something out. In this case, I saw a whole bunch of orders, so I devised an interface of five functions to create and get data from orders, and used that interface along with some list operations (one of CL's built-in interfaces) to write TOTAL-BILL in a high-level way. When you get your interface right, you can program just by telling the computer what you want, viz. the total of a list of orders:
Code: Select all
;; Altogether, I've written one more line of code than you did--but if
;; I want to add more items to a bill, I just tack them onto this list
;; (which would probably be generated programmatically or read from a
;; database anyway).
(total-bill (list (order 'bolts 7 2.99)
                  (order 'nuts 7 1.19)
                  (order 'screws 20 0.79)))

If I wanted to be obsessive, I could have defined a bill type as well, but since a list is a list, unlike an order which stores several different types of data (name, quantity, price), I didn't see the advantage. If you expected to be typing up a lot of bills by hand, a macro implementing a less verbose syntax than (list (order 'thing &c... would be nice to have. You could also use DEFSTRUCT or DEFCLASS to create your data types, or make the list-based order type official with DEFTYPE.
Paul Donnelly
 
Posts: 148
Joined: Wed Jul 30, 2008 11:26 pm

Re: Best program to use lisp on?

Postby Paul Donnelly » Thu Aug 07, 2008 2:20 pm

One more thing:

Your use of ten assignment statements in a row is also a red flag. There's nothing wrong with SETF per se, but when you're creating a variable to store every step of the equation, you often end up creating a variable that only gets used once—such as... every single one of them, in this case :)—and you can often simplify your code by taking advantage of the fact that every form returns a value: turning the code "inside out", you might say. To do so, just substitute in all variables that are referred to only once. When begin to write a function, I'm often not sure which calculations I'll need to access twice, so I go ahead and add a a bunch of variables to my LET as I go along, with the expectation that's most of these will get unwritten when I take a look at the whole thing.

In this particular program, inversion results in a chain of nameless arithmetic, due to the loss of names, which is a clue that you either need to factor out some patterns before you simplify, or else that there are no patterns and what you really wanted was a calculator.
Paul Donnelly
 
Posts: 148
Joined: Wed Jul 30, 2008 11:26 pm

Previous

Return to Common Lisp

Who is online

Users browsing this forum: Bing [Bot], j831526, pjstirling and 2 guests