Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function that is always evaluated to t (or any value)

Say, we would like to use a function that requires a predicate, but for some reason we're interested in other features of the function (like :start and :end parameters), so we need to supply a predicate, that always returns true.

Obviously, it's not a problem at all:

CL-USER> (defparameter *list* '(0 1 2 3 4 5))
*LIST*
CL-USER> (remove-if (lambda (x) t) *list* :start 1 :end 3)
(0 3 4 5)

Works, but not beautifully at all. And we may get ugly message that variable x is not used. Since I'm in LISP for beauty, I'm curious if there is a 'always t' predicate?

We can define it:

(defun tp (&rest rest)
  (declare (ignore rest))
  t)

..but may be it exists?

like image 833
Mark Karpov Avatar asked Sep 02 '25 04:09

Mark Karpov


1 Answers

You're looking for the function constantly that takes an argument and returns a function that always returns that value. The predicate you need, then is (constantly t). Thus:

CL-USER> (remove-if (constantly t) '(0 1 2 3 4 5) :start 1 :end 3)
(0 3 4 5)

The notes on constantly show that you were absolutely on the right track with your proposed implementation. (You did even better, though, by adding the (declare (ignore …)).)

Notes:

constantly could be defined by:

(defun constantly (object)
  #'(lambda (&rest arguments) object))

After writing this, it occurred to me that this might be a duplicate. I didn't find a proper duplicate, found a similar, more specific, question that was looking to remove a single element at a position, Is there a common lisp macro for popping the nth element from a list?, in which Rainer Joswig's answer includes:

Removing the nth element of a list:

(defun remove-nth (list n)
  (remove-if (constantly t) list :start n :end (1+ n)))

This is really just a generalization of that approach, since you're working with arbitrary sequence boundaries. Thus we could have (making the boundary argument analogous to subseq's):

(defun remove-subseq (sequence &optional (start 0) end)
  (remove-if (constantly t) sequence :start start :end end))

(defun remove-nth (sequence n)
  (remove-subseq sequence n (1+ n)))
CL-USER> (remove-subseq '(0 1 2 3 4 5) 1 3)
(0 3 4 5)
CL-USER> (remove-nth '(0 1 2 3 4 5) 3)
(0 1 2 4 5)
like image 180
Joshua Taylor Avatar answered Sep 05 '25 01:09

Joshua Taylor