Deutsch   English   Français   Italiano  
<valq57$366nu$1@dont-email.me>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!feeds.phibee-telecom.net!2.eu.feeder.erje.net!feeder.erje.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: "B. Pym" <Nobody447095@here-nor-there.org>
Newsgroups: comp.lang.lisp,comp.lang.scheme
Subject: Re: need help with lists
Date: Wed, 28 Aug 2024 00:14:37 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 104
Message-ID: <valq57$366nu$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Injection-Date: Wed, 28 Aug 2024 02:14:37 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="bce2bfa34fe9e2220c0dcf18ffa540e6";
	logging-data="3349246"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1/hnx1o7eA7WDBf7tnmkqwR"
User-Agent: XanaNews/1.18.1.6
Cancel-Lock: sha1:2ErEvGrrRm2/dmA75tSUxtx6fZ0=
Bytes: 4081

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