join N lists ?



hi Stavros
This cords is rewritten Barton's flatten.lisp.
So lists of sets  or sets of list  are almost  correct.
And  level error  is free. but it can not treat matrix.
(%i9) nflatten([[{a,b,c},d],[e,f]],2);
(%o9)                  [{a, b, c}, d, e, f]
(%i10) nflatten([[{a,b,c},d],{e,f}],2);
(%o10)                 [{a, b, c}, d, {e, f}]
(%i11) nflatten({[{a,b,c},d],[e,f]},2);
(%o11)                {[{a, b, c}, d], [e, f]}
(%i12) nflatten({[{a,b,c},d],[e,f]},3);
(%o12)                {[{a, b, c}, d], [e, f]}
(%i13) nflatten({{{a,b,c},d},[e,f]},2);
(%o13)                  {a, b, c, d, [e, f]}

gosei furuya

(defun $nflatten (e &optional (n 5))
  (setq e (ratdisrep e))
  (cond ((or ($atom e) ($subvarp e)(or (member ($inpart e 0) (list '&^
'&=))))
         e)
        (t
         (let ((op (multiple-value-list (get-op-and-arg e))))
           (setq e (cadr op))
           (setq op (car op))
           (setq e (mapcar #'(lambda (x) (flatten-op x op n)) e))
           (setq e (reduce #'append e))
           (cond ((and (consp (car op)) (eq (caar op) 'mqapply))
                  (append op e))
                 (t
                  `(,op , at e)))))))

(defun flatten-op (e op nlev)
  (let ((e-op) (e-arg))
    (setq e-op (multiple-value-list (get-op-and-arg e)))
    (setq e-arg (cadr e-op))
    (setq e-op (car e-op))
    (cond ((and (>= nlev 1)(equal e-op op))
           (mapcan #'(lambda (x) (flatten-op x op (1- nlev))) e-arg))
          (t
           (list e)))))





2007/6/4, Stavros Macrakis <macrakis at alum.mit.edu>:
>
> On 6/3/07, Gosei Furuya <go.maxima at gmail.com> wrote:
> >
> > I would like to use flatten with outermap,
> > I wrote newflatten dunction.
> >
> > (%i4) load("newflatten.lisp");
> > (%o4)                     newflatten.lisp
> > (%i5) nflatten([[[a,b,c],d],[e,f]],1);
> > (%o5)                  [[a, b, c], d, e, f]
> > (%i6) nflatten([[[a,b,c],d],[e,f]]); /*default level 3*/
> > (%o6)                   [a, b, c, d, e, f]
> > (%i7) nflatten([[[a,b,c],d],[e,f]],2);
> > (%o7)                   [a, b, c, d, e, f]
> > how do you think?
> >
>
> I like the idea of specifying how many levels.  In particular, this means
> you could give a useful error message in cases where N levels aren't
> present, which will, I suspect, catch a lot of errors -- but I see from %o6
> that you do not do this.  I would have thought that the intuition of
> nflatten was that you know a priori that the object consists precisely of
> nested lists to N levels.  Also, I wonder if nflatten works only for the "["
> operator, or for other operators as well, and for mixed operators, e.g.
> sets of lists or lists of sets.  If so, is the result a list or a set?
>
>               -s
>