From: Daniel C. <dca...@gm...> - 2010-12-12 19:29:54
|
Hello, I am new to Lisp, but I know several other programming languages (C, Perl, Ruby, Fortran, Python, JavaScript, PHP, etc). I have been trying to use online resources to learn Common Lisp but I'm having a really hard time finding the information I need. Most information is for people who are new to programming. I was hoping someone could help me with the following: (1) I would like to have a list of Common Lisp functions, and what they do. Right now I'm getting frustrated with SETF and SETQ and I can't find any information about them. (2) Can someone explain to me the difference between SETQ, SETF and DEFVAR? (3) Using SBCL, when I try to use SETQ I get an error I cannot understand: * (setq bar '(1 2)) ; in: LAMBDA NIL ; (SETQ BAR '(1 2)) ; ; caught WARNING: ; undefined variable: BAR ; ; compilation unit finished ; Undefined variable: ; BAR ; caught 1 WARNING condition Why is SBCL complaining that BAR is not defined? Of course it's not defined, I'm trying to define it right here... If I try to use the "list" construct, it's even worse. I just get the debugger: * (set bar (list 1 2 3)) debugger invoked on a TYPE-ERROR in thread #<THREAD "initial thread" RUNNING {AA5E679}>: The value (1 2) is not of type SYMBOL. Type HELP for debugger help, or (SB-EXT:QUIT) to exit from SBCL. restarts (invokable by number or by possibly-abbreviated name): 0: [ABORT] Exit debugger, returning to top level. (4) Can anyone tell me how to add two lists / vectors component-wise? I want to solve an algebraic problem. Suppose I have vectors (1 2 3 4) and (2 3 4 5). I'd like to make a function "add-vect" that gives me (3 5 7 9). I expect a syntax similar to this: (setq v1 (1 2 3 4)) (setq v2 (2 3 4 5)) (setq sum (add-vect v1 v2)) Thanks for the help. Daniel. -- No trees were destroyed in the generation of this email, but a large number of electrons were severely inconvenienced. |
From: Tamas K P. <tk...@gm...> - 2010-12-12 19:41:53
|
On Sun, 12 Dec 2010 20:29:47 +0100, Daniel Carrera wrote: > Hello, > > I am new to Lisp, but I know several other programming languages (C, > Perl, Ruby, Fortran, Python, JavaScript, PHP, etc). I have been trying > to use online resources to learn Common Lisp but I'm having a really > hard time finding the information I need. Most information is for people > who are new to programming. > > I was hoping someone could help me with the following: > > (1) I would like to have a list of Common Lisp functions, and what they > do. Right now I'm getting frustrated with SETF and SETQ and I can't find > any information about them. First, I would suggest that you get a decent book. PCL by Peter Seibel is very nice. There is an online version. It will introduce you to proper CL style too. When you have read PCL, you will find that SETF and SETQ, like all things in CL, are documented thoroughly in the Hyperspec. You can search that online (see eg lispdoc.com), but your IDE should make stuff really convenient. > (2) Can someone explain to me the difference between SETQ, SETF and > DEFVAR? > > (3) Using SBCL, when I try to use SETQ I get an error I cannot > understand: > > * (setq bar '(1 2)) > > ; in: LAMBDA NIL > ; (SETQ BAR '(1 2)) > ; > ; caught WARNING: > ; undefined variable: BAR > ; > ; compilation unit finished > ; Undefined variable: > ; BAR > ; caught 1 WARNING condition > > Why is SBCL complaining that BAR is not defined? Of course it's not > defined, I'm trying to define it right here... If I try to use the Just use DEFPARAMETER or DEFVAR. SETF is for assignment. Ignore SETQ for now. > "list" construct, it's even worse. I just get the debugger: > > * (set bar (list 1 2 3)) > > debugger invoked on a TYPE-ERROR in thread #<THREAD "initial thread" > RUNNING {AA5E679}>: > The value (1 2) is not of type SYMBOL. > > Type HELP for debugger help, or (SB-EXT:QUIT) to exit from SBCL. > > restarts (invokable by number or by possibly-abbreviated name): > 0: [ABORT] Exit debugger, returning to top level. > > > (4) Can anyone tell me how to add two lists / vectors component-wise? I > want to solve an algebraic problem. Suppose I have vectors (1 2 3 4) and > (2 3 4 5). I'd like to make a function "add-vect" that gives me (3 5 7 > 9). I expect a syntax similar to this: > > (setq v1 (1 2 3 4)) > (setq v2 (2 3 4 5)) > (setq sum (add-vect v1 v2)) (map 'vector #'+ '(1 2 3 4) '(2 3 4 5)) > Thanks for the help. Also try comp.lang.lisp for general CL questions. Best, Tamas |
From: Dan B. <db...@gm...> - 2010-12-12 21:37:32
|
On Sun, Dec 12, 2010 at 12:29 PM, Daniel Carrera <dca...@gm...> wrote: > (1) I would like to have a list of Common Lisp functions, and what > they do. Right now I'm getting frustrated with SETF and SETQ and I > can't find any information about them. In addition to what Tamas said, the hyperspec can be found here (along with other places on the web): http://www.ai.mit.edu/projects/iiip/doc/CommonLISP/HyperSpec/FrontMatter/index.html If you want to lookup, e.g., SETQ in the hyperspec, I find that googling "clhs setq" works very well. Of course integrating with your development environment is even better. I also really like the reference in Appendix D of Paul Graham's ANSI Common Lisp - another very good book on Common Lisp. > (2) Can someone explain to me the difference between SETQ, SETF and DEFVAR? comp.lang.lisp is better for this question, but since you are here already ... ;-) After you look all three up the hyperspec, you should be able to understand the following: CL-USER> (defvar foo 'a) FOO CL-USER> foo A CL-USER> (setq foo '(a b c)) (A B C) CL-USER> foo (A B C) CL-USER> (setf (car foo) 10) 10 CL-USER> foo (10 B C) CL-USER> (defvar foo 'a) FOO CL-USER> (setq (car foo) 100) ; in: LAMBDA NIL ; (SETQ (CAR FOO) 100) ; ; caught ERROR: ; Variable name is not a symbol: (CAR FOO). ; ; compilation unit finished ; caught 1 ERROR condition > (3) Using SBCL, when I try to use SETQ I get an error I cannot understand: > > * (setq bar '(1 2)) > > ; in: LAMBDA NIL > ; (SETQ BAR '(1 2)) > ; > ; caught WARNING: > ; undefined variable: BAR > ; > ; compilation unit finished > ; Undefined variable: > ; BAR > ; caught 1 WARNING condition > > Why is SBCL complaining that BAR is not defined? Of course it's not > defined, I'm trying to define it right here... It isn't giving you an error, it's giving you a warning that you are trying to set a variable that hasn't yet been defined via either defvar or defparameter. I personally find this error annoying while working at the repl, especially since it happens again if you try to setq/setf bar again, but not so annoying that I've tried to turn it off. Note that despite the error, bar *is* getting set to '(1 2). Maybe an sbcl person will tell us whether we should view this as purely a style thing, or whether the compiler is telling us that we are doing something that makes it hard to optimize the generated code. > If I try to use the > "list" construct, it's even worse. I just get the debugger: > > * (set bar (list 1 2 3)) Here I think you just have a typo; you mean setq or setf. if you really meant to use set, then you want (set 'bar (list 1 2 3)). comp.lang.lisp is a better place to ask about this 5th variation on setting variables. Dan -- Dan Becker |
From: Dan B. <db...@gm...> - 2010-12-12 21:52:05
|
Oh no, I left out an important part of the session, after the second (defvar foo 'a)! It should be as follows: > CL-USER> (defvar foo 'a) > FOO > CL-USER> foo > A > CL-USER> (setq foo '(a b c)) > (A B C) > CL-USER> foo > (A B C) > CL-USER> (setf (car foo) 10) > 10 > CL-USER> foo > (10 B C) > CL-USER> (defvar foo 'a) > FOO > CL-USER> foo > (10 B C) > CL-USER> (setq (car foo) 100) > ; in: LAMBDA NIL > ; (SETQ (CAR FOO) 100) > ; > ; caught ERROR: > ; Variable name is not a symbol: (CAR FOO). > ; > ; compilation unit finished > ; caught 1 ERROR condition > -- Dan Becker 303/497-6824 |
From: Daniel C. <dca...@gm...> - 2010-12-12 21:47:34
|
Hi Tamas, > First, I would suggest that you get a decent book. PCL by Peter > Seibel is very nice. There is an online version. It will introduce > you to proper CL style too. I forgot to mention, I did find that book and I've been reading it. It's the best information I've seen so far, but I don't particularly love it. Most of it talks about things I'm not interested in, and it omits things that I am looking for. I study best if I try to solve a problem that I'm interested in. So I tried to figure out how to do things with vectors so I could write a few mathematical routines, and I got stuck. > You can search that online (see eg lispdoc.com), Thanks. Good resource. > but your IDE should make stuff really convenient. Can you recommend an IDE for Linux? I've just been using a regular text editor. Emacs has syntax highlighting, so that's what I've been using, but I don't actually know Emacs. > Just use DEFPARAMETER or DEFVAR. SETF is for assignment. Ignore SETQ > for now. I must be missing something very basic. I can't use DEFVAR inside a function either: (defun myfunc () (defvar result 3) (result)) SBCL complains about "undefined function: RESULT". :-( > (map 'vector #'+ '(1 2 3 4) '(2 3 4 5)) Excellent. Thanks. >> Thanks for the help. > > Also try comp.lang.lisp for general CL questions. Thanks. I'm very new, so I wasn't even sure where to go for help. -- No trees were destroyed in the generation of this email, but a large number of electrons were severely inconvenienced. |
From: Giovanni G. <gi...@ci...> - 2010-12-12 22:32:33
|
Daniel, If you don't like PCL you can take a look at this, also free: http://www.cs.cmu.edu/~dst/LispBook/index.html Il giorno 12/dic/2010, alle ore 22.47, Daniel Carrera <dca...@gm...> ha scritto: > Hi Tamas, > >> First, I would suggest that you get a decent book. PCL by Peter >> Seibel is very nice. There is an online version. It will introduce >> you to proper CL style too. > > I forgot to mention, I did find that book and I've been reading it. > It's the best information I've seen so far, but I don't particularly > love it. Most of it talks about things I'm not interested in, and it > omits things that I am looking for. > > I study best if I try to solve a problem that I'm interested in. So I > tried to figure out how to do things with vectors so I could write a > few mathematical routines, and I got stuck. > >> You can search that online (see eg lispdoc.com), > > Thanks. Good resource. > > >> but your IDE should make stuff really convenient. > > Can you recommend an IDE for Linux? I've just been using a regular > text editor. Emacs has syntax highlighting, so that's what I've been > using, but I don't actually know Emacs. > > >> Just use DEFPARAMETER or DEFVAR. SETF is for assignment. Ignore SETQ >> for now. > > I must be missing something very basic. I can't use DEFVAR inside a > function either: > > (defun myfunc () > (defvar result 3) > (result)) > > SBCL complains about "undefined function: RESULT". :-( > > >> (map 'vector #'+ '(1 2 3 4) '(2 3 4 5)) > > Excellent. Thanks. > >>> Thanks for the help. >> >> Also try comp.lang.lisp for general CL questions. > > Thanks. I'm very new, so I wasn't even sure where to go for help. > > -- > No trees were destroyed in the generation of this email, but a large > number of electrons were severely inconvenienced. > > ------------------------------------------------------------------------------ > Oracle to DB2 Conversion Guide: Learn learn about native support for PL/SQL, > new data types, scalar functions, improved concurrency, built-in packages, > OCI, SQL*Plus, data movement tools, best practices and more. > http://p.sf.net/sfu/oracle-sfdev2dev > _______________________________________________ > Sbcl-help mailing list > Sbc...@li... > https://lists.sourceforge.net/lists/listinfo/sbcl-help |
From: Stig H. <sti...@gm...> - 2010-12-12 23:58:38
|
On 12/12/10, Daniel Carrera <dca...@gm...> wrote: > I study best if I try to solve a problem that I'm interested in. That is the best way, indeed. Personally I think "Practical CL" is better as a SECOND book on Lisp, when you are starting to become fluent and want to take the language for a spin. As your first book I would recommend Graham's "ANSI CL" or Winston/Horn's "Lisp". (If somebody knows a good online resource on this level, please chime in) > Can you recommend an IDE for Linux? I've just been using a regular > text editor. Emacs has syntax highlighting, so that's what I've been > using, but I don't actually know Emacs. The IDE of choice these days is an extension of Emacs called SLIME (Superior Lisp Interactive Mode for Emacs) which improves a lot on the built-in Lisp support in Emacs. And by a lot I mean A LOT. Emacs is huge. Don't let it intimidate you, you don't need to know it all to use it. Good luck! The world needs more Lispers. Stig Hemmer |
From: Pascal J. B. <pj...@in...> - 2010-12-12 22:27:52
|
Daniel Carrera <dca...@gm...> writes: >> Just use DEFPARAMETER or DEFVAR. SETF is for assignment. Ignore SETQ >> for now. > > I must be missing something very basic. I can't use DEFVAR inside a > function either: > > (defun myfunc () > (defvar result 3) > (result)) And you don't want to either. If you had read clhs defvar, you'd know that it does two things: - defines global variables, and - makes it special. And in general, you do not want special variables, you want normal, lexical variables. So you'd use LET or LET*. > SBCL complains about "undefined function: RESULT". :-( In lisp code, parentheses are used only for one thing, but always for that one thing: to apply an operator (call a function). So when you write: (result) you are calling the function named RESULT; you are not obtaining the value of the variable named RESULT, you are not jumping to the goto label named RESULT, you are not exiting from a block named RESULT, etc. (defun my-func () (let ((result 3)) result)) -- __Pascal Bourguignon__ http://www.informatimago.com/ A bad day in () is better than a good day in {}. |
From: Daniel C. <dca...@gm...> - 2010-12-12 23:14:57
|
On Sun, Dec 12, 2010 at 11:27 PM, Pascal J. Bourguignon <pj...@in...> wrote: >> SBCL complains about "undefined function: RESULT". :-( > > In lisp code, parentheses are used only for one thing, but always for > that one thing: to apply an operator (call a function). So when you > write: > > (result) > > you are calling the function named RESULT; Thanks. This really caught me off-guard. It's different from other languages where you can use parentheses just to group statements. I'll note, however, that there are a few instances where a parenthesis does not mean "apply operator": (defun foo() (let ((a 1) (b 2) (c 3)) (+ a b c))) There are seven open parenthesis "(" here, but only three of them have to do with operators. In this context, it wasn't evident to me that the "result" variable didn't have to be in a parenthesis as well. But anyway, I now have a clearer picture of how Lisp is meant to fit together. Thanks for the help. Cheers, Daniel. -- No trees were destroyed in the generation of this email, but a large number of electrons were severely inconvenienced. |
From: Pascal J. B. <pj...@in...> - 2010-12-12 23:41:24
|
Daniel Carrera <dca...@gm...> writes: > On Sun, Dec 12, 2010 at 11:27 PM, Pascal J. Bourguignon > <pj...@in...> wrote: >>> SBCL complains about "undefined function: RESULT". :-( >> >> In lisp code, parentheses are used only for one thing, but always for >> that one thing: to apply an operator (call a function). So when you >> write: >> >> (result) >> >> you are calling the function named RESULT; > > Thanks. This really caught me off-guard. It's different from other > languages where you can use parentheses just to group statements. I'll > note, however, that there are a few instances where a parenthesis does > not mean "apply operator": Notice I said: "In lisp code". Parentheses are also used in lisp data to merely wrap lists. Now, of course, there are parts of lisp code that are data, such as the _list_ of binding in a let form. > (defun foo() > (let ((a 1) (b 2) (c 3)) > (+ a b c))) > > There are seven open parenthesis "(" here, but only three of them have > to do with operators. In this context, it wasn't evident to me that > the "result" variable didn't have to be in a parenthesis as well. But > anyway, I now have a clearer picture of how Lisp is meant to fit > together. The point basically is that there are three kinds of operators: - functions (either a symbol designating a function, or a lambda expression), - macros (a symbol designating a macro), - special operators (a symbol designating a special operator). In practice, macros and special operators behave similarly: the interpretation of the subforms depends on the operator, which determines what is code and what is data. In the case of functions, all the subforms are expressions that are evaluated to obtain the arguments. -- __Pascal Bourguignon__ http://www.informatimago.com/ A bad day in () is better than a good day in {}. |
From: Mario C. C. <mar...@gn...> - 2010-12-12 23:49:54
|
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA256 2010-12-12 for Daniel Carrera <dca...@gm...> copy in sbc...@li... thread "New user - Various Questions, SETQ, adding lists...". Hello Daniel. >> but your IDE should make stuff really convenient. > > Can you recommend an IDE for Linux? I've just been using a regular > text editor. Emacs has syntax highlighting, so that's what I've been > using, but I don't actually know Emacs. Personally I use GNU Emacs with SLIME. If you currently don't know Emacs it's a good idea to start learning it now. SLIME (http://common-lisp.net/project/slime/) is great and can help you to familiarize with Common Lisp. You can customize Emacs with its special dialect of Lisp which is somewhat similar to CL (With important differences). Please note Linux is only the kernel of a more complex system some we like to refer as GNU/Linux. I Please take a look at http://www.gnu.org/gnu/gnu-linux-faq.html#why. Regards and enjoy learning CL!. -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.9 (GNU/Linux) iEYEAREIAAYFAk0FXj4ACgkQZ4DA0TLic4gIHgCfd4FipMHriRBcfB7W9LyMw0JI rNkAn2bnSpPLQSfXbOr8AguRPbccJYdk =FKzK -----END PGP SIGNATURE----- |
From: Daniel C. <dca...@gm...> - 2010-12-13 00:52:29
|
Hi Mario, On Mon, Dec 13, 2010 at 12:44 AM, Mario Castelan Castro <mar...@gn...> wrote: > Please note Linux is only the kernel of a more complex system some we > like to refer as GNU/Linux. I Please take a look at > http://www.gnu.org/gnu/gnu-linux-faq.html#why. :-) Thanks. I've been using GNU/Linux for 12 years, but like a most GNU/Linux users, I usually speak loosely and just say Linux. It's natural human laziness. People always shorten words that they use often. But I understand the FSF's frustration. -- No trees were destroyed in the generation of this email, but a large number of electrons were severely inconvenienced. |
From: Tamas K P. <tk...@gm...> - 2010-12-13 06:40:46
|
On Sun, 12 Dec 2010 22:47:27 +0100, Daniel Carrera wrote: > Hi Tamas, First, there is no need to cc me, I read the list. >> First, I would suggest that you get a decent book. PCL by Peter Seibel >> is very nice. There is an online version. It will introduce you to >> proper CL style too. > > I forgot to mention, I did find that book and I've been reading it. It's > the best information I've seen so far, but I don't particularly love it. > Most of it talks about things I'm not interested in, and it omits things > that I am looking for. > > I study best if I try to solve a problem that I'm interested in. So I > tried to figure out how to do things with vectors so I could write a few > mathematical routines, and I got stuck. You should work through PCL first. As the example below shows, you need to understand some basic constructs before you can do anything. >> but your IDE should make stuff really convenient. > > Can you recommend an IDE for Linux? I've just been using a regular text > editor. Emacs has syntax highlighting, so that's what I've been using, > but I don't actually know Emacs. Learning Emacs was one the best investments of my time. I use Emacs+SLIME+ParEdit and I find it great. > I must be missing something very basic. I can't use DEFVAR inside a > function either: > > (defun myfunc () > (defvar result 3) > (result)) > > SBCL complains about "undefined function: RESULT". :-( (defun myfunc () (let ((result 3)) result)) Chapter 4 of PCL discusses LET. >> Also try comp.lang.lisp for general CL questions. > > Thanks. I'm very new, so I wasn't even sure where to go for help. Also look at the c.l.l archives. BTW, there was a recent thread about IDEs, "Simple Lisp Support". Best, Tamas |