how to find element of list by knowing its position in clisp? - lisp

If I know the position of an element in a list in clisp, then how could I retrieve the element knowing its position. Is there any predefined function for it?

For lists only there is NTH:
CL-USER> (nth 2 '(1 2 3 4 5))
3
For SEQUENCES (vectors, strings, lists ...) there is ELT:
CL-USER> (elt '(1 2 3 4 5) 2)
3
If you really need a lot to access element by index, I'll advice you to consider using vectors (and access elements by aref) instead of lists, especially if you have logn sequences, because accessing element by index in lists may need to travel along all list to your element.
Of course, if you have small amount of data, you wan't feel any difference, but it looks good to use things right for me.

I depends on your lisp flavour. The best thing is to just write a 2 parameter function which returns a list-item. Parameter 1 item_index, param 2 the list, just recursively reduce index as you move through and return the 0th of 1st index. Note you need to decide if the car of a list is index 1 or index 0. Humans prefer one,computers 0.

Related

Lisp, calculating mixed expressions

i'am currently trying lisp and i need to solve a Problem. I want to write a function that takes a list as input and returns the calculated number. The elements that are not numbers should be added at the end of the list. Its important that the caluclated number is in front. At the end of this post you can see some function calls with the output i'am looking for.
(Function '(+ 100 1 2 3 4 5 6));-> 121
(Function '(+ 1 2 A B 3 C));-> (+ 6 C B A)
(Function '(+ A B C D 0));-> (+ D C B A)
(Function '(- 2 3 4 5 6));-> -19
(Function '(- 1 B));-> (- 1 B)
(Function '(- 6 2 A B 3 C));-> (- 1 C B A)
In any Lisp you have car to fetch the first element of a list and cdr for fetch the list without the first element and cons to add a list to the beginning of another list.
identify the operation by looking at the first element. Since - in a quoted list is a symbol you cannot apply this so you need to have a list of acceptable operations.
Loop through the rest of the list accumulating the non numbers to a list and the numbers using the operation.
cons the resulting number onto the list of symbols, then the operation making it (op num . symbols)
I'm assuming that + and - don't work as math as swithcing the order when using - or any other non associative operation changes the result.

coerce function in common lisp -- arrays and lists

I am seeing different behavior of coerce between different versions of Common Lisp - wondering which one is "right" or is the standard ambiguous on this seemingly simple question:
is
(coerce '(1 2 3) 'array)
correct lisp? It works fine in Clozure Common Lisp but not in sbcl.
And when it does not work what is the easiest way to coerce a list into an array?
Thanks
The specification says:
If the result-type is a recognizable subtype of vector, and the object is a sequence, then the result is a vector that has the same elements as object.
array is not a subtype of vector -- vectors are 1-dimensional arrays, but array includes arrays with any number of dimensions.
You can use one of these
(coerce '(1 2 3) 'vector)
(coerce '(1 2 3) '(array t (*)))
In the second version, (*) specifies a single dimension whose size is unspecified.
Your use is unspecified, so implementations are free to implement it as they please. If it returns a value, the value has to be an ARRAY of some kind.
To add to Barmar's answer (this is really a comment, but it's too long), while it's fine for CCL to do what it does, I think it's clear that something like this would be very hard to define in a standard.
Consider something like this:
(coerce '((1 2 3) (4 5 6) (7 8 9)) 'array)
What is the result of this meant to be? Should it be:
a vector each of whose elements is a three-element list?
the equivalent of (make-array '(3 3) :initial-contents '((1 2 3) (4 5 6) (7 8 9)))?
the transpose of that array?
I think either of the first two are reasonable interpretations: sometimes you will want one, sometimes the other. The third is probably not reasonable given CL is a row-major language.
So if (coerce ... 'array) were in the standard, how would you specify which of these you wanted? If you just chose one, which should it be (and how do you now reach agreement with the people on the committee who think it should be the other!)?

Minizinc, counting occurrences in array of pairs

I'm new to constraint programming and toying around with some basic operations. I want to count the number of occurrences of an arbitrary element x in an array of pairs.
For instance, the following array has 2 eights, and 1 of every other element.
sampleArray = [{8,13}, {21,34}, {8,55}]
I wonder how I am to extract this information, possibly using built-in functions.
I'm not sure I understand exactly what you want to do here. Do you want to count only the first element in the pair?
Note that the example you show is an array of sets, not a 2 dimensional matrix. Extracting and count the first(?) element in each pair is probably easier if you have a two dimensional matrix (constructed with array2d).
In general there are at least two global constraints that you can use for this: "count" and perhaps also "global_cardinality". See http://www.minizinc.org/2.0/doc-lib/doc-globals-counting.html

Lisp - List of lists with variable size

I have to represent a board game using lisp. To do that i have to create a function that builds the board.
This function receives an integer that represents the number of sublist the original list as to have. each one of this sublists have a different size growing in 3*n proportion.
As an example ifthe function is called with the number 3, it will create a list with 3 sublists, the first with 3 position, the second with six and the third with 9.
Also, each of the positions need to be initialized with ´*.
To do this i think i have to make a recursive call to make-list, but i can't seem to do that right. i'be tried to use the 'dotimes' cycle to do that, but i didn't have any sucess with that.
So far i have:
(defun faz-tabuleiro (n_aneis)
(make-list n_aneis :initial-element (...)
Wich creates the main list, but how can i represent the sublists inside with the right size?
Does this do what you want?
(defun make-table (n)
(loop :for i :from 1 :to n
:collect (make-list (* i 3) :initial-element "*")))
Map over a list of numbers being the length of the sublists. Use a function for this mapping which returns the right list initialized with the initial element.

extract/slice/reorder lists in (emacs) lisp?

In python, you might do something like
i = (0, 3, 2)
x = [x+1 for x in range(0,5)]
operator.itemgetter(*i)(x)
to get (1, 4, 3).
In (emacs) lisp, I wrote this function called extract which does something similar,
(defun extract (elems seq)
(mapcar (lambda (x) (nth x seq)) elems))
(extract '(0 3 2) (number-sequence 1 5))
but I feel like there should be something built in? All I know is first, last, rest, nth, car, cdr... What's the way to go? ~ Thanks in advance ~
If your problem is the speed then use (vector 1 2 3 4 5) instead of a list, and (aref vec index) to get the element.
(defun extract (elems seq)
(let ((av (vconcat seq)))
(mapcar (lambda (x) (aref av x)) elems)))
If you're going to extract from the same sequence many times of course it make sense to store the sequence in a vector just once.
Python lists are indeed one-dimensional arrays, the equivalent in LISP are vectors.
I've only done simple scripting in elisp, but it's a relatively small language. And extract is a very inefficient function on linked lists, which is the default data structure in emacs lisp. So it's unlikely to be built-in.
Your solution is the best straightforward one. It's n^2, but to make it faster requires a lot more code.
Below is a guess at how it might work, but it might also be totally off base:
sort elems (n log n)
create a map that maps elements in sorted elem to their indices in original elem (probably n log n, maybe n)
iterate through seq and sorted elem. Keep only the indices in sorted elem (probably n, maybe n log n, depending on whether it's a hash map or a tree map)
sort the result by the values of the elem mapping (n log n)
From My Lisp Experiences and the Development of GNU Emacs:
There were people in those days, in 1985, who had one-megabyte machines without virtual memory. They wanted to be able to use GNU Emacs. This meant I had to keep the program as small as possible.
For instance, at the time the only looping construct was ‘while’, which was extremely simple. There was no way to break out of the ‘while’ statement, you just had to do a catch and a throw, or test a variable that ran the loop. That shows how far I was pushing to keep things small. We didn't have ‘caar’ and ‘cadr’ and so on; “squeeze out everything possible” was the spirit of GNU Emacs, the spirit of Emacs Lisp, from the beginning.
Obviously, machines are bigger now, and we don't do it that way anymore. We put in ‘caar’ and ‘cadr’ and so on, and we might put in another looping construct one of these days.
So my guess is, if you don't see it, it's not there.