help with translation from python to lisp 
Author Message
 help with translation from python to lisp

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



Sat, 30 Apr 2005 07:00:39 GMT  
 help with translation from python to lisp

Quote:

> 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.



Sat, 30 Apr 2005 08:25:15 GMT  
 help with translation from python to lisp
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

Quote:


> > 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.



Sat, 30 Apr 2005 09:42:24 GMT  
 help with translation from python to lisp

Quote:

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

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

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

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

E.



Sat, 30 Apr 2005 10:06:24 GMT  
 help with translation from python to lisp

Quote:



>>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



Sat, 30 Apr 2005 11:00:31 GMT  
 help with translation from python to lisp
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


Quote:


> > 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.



Sat, 30 Apr 2005 11:56:26 GMT  
 help with translation from python to lisp

Quote:

> 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.



Sat, 30 Apr 2005 12:45:49 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. New Common Lisp, Lisp-to-C translation, Lisp library for C

2. Lisp to Python translation criticism?

3. python to lisp translation

4. UCI lisp -> commmon lisp translation

5. Animation help: Translation->Rotation->Translation

6. translation from Common-Lisp to Scheme

7. Common-lisp to Scheme translation

8. Scheme to Common Lisp Translation

9. LISP to Prolog translation

10. Scheme to Common Lisp Translation

11. Translation of Symbolics Lisp Machine Backup Tape

12. lisp-to-c translation

 

 
Powered by phpBB® Forum Software