How to resolve the algorithm Associative array/Iteration step by step in the Common Lisp programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Associative array/Iteration step by step in the Common Lisp programming language
Table of Contents
Problem Statement
Also show how to iterate just over the keys, or the values, if there is a separate way to do that in your language.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Associative array/Iteration step by step in the Common Lisp programming language
Source code in the common programming language
;; iterate using dolist, destructure manually
(dolist (pair alist)
(destructuring-bind (key . value) pair
(format t "~&Key: ~a, Value: ~a." key value)))
;; iterate and destructure with loop
(loop for (key . value) in alist
do (format t "~&Key: ~a, Value: ~a." key value))
(loop for (key value) on plist :by 'cddr
do (format t "~&Key: ~a, Value: ~a." key value))
(maphash (lambda (key value)
(format t "~&Key: ~a, Value: ~a." key value))
hash-table)
(loop for key being each hash-key of hash-table using (hash-value value)
do (format t "~&Key: ~a, Value: ~a." key value))
(with-hash-table-iterator (next-entry hash-table)
(loop
(multiple-value-bind (nextp key value) (next-entry)
(if (not nextp)
(return)
(format t "~&Key: ~a, Value: ~a." key value)))))
;; Project : Associative array/Iteration
(setf x (make-array '(3 2)
:initial-contents '(("hello" 13 ) ("world" 31) ("!" 71))))
(setf xlen (array-dimensions x))
(setf len (car xlen))
(dotimes (n len)
(terpri)
(format t "~a" (aref x n 0))
(format t "~a" " : ")
(format t "~a" (aref x n 1)))
You may also check:How to resolve the algorithm String length step by step in the Wren programming language
You may also check:How to resolve the algorithm LZW compression step by step in the Dylan programming language
You may also check:How to resolve the algorithm Guess the number step by step in the Icon and Unicon programming language
You may also check:How to resolve the algorithm Strip whitespace from a string/Top and tail step by step in the Zoea programming language
You may also check:How to resolve the algorithm GUI enabling/disabling of controls step by step in the Python programming language