From: Martti Halminen on 4 Apr 2010 05:13 Alexandra Vorobyova wrote: > Hey all, > > I'm currently learning LISP and have an assignment due tomorrow (for > my AI class), and I'm really having trouble getting this straight- I > understand the functions I have to use together in order to get the > answers, but not how really put them together. 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. > > Here are some functions I have to create. Help?: Generally, the accepted protocol is to show what you have written already, so that anybody trying to help you could see what you are getting wrong. - Otherwise any programs you get as answer are likely to be either heavily obfuscated, or in a style no professor would believe as your own. > > -Reverse all: to reverse a nested list, transforming: > '((1 2) (3 4) 4) --> (5 (4 3) (2 1)) one way of achieving this result: (defun reverse-all-incr (list) "Reverses a list recursively, incrementing the first item of the result if a number." (labels ((rev-all (d) (typecase d (cons (reverse (mapcar #'rev-all d))) (otherwise d)))) (let ((result (rev-all list))) (if (numberp (first result)) (cons (1+ (first result)) (rest result)) result)))) You didn't state what is the rule for transforming 4 to 5, so I'm assuming a simple one. Of course, if it was just a typo, the situation gets a little bit simpler, for example: (defgeneric reverse-all-b (d)) (defmethod reverse-all-b (d) d) (defmethod reverse-all-b ((d list)) (reverse (mapcar #'reverse-all-b d))) > -Remove left-most: to remove the first instance of an item inside a > nested list, for instance > (remove-left-most 'b '(a (b c) (c (b a)))) --> (a (c)(c b a))) While this might have some use as a list manipulation exercise, I wouldn't recommend using something like that in any serious program: this would modify the list structure wherever the value was found, so unless your data is highly structured, the results would be rather varying. > > -Find-substr: to find the first substring start with the given list: > (find-subst '(b a b) '(a b b a b c)) --> '(b a b c) If you intend to have any further programming career, whatever the language, you'd better learn to use more precision in your writing. Your first statement talks about substrings, yet the example shows lists of symbols, no strings anywhere. - As CL strings are vectors of characters, some sequence functions will work on both, but you might have to play with the equality predicates to get the exact results you want. > For this, I was trying to use the search and subseq functions > together, but it wasn't working So why not show what your attempt was, and what went wrong? --
From: Alexandra Vorobyova on 4 Apr 2010 06:31 On Apr 4, 1:29 pm, D Herring <dherr...(a)at.tentpost.dot.com> wrote: > On 04/03/2010 10:44 PM, Alexandra Vorobyova wrote: > > > > > Hey all, > > > I'm currently learning LISP and have an assignment due tomorrow (for > > my AI class), and I'm really having trouble getting this straight- I > > understand the functions I have to use together in order to get the > > answers, but not how really put them together. > > > Here are some functions I have to create. Help?: > > > -Reverse all: to reverse a nested list, transforming: > > '((1 2) (3 4) 4) --> (5 (4 3) (2 1)) > > > -Remove left-most: to remove the first instance of an item inside a > > nested list, for instance > > (remove-left-most 'b '(a (b c) (c (b a)))) --> (a (c)(c b a))) > > > -Find-substr: to find the first substring start with the given list: > > (find-subst '(b a b) '(a b b a b c)) --> '(b a b c) > > For this, I was trying to use the search and subseq functions > > together, but it wasn't working > > > -Subst-count: to find the number of substrings occurring in a list, > > such as: > > (subst-count '(b a b) '(b b a b a b b a a b)) --> 2 > > > So basically I have what the results my functions are supposed to > > give, but I can't figure out how to get them. > > The first two are simple exercises, slightly complicated by an > exercise in recursion. > > For example: To reverse all, first figure out how to reverse one > list. Then add recursion to reverse child lists (and leave non-lists > alone). > > The subst-count can reuse find-substr. > > Good luck, > Daniel 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? Thank you so much!
From: Martti Halminen on 4 Apr 2010 07:00 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 --
From: Alexandra Vorobyova on 4 Apr 2010 07:13 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: Alexandra Vorobyova on 4 Apr 2010 07:15 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?
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 Prev: How up-to-date are the Lispcast videos? Next: type of an array member of a class |