How to resolve the algorithm Search a list step by step in the Common Lisp programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Search a list step by step in the Common Lisp programming language

Table of Contents

Problem Statement

Find the index of a string (needle) in an indexable, ordered collection of strings (haystack). Raise an exception if the needle is missing. If there is more than one occurrence then return the smallest index to the needle. Return the largest index to a needle that has multiple occurrences in the haystack.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Search a list step by step in the Common Lisp programming language

Source code in the common programming language

(let ((haystack '(Zig Zag Wally Ronald Bush Krusty Charlie Bush Bozo)))
  (dolist (needle '(Washington Bush))
    (let ((index (position needle haystack)))
      (if index
          (progn (print index) (princ needle))
          (progn (print needle) (princ "is not in haystack"))))))


CL-USER> (defparameter *list* '(Zig Zag Wally Ronald Bush Krusty Charlie Bush Bozo))
*LIST*
CL-USER> (position 'Bush *list*)
4
CL-USER> (position 'Bush *list* :from-end t)
7
CL-USER> (position 'Washington *list*)
NIL


  

You may also check:How to resolve the algorithm Determine if a string has all the same characters step by step in the Phix programming language
You may also check:How to resolve the algorithm Strip block comments step by step in the C# programming language
You may also check:How to resolve the algorithm Binary digits step by step in the Emacs Lisp programming language
You may also check:How to resolve the algorithm Priority queue step by step in the Ada programming language
You may also check:How to resolve the algorithm Loops/For step by step in the Sidef programming language