Lisp functions and very new to lisp

Discussion of Common Lisp
lisp noob
Posts: 3
Joined: Tue Jun 08, 2010 7:22 pm

Lisp functions and very new to lisp

Post by lisp noob » Tue Jun 08, 2010 7:46 pm

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 .

ramarren
Posts: 613
Joined: Sun Jun 29, 2008 4:02 am
Location: Warsaw, Poland
Contact:

Re: Lisp functions and very new to lisp

Post by ramarren » Tue Jun 08, 2010 11:40 pm

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.

gugamilare
Posts: 406
Joined: Sat Mar 07, 2009 6:17 pm
Location: Brazil
Contact:

Re: Lisp functions and very new to lisp

Post by gugamilare » Wed Jun 09, 2010 6:52 am

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.

Jasper
Posts: 209
Joined: Fri Oct 10, 2008 8:22 am
Location: Eindhoven, The Netherlands
Contact:

Re: Lisp functions and very new to lisp

Post by Jasper » Wed Jun 09, 2010 8:14 am

Code: Select all

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:

Code: Select all

(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.

lisp noob
Posts: 3
Joined: Tue Jun 08, 2010 7:22 pm

Re: Lisp functions and very new to lisp

Post by lisp noob » Wed Jun 09, 2010 2:25 pm

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.

ramarren
Posts: 613
Joined: Sun Jun 29, 2008 4:02 am
Location: Warsaw, Poland
Contact:

Re: Lisp functions and very new to lisp

Post by ramarren » Wed Jun 09, 2010 3:10 pm

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?

lisp noob
Posts: 3
Joined: Tue Jun 08, 2010 7:22 pm

Re: Lisp functions and very new to lisp

Post by lisp noob » Thu Jun 10, 2010 11:30 am

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.

ramarren
Posts: 613
Joined: Sun Jun 29, 2008 4:02 am
Location: Warsaw, Poland
Contact:

Re: Lisp functions and very new to lisp

Post by ramarren » Thu Jun 10, 2010 11:55 am

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.

Jasper
Posts: 209
Joined: Fri Oct 10, 2008 8:22 am
Location: Eindhoven, The Netherlands
Contact:

Re: Lisp functions and very new to lisp

Post by Jasper » Thu Jun 10, 2010 12:02 pm

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)

Code: Select all

(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.

DT6011
Posts: 1
Joined: Mon Jul 05, 2010 7:11 pm
Location: Germany

Re: Lisp functions and very new to lisp

Post by DT6011 » Mon Jul 05, 2010 9:50 pm

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

Post Reply