LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-09-2010, 01:03 PM   #1
zaxonus
Member
 
Registered: Jul 2005
Posts: 66

Rep: Reputation: 15
Common Lisp functions.


Hello,

I have written 2 functions, this way :

(let ((sum 10))
(defun ACC10 (x) (setf sum (+ sum x))))

(let ((sum 50))
(defun ACC50 (x) (setf sum (+ sum x))))

I don't know if I am following good Lisp practices here; but the 2 functions do what I want.
Each of them keeps track of an internal value and I can make calls like :

(ACC10 5)
(ACC10 4)
(ACC10 -3)
(ACC50 -15)
(ACC50 22)
(ACC10 7)
(ACC50 13)
... etc ... each function is doing its book-keeping job.

My first question is :

To write say ACC97; could I start like ?

(defun ACC97 (x)
(let ((sum 97)) ....

instead of like before :
(let ((sum 97))
(defun ACC97 (x) ....

If yes what is the proper syntax ? Since what I tried did not work.

My second question is :
Would it be possible to write a function make-ACC that I could use to genarate my ACC functions above ?
For example in order to make ACC97 I would use something like :
(make-ACC 97)
and in the same way to make ACC50 I would use :
(make-ACC 50)

Here also what I tried did not quite work.

Thanks for any tip.
 
Old 07-09-2010, 01:13 PM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by zaxonus View Post
... Would it be possible to write a function make-ACC that I could use to genarate my ACC functions above ? ...
I am not a LISP guy, but isn't it what macros in LISP are for ?
 
Old 07-09-2010, 04:30 PM   #3
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
This is just Paul Graham's Accumulator Generator:
Code:
(defun foo (n) (lambda (i) (incf n i)))
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Common Lisp deep copy PatrickNew Programming 1 10-13-2009 12:02 AM
can I make C + common lisp simpler than C alone? ta0kira Programming 7 03-08-2009 10:56 PM
Programming Ideas in Common Lisp Trizon Programming 1 03-03-2008 02:07 PM
Common Lisp stand alone question Trizon Programming 5 05-23-2007 09:07 AM
lisp/scheme: is there a way to trace all functions? psiakr3w Programming 3 08-09-2004 07:46 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:08 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration