From: ···········@gmail.com
Subject: Need lisp help!!!
Date: 
Message-ID: <1146590078.496661.215790@g10g2000cwb.googlegroups.com>
I am very new to learning lisp and need help writing a recursive
function.  What I need to do is write a function that takes a "list of
lists" as an arguement and returns a list containing only the sublists.
 Any help would be appreciated.  Thanks!!

From: Bill Atkins
Subject: Re: Need lisp help!!!
Date: 
Message-ID: <8764kos52o.fsf@rpi.edu>
···········@gmail.com writes:

> I am very new to learning lisp and need help writing a recursive
> function.  What I need to do is write a function that takes a "list of
> lists" as an arguement and returns a list containing only the sublists.
>  Any help would be appreciated.  Thanks!!
>

Ask your professor.

-- 
This is a song that took me ten years to live and two years to write.
 - Bob Dylan
From: Pascal Bourguignon
Subject: Re: Need lisp help!!!
Date: 
Message-ID: <87aca0z5cr.fsf@thalassa.informatimago.com>
···········@gmail.com writes:
> I am very new to learning lisp and need help writing a recursive
> function.  What I need to do is write a function that takes a "list of
> lists" as an arguement and returns a list containing only the sublists.
>  Any help would be appreciated.  Thanks!!

Let me see if I understand. You want a function f such as:

 (f (list (list (list 1 2 3) (list 4 5) (list 6))
          (list (list 'a 'b) (list 'c 'd))
          (list (list "Hello" "world" "!"))))

return:

    ((1 2 3) (4 5) (6) (a b) (c d) ("Hello" "world" "!"))

?


                 
Let's generalize a little.  Would you mind if we accepted a list of
lists containing any kind of object instead of sublists?

 (f (list (list (list 1 2 3) (list 4 5) (list 6))
          (list 3.14 2.78 1.414 0.30103)
          (list (list 'a 'b) (list 'c 'd))
          (list 'something 'like 'this '?)
          (list (list "Hello" "world" "!"))))
-->
    ((1 2 3) (4 5) (6) 3.14 2.78 1.414 0.30103
     (a b) (c d)  something like this ?  ("Hello" "world" "!"))



Note that this is what you can get with REDUCE and APPEND:

