• Re: need help with lists

    From B. Pym@Nobody447095@here-nor-there.org to comp.lang.lisp on Mon Aug 4 12:28:22 2025
    From Newsgroup: comp.lang.lisp

    Slobodan Blazeski wrote:

    (defun rob-st-amant (list)
    (loop for element in list
    if (plusp element)
    collect element into positives
    else if (minusp element)
    collect (abs element) into minuses
    else collect element into zeroes
    finally (return (list positives zeroes minuses))))

    Given a list of integers, segregate them according to
    whether they are negative, positive, or zero.
    The absolute values of the negative numbers will be saved.

    The function shown above is the fastest one that he tested.


    (defconstant *cll* '(-2 0 -4 8 -6 35 0 42 -26 75 -88 92))


    Checking output:

    (rob-st-amant *cll*)
    ===>
    ((8 35 42 75 92) (0 0) (2 4 6 26 88))

    Using SBCL.

    Timing (fastest run shown here):

    (time (dotimes (i 1000000) (rob-st-amant *cll*)))

    0.562 seconds of real time
    0.562500 seconds of total run time (0.562500 user, 0.000000 system)
    [Run times consist of 0.031 seconds GC time, and 0.532 seconds non-GC time.] 100.00% CPU
    1,038,368,540 processor cycles
    143,998,192 bytes consed



    (defun b-pym (seq)
    (do ((pos '())
    (zer '())
    (neg '()))
    ((not seq) (list pos zer neg))
    (let ((e (pop seq)))
    (cond ((minusp e) (push (abs e) neg))
    ((plusp e) (push e pos))
    (t (push e zer))))))

    Checking output:

    (b-pym *cll*)
    ===>
    ((92 75 42 35 8) (0 0) (88 26 6 4 2))


    Timing (fastest run shown here):

    (time (dotimes (i 1000000) (b-pym *cll*)))

    0.391 seconds of real time
    0.390625 seconds of total run time (0.390625 user, 0.000000 system)
    100.00% CPU
    716,289,211 processor cycles
    119,997,064 bytes consed
    --
    [T]he problem is that lispniks are as cultish as any other devout group and basically fall down frothing at the mouth if they see [heterodoxy].
    --- Kenny Tilton
    The good news is, it's not Lisp that sucks, but Common Lisp. --- Paul Graham --- Synchronet 3.21a-Linux NewsLink 1.2
  • From B. Pym@Nobody447095@here-nor-there.org to comp.lang.lisp on Tue Aug 5 04:39:24 2025
    From Newsgroup: comp.lang.lisp

    B. Pym wrote:

    Slobodan Blazeski wrote:

    (defun rob-st-amant (list)
    (loop for element in list
    if (plusp element)
    collect element into positives
    else if (minusp element)
    collect (abs element) into minuses
    else collect element into zeroes
    finally (return (list positives zeroes minuses))))

    Given a list of integers, segregate them according to
    whether they are negative, positive, or zero.
    The absolute values of the negative numbers will be saved.

    The function shown above is the fastest one that he tested.


    (defconstant cll '(-2 0 -4 8 -6 35 0 42 -26 75 -88 92))


    Checking output:

    (rob-st-amant cll)
    ===>
    ((8 35 42 75 92) (0 0) (2 4 6 26 88))

    Using SBCL.

    Timing (fastest run shown here):

    (time (dotimes (i 1000000) (rob-st-amant cll)))

    0.562 seconds of real time
    0.562500 seconds of total run time (0.562500 user, 0.000000 system)
    [Run times consist of 0.031 seconds GC time, and 0.532 seconds non-GC time.] 100.00% CPU
    1,038,368,540 processor cycles
    143,998,192 bytes consed



    (defun b-pym (seq)
    (do ((pos '())
    (zer '())
    (neg '()))
    ((not seq) (list pos zer neg))
    (let ((e (pop seq)))
    (cond ((minusp e) (push (abs e) neg))
    ((plusp e) (push e pos))
    (t (push e zer))))))

    Checking output:

    (b-pym cll)
    ===>
    ((92 75 42 35 8) (0 0) (88 26 6 4 2))


    Timing (fastest run shown here):

    (time (dotimes (i 1000000) (b-pym cll)))

    0.391 seconds of real time
    0.390625 seconds of total run time (0.390625 user, 0.000000 system)
    100.00% CPU
    716,289,211 processor cycles
    119,997,064 bytes consed

    Shorter.

    (defun b-pym (seq)
    (do (pos zer neg)
    ((not seq) (list pos zer neg))
    (let ((e (pop seq)))
    (cond ((minusp e) (push (abs e) neg))
    ((plusp e) (push e pos))
    (t (push e zer))))))
    --
    [T]he problem is that lispniks are as cultish as any other devout group and basically fall down frothing at the mouth if they see [heterodoxy].
    --- Kenny Tilton
    The good news is, it's not Lisp that sucks, but Common Lisp. --- Paul Graham --- Synchronet 3.21a-Linux NewsLink 1.2
  • From B. Pym@Nobody447095@here-nor-there.org to comp.lang.lisp,comp.lang.scheme on Thu Jul 3 17:26:33 2025
    From Newsgroup: comp.lang.lisp

    B. Pym wrote:

    Ken Tilton wrote:

    Mark Tarver wrote:
    On 21 Oct, 10:15, Dan Kruchinin <just.asg...@gmail.com> wrote:

    On Oct 21, 1:07 pm, Dan Kruchinin <just.asg...@gmail.com> wrote:






    may be it's quite stupid question, but i don't know how to get more beautiful solution than i have.
    so the question is:
    i have a list always containing 2 integer elements. each element can be negative, positive or zero.
    i need to get a list containing 3 lists using the following strategy: if an element from source list is negative, it should be included to the first sublist,
    if it is positive, it should be placed to the third sublist
    and if it is zero, it should be placed to the second sublist. (note#1: each element should be abs'ed before including)
    (note#2: i need side-effect safe solution)

    for example i have a list from 2 elements: (setf l '(-2 35))
    i should get the following list containing 3 sublists:
    '((2) nil (35))
    or if l is '(-2 -3)
    the result list should be
    '((2 3) nil nil)
    or if l is '(0 5):
    '(nil (0) (5))
    and so on.

    uff, sorry, i made an error in the description
    if the element is negative it should be included to the third
    sublist
    and if it is positive it should be included to the first sublist.
    so the examples will be:
    src: '(-2 35)
    res: '((35) nil (2))
    --
    src: '(-2 -3)
    res: '(nil nil (2 3))
    --
    src :'(0 5)
    res: ((5) (0) nil)- Hide quoted text -

    - Show quoted text -


    Dan,

    You need to learn about accumulators and help functions.
    Probably somebody here can recommend an introductory book.

    In Qi

    (define seperate
    Ns -> (s-help Ns [] [] []))

    Pos Zero Neg are accumulators

    (define s-help
    [] Pos Zero Neg -> [Pos Zero Neg]
    [P | Ns] Pos Zero Neg -> (s-help Ns [P | Pos] Zero Neg) where (> P
    0)
    [0 | Ns] Pos Zero Neg -> (s-help Ns Pos [0 | Zero] Neg)
    [N | Ns] Pos Zero Neg -> (s-help Ns Pos Zero [N | Neg]))

    or in Lisp

    (defun seperate (Ns) (s-help Ns () () ()))

    (defun s-help (Ns Pos Zero Neg)
    (cond ((null Ns) (list Pos Zero Neg))
    ((> (car Ns) 0) (s-help (cdr Ns) (cons (car Ns) Pos) Zero
    Neg))
    ((zerop (car Ns)) (s-help (cdr Ns) Pos (cons 0 Zero) Neg))
    (t (s-help (cdr Ns) Pos Zero (cons (car Ns) Neg)))))

    I'm typing this wondering if its a homework question - but you've had
    a bash yourself so a bit of help is ok.

    The "ugly" solution was supplied as part of the question, with the instructions to find something more elegant.

    Fortunately you answered in Qi, I am hard at work on a solution using Cells, and I wonder where is the F# answer?

    Gauche Scheme

    (use gauche.collection)
    (use srfi-1)

    (define (group nums)
    (let1 res (group-collection nums :key (cut compare <> 0))
    (map! abs (last res))
    res))

    (group '(0 2 4 6 8 -3 -5 -7))
    ===>
    ((0) (2 4 6 8) (3 5 7))

    Scheme

    (define (triplet n)
    (cond ((zero? n) '(() (0) ()))
    ((negative? n) `(() () (,(- n))))
    (#t `((,n) () ()))))

    (define (separate ns)
    (apply
    map
    append
    (map triplet ns)))

    (separate '(-9 0 2 -8 0 3 4 5 -7 -6))
    ===>
    ((2 3 4 5) (0 0) (9 8 7 6))


    --- Synchronet 3.21d-Linux NewsLink 1.2