This is a read-only archive of lispforum.com. The forum was locked to new users and posts and is preserved here as static HTML from a database snapshot taken on 2019-09-07.

Lisp functions and very new to lisp

11 posts · 2624 views

I know Java, I know C, I know a touch of C++ but Lisp is absolute zero, well not really. I am to use car, cdr, cons, cond to write some functions and only these predicates. I know car returns first element of a list and I know cdr returns the rest and can be used to break a list down. I know cons constructs lists and cond is a big ugly way of saying I wish I was an if statement. The functions I need help writing will interleave two lists, another returns true if an element is in a list, returns true if two lists are identical, and the last function returns the set of all subsets in a list.

Any assistance is of great appreciation as I am like a newcomer to Lisp and I kinda like the interpreted functional language .

Re: Lisp functions and very new to lisp

lisp noob wrote:Any assistance is of great appreciation as I am like a newcomer to Lisp and I kinda like the interpreted functional language .
Common Lisp is neither functional nor interpreted. Do note that Common Lisp is a specific language in a lisp language family. Your question appears more like a Scheme homework problem. You should probably figure out what language are you using exactly before asking further question. Also if it is in fact homework, you should present at least some attempt at solution.

Re: Lisp functions and very new to lisp

Well, Lisp is very good for functional programming. I guess what he meant with interpreted language is that Lisp is dynamic and it has a REPL.

@lisp noob: cond is a very useful macro. If you want to use if, you can use if. Cond is used to concise sequential if statements to make it more concise and easier to read.

Make sure that what your are trying to learn is Common Lisp and not other dialect of Lisp. To learn Common Lisp I recommend the book Parctacl Common Lisp.

Re: Lisp functions and very new to lisp

if ( condition_a ) { do_a } else { if ( condition_b ) { do_b } else { do_c } }
Is a big ugly way doing COND. ... ? ... : ... is an ugly way of wishing Cs IF could go into expressions. (wtf @distinction) And member functions are an ugly way of saying you want WITH-SLOTS and conflating it with oop.

Do you have some CL implementation running? Are you on windows or Linux? Able should work there, it says in the faq that there aren't any binaries on there, but you'd have to see. Hopefully he left binaries for windows on there..

On Linux most people use emacs+sbcl. With Ubuntu 'sudo apt-get install emacs slime', installs them, and then you only need configure your .emacs file in your home directory. Looking at my file, i think it is:
(add-to-list 'load-path "/usr/share/common-lisp/source/slime/") ;So emacs knows where slime is.
(setq inferior-lisp-program "/usr/bin/sbcl") ;So slime(or emacs?) knows where the CL implementation binary is.
(require 'slime) ;Enable starting slime.
(slime-setup) ;Something, i am really making this up.
Not sure what else to tell you that can help, i guess you could start learning from something like PCL. There are many more functions and macros than you're saying of course. For instance, destructuring-bind to get elements from a list according to a form; typecase so you can do different things based on the type of objects; object-oriented stuff like classes, generic functions and methods; higher order functions etcetera.

Re: Lisp functions and very new to lisp

I am using Lisp Works as both my editor and REPL. It uses common Lisp and is an interpreter for the language. I need help understanding how the above problems could be approached using Common Lisp.

Re: Lisp functions and very new to lisp

lisp noob wrote:I need help understanding how the above problems could be approached using Common Lisp.
Half of your problems are solved by built in operators (MEMBER, EQUAL), one is a trivial LOOP and the subsets would, if actually needed, be approached by using a function from appropriate library.

If you meant reimplementing those using some contrived subset of the language, then you should ask a more precise questions. What exactly is your problem here?

Re: Lisp functions and very new to lisp

Using only car, cdr, cons, cond I am to assemble the functions, irrespective to their existence in the language or not, I initially posted above. I understand the use of the 4 predicates listed within this reply but I am very new to Lisp and it is a language quite foreign to what I am accustomed to. If you can provide me assistance in writing the functions listed above, that is wonderful.

Re: Lisp functions and very new to lisp

Three of the functions you asked about are impossible without a comparison operator, which none of the four you mention is. And none of them are possible without function definition/calling capability.

In any case a forum post is not a good medium for writing an introduction to a language. The good book at this level is A Gentle Introduction to Symbolic Computation. Please read it, and ask again if you have any specific questions.

Re: Lisp functions and very new to lisp

Okey, sorry if we got a bit sidetracked.(That was silly) Is LISTP, EQL allowed? I honestly don't know how to do those functions without being able to differentiate between atoms and lists, and comparing atoms. Here is how i'd one of those.(Presumably defun is allowed)
(defun in-list (item list)
  (when list ;If list is empty the item isn't in it.
     (or (eql item (car list)) ;Either we find it here and are satisfied,
         (in-list item (cdr list))))) ;or we have to continue searching in the function.
Or did i use too much there? (when test ..forms..) is (cond (test ..forms...)), or can also be turned into a cond, all the arguments of it become the condition of the clauses, and the bodies of the clauses are true. (this function is essentially FIND, of course.) You can probably guess how IF can be done in COND.

The comment lines tell a little about the thought process in making recursive functions like this. You look at what to do in the local situation, and then do that. Then once you're satisfied, you try it, and hopefully it works, otherwise you go figure out what you've reasoned wrong.

Ramarren is right, btw, learning by just trying stuff and asking isn't too effective. Better use some book, like the one he linked.(Personally i initially learned from PCL, but i am sure the one he linked is good too.) Trying stuff is good too, but it is horrible in figuring out the definitions of things, and i think it sometimes might lack depth in more advanced stuff.

Re: Lisp functions and very new to lisp

does anyone know of a good tutorial on the net that can help me implement the following LISP functions.

1.make-book (create a book structure from a list of parameters)

2.Make-Library (add a new book to the library)

3.Book-Author (returns the author's name of a book)

4.Set-loan (loan a book to a user by setting a flag; return nil if the flag is already true)

5.Book-loaned (returns the list of books loaned to a user)


a library type program seemed like a perfect type of program to learn some new stuff but i cant find an example of how the syntax might look for even the simplest parts. Any help is appreciated.
thanx
"Give a man a fish and you feed him for a day; teach him how to use the net and he won't bother you for weeks."
My flash drive: Lexar Echo SE

Re: Lisp functions and very new to lisp

Not sure where to find an 'accounting example' tutorial, if you mean that. There are a couple of good books, like Practical Common Lisp. this thread lists a bunch of other books, though not all as suitable.

You could, for instance, make what you say with hashtables(make-hash-table,gethash) and structures(defstruct make-... etc.) you might want a registered-book structure, because each type of book is there in some about.