Function: ntake

ntake is a function defined in fns.c.

Signature

(ntake N LIST)

Documentation

Modify LIST to keep only the first N elements.

If N is zero or negative, return nil. If N is greater or equal to the length of LIST, return LIST unmodified. Otherwise, return LIST after truncating it.

Other relevant functions are documented in the list group.

View in manual

Probably introduced at or before Emacs version 29.1.

Shortdoc

;; list
(ntake 3 (list 'one 'two 'three 'four))
    => (one two three)

Aliases

tramp-compat-ntake

Source Code

// Defined in /usr/src/emacs/src/fns.c
{
  EMACS_INT m;
  if (FIXNUMP (n))
    {
      m = XFIXNUM (n);
      if (m <= 0)
	return Qnil;
    }
  else if (BIGNUMP (n))
    {
      if (mpz_sgn (*xbignum_val (n)) < 0)
	return Qnil;
      m = MOST_POSITIVE_FIXNUM;
    }
  else
    wrong_type_argument (Qintegerp, n);
  CHECK_LIST (list);
  Lisp_Object tail = list;
  --m;
  while (m > 0 && CONSP (tail))
    {
      tail = XCDR (tail);
      m--;
    }
  if (CONSP (tail))
    XSETCDR (tail, Qnil);
  else if (!NILP (tail))
    wrong_type_argument (Qlistp, list);
  return list;
}