From: David Brynjar Franzson
Subject: help with translation from python to lisp
Date: 
Message-ID: <aqpcqo$7ni$1@news.Stanford.EDU>
Hi, I realize that this kind of idiotic posts are probably not the most
popular here, but the thing is that I am in a terrible situation. Being a
composer, I am forced to use Patchwork, a lisp based graphic composition
environment. I am used to do all my programming in Python, and can easily
build all my functions there. The function I need to translate is as
follows:

def splitlist(list, barlength):
    tmplist=[]
    mainlist=[]
    intcount=0
    for i in range (0, len(list)):
        if intcount>=barlength:
            intcount=intcount%barlength
            if intcount==0:
                mainlist.append(tmplist)
                tmplist=[]
            else:
                tmplist[len(tmplist)-1]=list[i-1]-intcount
                mainlist.append(tmplist)
                tmplist=[]
                tmplist.append(-1*abs(intcount))
        tmplist.append(list[i])
        intcount+=abs(list[i])
    mainlist.append(tmplist)
    return mainlist

where the list is a list of numerical values, and the barlength is the size
of fragments that I need to get out of the list. E.G. if I feed it the list
1,2,3,4,1,2,3,4 and the barlength 8, it will return (1,2,3,2)(-2, 1,
3,1)(-3....; the - is the leftover of the value 4. If anybody is willing to
tell me how to go about translating this function, I'll be forever
greatefull...
Sincerely,
David Brynjar Franzson
········@stanford.edu

From: Erann Gat
Subject: Re: help with translation from python to lisp
Date: 
Message-ID: <gat-1111021625150001@k-137-79-50-101.jpl.nasa.gov>
In article <············@news.Stanford.EDU>, "David Brynjar Franzson"
<········@stanford.edu> wrote:

> Hi, I realize that this kind of idiotic posts are probably not the most
> popular here, but the thing is that I am in a terrible situation. Being a
> composer, I am forced to use Patchwork, a lisp based graphic composition
> environment. I am used to do all my programming in Python, and can easily
> build all my functions there. The function I need to translate is as
> follows:
> 
> def splitlist(list, barlength):
>     tmplist=[]
>     mainlist=[]
>     intcount=0
>     for i in range (0, len(list)):
>         if intcount>=barlength:
>             intcount=intcount%barlength
>             if intcount==0:
>                 mainlist.append(tmplist)
>                 tmplist=[]
>             else:
>                 tmplist[len(tmplist)-1]=list[i-1]-intcount
>                 mainlist.append(tmplist)
>                 tmplist=[]
>                 tmplist.append(-1*abs(intcount))
>         tmplist.append(list[i])
>         intcount+=abs(list[i])
>     mainlist.append(tmplist)
>     return mainlist
> 
> where the list is a list of numerical values, and the barlength is the size
> of fragments that I need to get out of the list. E.G. if I feed it the list
> 1,2,3,4,1,2,3,4 and the barlength 8, it will return (1,2,3,2)(-2, 1,
> 3,1)(-3....; the - is the leftover of the value 4.

Here's a more-or-less literal translation into Common Lisp:

(defun splitlist (list barlength)
  (let ( (tmplist '())
         (mainlist '())
         (intcount 0) )
    (dotimes (i (length list))
      (if (>= intcount barlength)
        (progn
          (setf intcount (mod intcount barlength))
          (if (zerop intcount)
            (progn
              (setf mainlist (append mainlist (list tmplist)))
              (setf tmplist '()))
            (progn
              (setf (first (last tmplist)) (- (nth (1- i) list) intcount))
              (setf mainlist (append mainlist (list tmplist)))
              (setf tmplist (list (- (abs intcount))))))))
      (setf tmplist (append tmplist (list (nth i list))))
      (incf intcount (abs (nth i list))))
    (setf mainlist (append mainlist (list tmplist)))
    mainlist))

Here's a simpler version that uses the same basic algorithm but uses some
of Lisp's features and conventional programming style to simplify the
code:

(defun splitlist (list barlength)
  (let ( (tmplist '())
         (mainlist '())
         (intcount 0) )
    (dolist (item list)
      (when (>= intcount barlength)
        (setf intcount (mod intcount barlength))
        (if (zerop intcount)
          (progn
            (push (nreverse tmplist) mainlist)
            (setf tmplist '()))
          (progn
            (decf (first tmplist) intcount)
            (push (nreverse tmplist) mainlist)
            (setf tmplist (list (- intcount))))))
      (push item tmplist)
      (incf intcount item))
    (push tmplist mainlist)
    (nreverse mainlist)))

And here's a really Lispy version:

(defun splitlist1 (list barlength &optional (leftover barlength))
  (cond
   ( (null list) '(()) )
   ( (zerop leftover)
     (list (splitlist1 list barlength barlength)) )
   ( (< leftover 0)
     (list (cons leftover (splitlist1 (cdr list) barlength
                                      (+ barlength leftover)))) )
   ( (>= leftover (first list))
     (cons (first list) (splitlist1 (cdr list) barlength
                                    (- leftover (first list)))) )
   (t (cons leftover (splitlist1 list barlength (- leftover (first list)))))))

(defun splitlist2 (l)
  (and l (cons (butlast l) (splitlist2 (first (last l))))))

(defun splitlist (list barlength) (splitlist2 (splitlist1 list barlength)))



NOTE: This code has been tested on only one test case.  It may be buggy.

E.
From: David Brynjar Franzson
Subject: Re: help with translation from python to lisp
Date: 
Message-ID: <aqpma6$eec$1@news.Stanford.EDU>
Thank you so much, this just saved my life :) Hope I can figure out what is
happening in there at some point, but for now it is great to have something
that actually works...
db
"Erann Gat" <···@jpl.nasa.gov> wrote in message
·························@k-137-79-50-101.jpl.nasa.gov...
> In article <············@news.Stanford.EDU>, "David Brynjar Franzson"
> <········@stanford.edu> wrote:
>
> > Hi, I realize that this kind of idiotic posts are probably not the most
> > popular here, but the thing is that I am in a terrible situation. Being
a
> > composer, I am forced to use Patchwork, a lisp based graphic composition
> > environment. I am used to do all my programming in Python, and can
easily
> > build all my functions there. The function I need to translate is as
> > follows:
> >
> > def splitlist(list, barlength):
> >     tmplist=[]
> >     mainlist=[]
> >     intcount=0
> >     for i in range (0, len(list)):
> >         if intcount>=barlength:
> >             intcount=intcount%barlength
> >             if intcount==0:
> >                 mainlist.append(tmplist)
> >                 tmplist=[]
> >             else:
> >                 tmplist[len(tmplist)-1]=list[i-1]-intcount
> >                 mainlist.append(tmplist)
> >                 tmplist=[]
> >                 tmplist.append(-1*abs(intcount))
> >         tmplist.append(list[i])
> >         intcount+=abs(list[i])
> >     mainlist.append(tmplist)
> >     return mainlist
> >
> > where the list is a list of numerical values, and the barlength is the
size
> > of fragments that I need to get out of the list. E.G. if I feed it the
list
> > 1,2,3,4,1,2,3,4 and the barlength 8, it will return (1,2,3,2)(-2, 1,
> > 3,1)(-3....; the - is the leftover of the value 4.
>
> Here's a more-or-less literal translation into Common Lisp:
>
> (defun splitlist (list barlength)
>   (let ( (tmplist '())
>          (mainlist '())
>          (intcount 0) )
>     (dotimes (i (length list))
>       (if (>= intcount barlength)
>         (progn
>           (setf intcount (mod intcount barlength))
>           (if (zerop intcount)
>             (progn
>               (setf mainlist (append mainlist (list tmplist)))
>               (setf tmplist '()))
>             (progn
>               (setf (first (last tmplist)) (- (nth (1- i) list) intcount))
>               (setf mainlist (append mainlist (list tmplist)))
>               (setf tmplist (list (- (abs intcount))))))))
>       (setf tmplist (append tmplist (list (nth i list))))
>       (incf intcount (abs (nth i list))))
>     (setf mainlist (append mainlist (list tmplist)))
>     mainlist))
>
> Here's a simpler version that uses the same basic algorithm but uses some
> of Lisp's features and conventional programming style to simplify the
> code:
>
> (defun splitlist (list barlength)
>   (let ( (tmplist '())
>          (mainlist '())
>          (intcount 0) )
>     (dolist (item list)
>       (when (>= intcount barlength)
>         (setf intcount (mod intcount barlength))
>         (if (zerop intcount)
>           (progn
>             (push (nreverse tmplist) mainlist)
>             (setf tmplist '()))
>           (progn
>             (decf (first tmplist) intcount)
>             (push (nreverse tmplist) mainlist)
>             (setf tmplist (list (- intcount))))))
>       (push item tmplist)
>       (incf intcount item))
>     (push tmplist mainlist)
>     (nreverse mainlist)))
>
> And here's a really Lispy version:
>
> (defun splitlist1 (list barlength &optional (leftover barlength))
>   (cond
>    ( (null list) '(()) )
>    ( (zerop leftover)
>      (list (splitlist1 list barlength barlength)) )
>    ( (< leftover 0)
>      (list (cons leftover (splitlist1 (cdr list) barlength
>                                       (+ barlength leftover)))) )
>    ( (>= leftover (first list))
>      (cons (first list) (splitlist1 (cdr list) barlength
>                                     (- leftover (first list)))) )
>    (t (cons leftover (splitlist1 list barlength (- leftover (first
list)))))))
>
> (defun splitlist2 (l)
>   (and l (cons (butlast l) (splitlist2 (first (last l))))))
>
> (defun splitlist (list barlength) (splitlist2 (splitlist1 list
barlength)))
>
>
>
> NOTE: This code has been tested on only one test case.  It may be buggy.
>
> E.
From: Erann Gat
Subject: Re: help with translation from python to lisp
Date: 
Message-ID: <gat-1111021807080001@192.168.1.51>
In article <············@news.Stanford.EDU>, "David Brynjar Franzson"
<········@stanford.edu> wrote:

> Thank you so much, this just saved my life :)

Does that mean you're my slave now?  :-)

> Hope I can figure out what is happening in there at some point,

Please do try.  You'll find it most rewarding.

E.
From: David Brynjar Franzson
Subject: Re: help with translation from python to lisp
Date: 
Message-ID: <aqpu5g$ipc$1@news.Stanford.EDU>
Thanks again, I just realized one problem. In the python version I used abs
to have the possibility of using negative numbers in the input, and still
advance the counters (due to the music software using negative numbers to
indicate rests). This means that an input list of -1 1 2 3 4  is read as 1 1
2 3 4 with regards to the overall number in the group, but shown as a
negative number in the output. It seems to work if I do this, is this the
correct way of doing this?

(defun splitlist (list barlength)
  (let ( (tmplist '())
         (mainlist '())
         (intcount 0) )
    (dolist (item list)
      (when (>= intcount barlength)
        (setf intcount (mod intcount barlength))
        (if (zerop intcount)
          (progn
            (push (nreverse tmplist) mainlist)
            (setf tmplist '()))
          (progn
            (decf (first tmplist) intcount)
            (push (nreverse tmplist) mainlist)
            (setf tmplist (list (- intcount))))))
      (push item tmplist)
      (incf intcount (abs item)))        <-- Would this do the trick?
    (push tmplist mainlist)
    (nreverse mainlist)))

db

"Erann Gat" <···@jpl.nasa.gov> wrote in message
·························@192.168.1.51...
> In article <············@news.Stanford.EDU>, "David Brynjar Franzson"
> <········@stanford.edu> wrote:
>
> > Thank you so much, this just saved my life :)
>
> Does that mean you're my slave now?  :-)
>
> > Hope I can figure out what is happening in there at some point,
>
> Please do try.  You'll find it most rewarding.
>
> E.
From: Erann Gat
Subject: Re: help with translation from python to lisp
Date: 
Message-ID: <gat-1111022046360001@192.168.1.51>
In article <············@news.Stanford.EDU>, "David Brynjar Franzson"
<········@stanford.edu> wrote:

> Thanks again, I just realized one problem. In the python version I used abs
> to have the possibility of using negative numbers in the input, and still
> advance the counters (due to the music software using negative numbers to
> indicate rests). This means that an input list of -1 1 2 3 4  is read as 1 1
> 2 3 4 with regards to the overall number in the group, but shown as a
> negative number in the output. It seems to work if I do this, is this the
> correct way of doing this?

(abs x) is the right way to take the absolute value of X.  Whether that
does the right thing in your algorithm you'll have to figure out on your
own.

E.
From: Kenny Tilton
Subject: Re: help with translation from python to lisp
Date: 
Message-ID: <3DD06F9B.5060605@nyc.rr.com>
Erann Gat wrote:
> In article <············@news.Stanford.EDU>, "David Brynjar Franzson"
> <········@stanford.edu> wrote:
> 
> 
>>Thank you so much, this just saved my life :)
> 
> 
> Does that mean you're my slave now?  :-)

It means you are responsible for everything David does for the rest of 
his life.

:)

-- 

  kenny tilton
  clinisys, inc
  ---------------------------------------------------------------
""Well, I've wrestled with reality for thirty-five years, Doctor,
   and I'm happy to state I finally won out over it.""
                                                   Elwood P. Dowd