[157]>  (reduce (function append)
                (list (list (list 1 2 3) (list 4 5) (list 6))
                      (list 3.14 2.78 1.414 0.30103)
                      (list (list 'a 'b) (list 'c 'd))
                      (list 'something 'like 'this '?)
                      (list (list "Hello" "world" "!")))
                :initial-value '())
((1 2 3) (4 5) (6) 3.14 2.78 1.414 0.30103 (A B) (C D) SOMETHING LIKE THIS ?
 ("Hello" "world" "!"))


So now, we have a prototype (thanks Common Lisp!):

(defun f-prototype (list-of-lists)
   (reduce (function append) list-of-lists :initial-value '()))


Ok, let's see how we could divide this function f.

First, we could divide the set of lists of lists into two classes, the
singleton containing the empty list of lists, and the others.

What would the result of (f '()) be?

Now, if the list of lists is not empty, how could we subdivise them
into two category?  What about if we consider the first sublist?

What would the result of (f (list '() ...)) be?

and when the first sublist is not empty, we could further subdivide
the sublists of one element and the sublists of more than one element:

What would the result of (f (list (list item) ...)) be?
What would the result of (f (list (list item ...) ...)) be?




If you don't know, let's get a hint from our prototype:

(mapcar (function f-prototype)
        '(   ()
             (() (a b c))
             ((1) (a b c))
             ((1 2 3) (a b c))
             ((1 2 3) (a b c) (1.1 2.2 3.3)) )) 


From each of these questions, you should extract two things:

- a condition that identify the case, an expression to compute the

- result in function of the list-of-lists, using the "primitives"
  functions, and possibly the function f itself, to the condition that
  the data you feed it recursively be "smaller" than the list-of-lists
  itself.





Note that when we use the special operator COND to write these
conditions in cascade, we can omit the "else" part of the succeding
conditions:

First we will have to cases:
      (null list-of-lists)
      (not (null list-of-lists))

The second case is subdivided:
      (and (not (null list-of-lists))  (null (first list-of-lists)))
      (and (not (null list-of-lists))  (not (null (first list-of-lists))))

which gives these three cases:

      (null list-of-lists)
      (and (not (null list-of-lists))  (null (first list-of-lists)))
      (and (not (null list-of-lists))  (not (null (first list-of-lists))))

But when we write the COND form, the following clauses are executed
only when the first condition is false, so we don't need to test it
again:

     (cond 
      ((null list-of-lists)         ...)
      ((null (first list-of-lists)) ...)
      (t                            ...))

-- 
__Pascal Bourguignon__                     http://www.informatimago.com/
You never feed me.
Perhaps I'll sleep on your face.
That will sure show you.
From: Frank Buss
Subject: Re: Need lisp help!!!
Date: 
Message-ID: <1kzsu9t4vzs6m.1d111iapazo09$.dlg@40tude.net>
···········@gmail.com wrote:

> I am very new to learning lisp and need help writing a recursive
> function.  What I need to do is write a function that takes a "list of
> lists" as an arguement and returns a list containing only the sublists.
>  Any help would be appreciated.  Thanks!!

This function is already defined in Common Lisp and it is called
"identity":

CL-USER > (identity '((1 2 3) (4 5) (6 7 8 9)))
((1 2 3) (4 5) (6 7 8 9))

The input is a "list of lists" and it returns a list, which contains only
the sublists of the input list.

But I don't know, if it is recursive, so here is a recursive version:

(defun identity-copy-recursive (list)
  (when list
    (cons (car list) (identity-copy-recursive (cdr list)))))

-- 
Frank Buss, ··@frank-buss.de
http://www.frank-buss.de, http://www.it4-systems.de
From: ···········@gmail.com
Subject: Re: Need lisp help!!!
Date: 
Message-ID: <1146595068.637577.154050@i40g2000cwc.googlegroups.com>
Frank, thanks for the help.  Let me try to explain further what I need
help with.  Given a list of "whatever" (eg. ((a b) 1 c (1 2 3) q)) what
the recursive function should return is just the "lists" in the given
list.  So ( (a b) (1 2 3)) would be returned.  Not 1, c , or q.

thanks!
From: William Bland
Subject: Re: Need lisp help!!!
Date: 
Message-ID: <pan.2006.05.02.18.47.47.587535@gmail.com>
On Tue, 02 May 2006 11:37:48 -0700, jasongalati wrote:

> Frank, thanks for the help.  Let me try to explain further what I need
> help with.  Given a list of "whatever" (eg. ((a b) 1 c (1 2 3) q)) what
> the recursive function should return is just the "lists" in the given
> list.  So ( (a b) (1 2 3)) would be returned.  Not 1, c , or q.
> 
> thanks!

Sounds like you want:

CL-USER> (remove-if-not #'listp '((a b) 1 c (1 2 3) q))
((A B) (1 2 3))

It's not recursive, but it is probably the more natural solution in
Common Lisp.  The recursive way isn't too difficult really and can be a
valuable learning exercise - good luck!

Cheers,
	Bill.
From: ···········@gmail.com
Subject: Re: Need lisp help!!!
Date: 
Message-ID: <1146595905.457186.78220@j73g2000cwa.googlegroups.com>
Bill, thanks for the advice.  Can you write your above example using
recursion?  That is what I'm trying to figure out how to do.

Thanks!
From: bradb
Subject: Re: Need lisp help!!!
Date: 
Message-ID: <1146596479.552470.201060@y43g2000cwc.googlegroups.com>
What you want to do is have a function (say, solve) that takes a list
as input.
If the list is nil, return nil
Otherwise if (listp (car list)) then return (append (car list) (solve
(cdr list)))
if the head of the list isn't a list, then just return (solve (cdr
list))

Hope that helps
Brad
From: ···········@gmail.com
Subject: Re: Need lisp help!!!
Date: 
Message-ID: <1146596839.546521.273510@v46g2000cwv.googlegroups.com>
Brad.. Does this look correct?

(DEFUN solve (lyst)
  (COND
    ((NULL lyst) NIL)
    ((> (listp (CAR lyst)) 0)
     (append (CAR lyst)
           (solve (CDR lyst))))
  (T (solve (CDR lyst)))
 ))

Thanks!
From: Pascal Bourguignon
Subject: Re: Need lisp help!!!
Date: 
Message-ID: <87y7xkxmkj.fsf@thalassa.informatimago.com>
···········@gmail.com writes:

> Brad.. Does this look correct?
>
> (DEFUN solve (lyst)
>   (COND
>     ((NULL lyst) NIL)
>     ((> (listp (CAR lyst)) 0)
>      (append (CAR lyst)
>            (solve (CDR lyst))))
>   (T (solve (CDR lyst)))
>  ))
>
> Thanks!

Not really.

Like this, it's a little better:

(DEFUN SOLVE (LIST)
  (COND
    ((NULL LIST)         '())
    ((LISTP (CAR LIST))  (APPEND (CAR LIST) (SOLVE (CDR LIST))))
    (T                   (SOLVE (CDR LIST)))))

Now, you should read the reference page for each of the standard
functions you use.  What does LISTP return?  This is not C. (And even
if it was C, C boolean true is any non 0 value, it can be negative!

The booleans are the symbols NIL and T. (But generalized booleans are
NIL and any non-NIL value).
NIL is also the empty list.

Since NIL can be used for four different things, we use four different
notations to denote the nuances:

NIL   boolean false.
'NIL  symbol NIL.
()    empty list in program source.
'()   empty data list.


-- 
__Pascal Bourguignon__                     http://www.informatimago.com/

What is this talk of 'release'? Klingons do not make software 'releases'.
Our software 'escapes' leaving a bloody trail of designers and quality
assurance people in it's wake.
From: ···········@gmail.com
Subject: Re: Need lisp help!!!
Date: 
Message-ID: <1146598368.816301.227780@i39g2000cwa.googlegroups.com>
Pascal,
You've been a great help!  I'm an EE trying to learn programming.  One
last thing.  I ran the function with ( (a b) c (1 2 3) d) and it
returned (a b 1 2 3).  How would I make the output look like ( (a b) (1
2 3) )??

Thanks again.
From: Ken Tilton
Subject: Re: Need lisp help!!!
Date: 
Message-ID: <2EO5g.15$8A2.11@fe10.lga>
···········@gmail.com wrote:
> Pascal,
> You've been a great help!  I'm an EE trying to learn programming.  One
> last thing.  I ran the function with ( (a b) c (1 2 3) d) and it
> returned (a b 1 2 3).  How would I make the output look like ( (a b) (1
> 2 3) )??
> 
You have to debug Pascal's code for him as a return favor. Look up 
APPEND for a headstart (it ain't what you want).

kenny

-- 
Cells: http://common-lisp.net/project/cells/

"Have you ever been in a relationship?"
    Attorney for Mary Winkler, confessed killer of her
    minister husband, when asked if the couple had
    marital problems.
From: Pascal Bourguignon
Subject: Re: Need lisp help!!!
Date: 
Message-ID: <87r73cxku0.fsf@thalassa.informatimago.com>
···········@gmail.com writes:

···········@gmail.com writes:

> Pascal,
> You've been a great help!  I'm an EE trying to learn programming.  One
> last thing.  I ran the function with ( (a b) c (1 2 3) d) and it
> returned (a b 1 2 3).  How would I make the output look like ( (a b) (1
> 2 3) )??

Try to substitute CONS for APPEND.  Why does it work with CONS?

-- 
__Pascal Bourguignon__                     http://www.informatimago.com/

This universe shipped by weight, not volume.  Some expansion may have
occurred during shipment.
From: ···········@gmail.com
Subject: Re: Need lisp help!!!
Date: 
Message-ID: <1146600224.515266.115940@g10g2000cwb.googlegroups.com>
CONS  takes two parameters, the first of which can be either an atom or
a list and the second of which is a list; returns a new list that
includes the first parameter as its first element and the second
parameter as the remainder of its result.
APPEND - takes two or more lists and concatenates them.

Thanks Pascal...and everyone else for your help.  This discussion can
be closed now.
From: Pascal Bourguignon
Subject: Re: Need lisp help!!!
Date: 
Message-ID: <873bfsz1s5.fsf@thalassa.informatimago.com>
···········@gmail.com writes:

> Frank, thanks for the help.  Let me try to explain further what I need
> help with.  Given a list of "whatever" (eg. ((a b) 1 c (1 2 3) q)) what
> the recursive function should return is just the "lists" in the given
> list.  So ( (a b) (1 2 3)) would be returned.  Not 1, c , or q.

Not quite what you specified first, is it?
Anyways, just apply the same methodology.
William already provided a prototype.

-- 
__Pascal Bourguignon__                     http://www.informatimago.com/

What is this talk of 'release'? Klingons do not make software 'releases'.
Our software 'escapes' leaving a bloody trail of designers and quality
assurance people in it's wake.
From: Frank Buss
Subject: Re: Need lisp help!!!
Date: 
Message-ID: <gvqfx5efmhrz.1rgialx8yfh81.dlg@40tude.net>
···········@gmail.com wrote:

> Frank, thanks for the help.  Let me try to explain further what I need
> help with.  Given a list of "whatever" (eg. ((a b) 1 c (1 2 3) q)) what
> the recursive function should return is just the "lists" in the given
> list.  So ( (a b) (1 2 3)) would be returned.  Not 1, c , or q.

No problem. First define a y-combinator to build a recursive function:

(defun y-combinator (fun)
  ((lambda (function)
     (funcall function function))
   (lambda (function)
     (funcall fun
              (lambda (argument)
                (funcall (funcall function function)
                         argument))))))

The y-combinator returns a function, which returns a function when called.
When this function is called, it calls the supplied function fun. The
argument to fun is a function, which is called with the argument on which
the function fun operates and which calls the function, which is returned
by calling the last function, with the supplied argument. For a recursion,
fun must call the supplied function. For your problem this could look like
this:

(defun only-lists-implementation (y-combinator)
  (lambda (list)
    (when list
      (destructuring-bind (car . cdr) list
        (if (listp car)
            (cons car (funcall y-combinator cdr))
          (funcall y-combinator cdr))))))

To start the recursion, create a y-combinator function with your function
and then call this function, with the argument on which your function
operates:

(defun only-lists (list)
  (funcall (y-combinator (function only-lists-operator)) list))

Then you can use it like this:

CL-USER > (only-lists '((a b) 1 c (1 2 3) q))
((A B) (1 2 3))

-- 
Frank Buss, ··@frank-buss.de
http://www.frank-buss.de, http://www.it4-systems.de
From: bradb
Subject: Re: Need lisp help!!!
Date: 
Message-ID: <1146601979.527695.67770@v46g2000cwv.googlegroups.com>
That's just plain mean :)

Brad
From: Tel A.
Subject: Re: Need lisp help!!!
Date: 
Message-ID: <1146612603.818951.84100@u72g2000cwu.googlegroups.com>
Bravo. I've never heard Y combinators explained in such a clear and
consise fashion.
From: Aravindh Johendran
Subject: Re: Need lisp help!!!
Date: 
Message-ID: <1146676096.179955.216010@i39g2000cwa.googlegroups.com>
Frank Buss wrote:

> No problem. First define a y-combinator to build a recursive function:
>
> The y-combinator returns a function, which returns a function when called.
> When this function is called, it calls the supplied function fun.


Tel A. wrote:
> Bravo. I've never heard Y combinators explained in such a clear and
> consise fashion.

Agreed. 
Thanks Frank. Had a gotcha moment.
From: Thomas A. Russ
Subject: Re: Need lisp help!!!
Date: 
Message-ID: <ymi4q05soos.fsf@sevak.isi.edu>
···········@gmail.com writes:

> I am very new to learning lisp and need help writing a recursive
> function.  What I need to do is write a function that takes a "list of
> lists" as an arguement and returns a list containing only the sublists.
>  Any help would be appreciated.  Thanks!!

Isn't that just the IDENTITY function?


But I suspect you want something specified a little bit differently.
Perhaps you could provide an example of inputs and outputs?

An even better way to get help is to show what you've already tried.
That will also result in much more effective learning on your part.

-- 
Thomas A. Russ,  USC/Information Sciences Institute

(defun s1 (l) (apply #'nconc l))