[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.lisp

Re: Improving coding style - lisp processing

William James

11/16/2015 11:00:00 AM

Pascal Costanza wrote:

> > I repeatedly find myself in trouble when I need to do list processing
> > that involves dealing with multiple values at once. For example,
> > consider a function that takes an argument list and removes a
> > particular keyword parameter:
> >
> > (remove-keyword-parameter '(1 :a 1 :b 2 :c 3) :b)
> > => (1 :a 1 :c 3)
> >
> > What's an elegant functional (and/or lispy) solution to this problem?
> > The solution I came up with is, IMO, very convoluted:
> >
> > (defun remove-keyword-parameter (parameter-list keyword)
> > (let (remove)
> > (loop for i in parameter-list
> > when (eql i keyword)
> > do (setf remove t)
> > else when remove
> > do (setf remove nil)
> > else unless remove
> > collect i)))
> >
> > Could someone suggest a better way?
>
> (loop for (key value) on plist by #'cddr
> unless (eql key keyword)
> nconc (list key value))

Really? Let's test.

(defun remove-keyword-parameter (plist keyword)
(loop for (key value) on plist by #'cddr
unless (eql key keyword)
nconc (list key value)))

(remove-keyword-parameter '(1 :a 1 :b 2 :c 3) :b)
===>
(1 :A 1 :B 2 :C 3 NIL)

Wow. It failed to remove :b and 2, and it added nil
at the end.

To fail that miserably requires CL and one of its worshippers.


MatzLisp (Ruby):

def remove_key_and_val( plist, key)
plist.slice_before{|x| x.is_a? Symbol}.reject{|k,v| k == key}.flatten
end

remove_key_and_val([1,:a,1,:b,2,:c,3], :b)
==>[1, :a, 1, :c, 3]

--
Viewed at its most abstract level, a fundamental agenda is thus to influence
the European-derived peoples of the United States to view concern about their
own demographic and cultural eclipse as irrational and as an indication of
psychopathology. --- Dr. Kevin MacDonald; "The Frankfurt School of Social
Research and the Pathologization of Gentile Group Allegiances"
1 Answer

Carlos

11/16/2015 1:25:00 PM

0

["WJ" <w_a_x_man@yahoo.com>, 2015-11-16 11:00]
> Wow. It failed to remove :b and 2, and it added nil
> at the end.
>
> To fail that miserably requires CL and one of its worshippers.
>
>
> MatzLisp (Ruby):
>
> def remove_key_and_val( plist, key)
> plist.slice_before{|x| x.is_a? Symbol}.reject{|k,v| k ==
> key}.flatten end
>
> remove_key_and_val([1,:a,1,:b,2,:c,3], :b)
> ==>[1, :a, 1, :c, 3]

def remove_key_and_val( plist, key)
plist.slice_before{|x| x.is_a? Symbol}.reject{|k,v| k == key}.flatten
end
=> :remove_key_and_val

remove_key_and_val([1, :first_symbol, :a, :second_symbol, :b, :third, :c], :b)
=> [1, :first_symbol, :a, :second_symbol, :third, :c]

OMG, total failure! That one only required WJ.

--