From: Alexandra Vorobyova on 4 Apr 2010 07:23 On Apr 4, 8:00 pm, Martti Halminen <martti.halmi...(a)none.invalid> wrote: > Alexandra Vorobyova wrote: > > I can't figure out how to use the 'find' function... I get the > > boundary definition, but I want to transform that into an actual > > instance of the substring, as shown in the example. How do you > > transform the boundary number (2 or 5 or whenever it starts) into an > > instance of it? > > http://www.lispworks.com/documentation/HyperSpec/Body/f_subseq.htm > > Wouldn't hurt you to read this, either: > > http://www.catb.org/~esr/faqs/smart-questions.html > > -- That's what I thought! I've been trying to merge find and subseq together, I've gotten this: (defun find-subst (x list y list) (if (null list) nil (if (search x list) (subseq y list (result (cdr list)))) But it's not working, I think the syntax is all wrong... yes?
From: Martti Halminen on 4 Apr 2010 07:52 Alexandra Vorobyova wrote: > That's what I thought! I've been trying to merge find and subseq > together, I've gotten this: > > (defun find-subst (x list y list) > (if (null list) nil > (if (search x list) (subseq y list (result (cdr list)))) > > But it's not working, I think the syntax is all wrong... yes? Yes, you're mixing normal function definitions and method definitions. in DEFUN you just list the parameters, not their types. So, here you are defining a function with 4 parameters, two of them with the same name which doesn't make much sense (at least with SBCL). Another problem, you have to remember the result of the SEARCH call for further use. So, what you should do is something like this: (defun find-subst (x y) (if (null x) nil (let ((pos (search x y))) (when pos (subseq y pos))))) This even works with strings, if they are in the same case: CL-USER> (find-subst "bab" "abbabc") "babc" CL-USER> (find-subst "BAB" "abbabc") NIL More generally, programming with cutting and pasting components without understanding what they do is a rather painful way to learn. I'd recommend reading those books first. --
From: Teemu Likonen on 4 Apr 2010 11:30 * 2010-04-04 01:21 (-0700), Sebastian Jaramillo wrote: >> -Reverse all: to reverse a nested list, transforming: >> '((1 2) (3 4) 4) --> (5 (4 3) (2 1)) > Here is a simple way of doing the first function. > there are probably better ways, but i hope it can get you started... > > (defun my-reverse (expression) > (cond ((null expression) expression) > ((listp expression) > (append (my-reverse (cdr expression)) (cons (my-reverse (car > expression)) '()))) > (t expression)) > ) It's probably easier to understand if flat reverse and recursion are separated. This allows replacing the flat reverse with the built-in if reversing itself is not part of the problem/exercise, only the recursion. Also, this recursive MAPCAR structure can be reused for code that recursively maps list's items. This is also much friendlier to the call stack: (defun reverse-flat (list &optional accumulation) (if list (reverse-flat (rest list) (cons (first list) accumulation)) accumulation)) (defun reverse-recursively (list) (reverse-flat (mapcar #'(lambda (item) (if (and item (listp item)) (reverse-recursively item) item)) list)))
From: Barry Margolin on 4 Apr 2010 15:35 In article <4bb844ee$0$283$14726298(a)news.sunsite.dk>, "Alex Mizrahi" <udodenko(a)users.sourceforge.net> wrote: > AV> how do you write the code? > > Didn't they teach this in class before giving you assignment? He said it's an AI class, not a Lisp programming class. At many schools, students in classes like this are expected to learn the language on their own, after perhaps a few introductory classes. -- Barry Margolin, barmar(a)alum.mit.edu Arlington, MA *** PLEASE post questions in newsgroups, not directly to me *** *** PLEASE don't copy me on replies, I'll read them in the group ***
From: Giovanni Gigante on 5 Apr 2010 11:20 > This sounds like you haven't read any good books about lisp programming. > You'd better take a look at for example http://gigamonkeys.com/book/ > before continuing. The OP has no time to read a book; she just want a quick solution for her assignment. I suspect she's not so interested in Lisp per se.
First
|
Prev
|
Pages: 1 2 3 Prev: How up-to-date are the Lispcast videos? Next: type of an array member of a class |