From: Alexandra Vorobyova on 3 Apr 2010 22:44 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. SOS!
From: D Herring on 4 Apr 2010 00:29 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
From: Alexandra Vorobyova on 4 Apr 2010 01:25 I'm in Asia, so this is due tomorrow (Monday) morning, and I've been attempting to work on it all weekend. I completely understand all that you've explained, but yet I don't understand how to put it in action.. how do you write the code? Where would I start? Thank you, Sasha
From: Alex Mizrahi on 4 Apr 2010 03:51 AV> how do you write the code? Didn't they teach this in class before giving you assignment? AV> Where would I start? Here's a function which reverses flat, non-nested list: (defun my-reverse (lst) (if (consp lst) (append (my-reverse (cdr lst)) (list (car lst))) nil)) Or if append is not allowed: (defun my-reverse-helper (lst acc) (if lst (my-reverse-helper (cdr lst) (cons (car lst) acc)) acc)) (defun my-reverse (lst) (my-reverse-helper lst nil)) If you understand these functions (any of them), you can start from it -- reverse-all can be made with some trivial modification. If you do not understand, then you need to learn at least basics of LISP -- I'm afraid it is not possible to write functions when you don't understand even basics. By the way, note that usually for assignments of this sort teachers requires use of some limited set of standard functions. E.g. sometimes APPEND is allowed, sometimes it is not. LOOP is never allowed when you're supposed to learn recursive functions and so on. So be careful if you decide to google some of these functions and pretend that it's yours.
From: Sebastian Jaramillo on 4 Apr 2010 04:21 On Apr 3, 7:44 pm, Alexandra Vorobyova <alexandra.voroby...(a)gmail.com> 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. > > SOS! 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)) )
|
Next
|
Last
Pages: 1 2 3 Prev: How up-to-date are the Lispcast videos? Next: type of an array member of a class |