Lisp cyclic lists [closed] - list

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
We have been given homework from lisp where I need to use "cyclic" list (I don't know what is the right naming for this). By "cyclic" list, I mean list, where cdr of the last one cons points to the very first one of the same list.
(Value1 . PointerValue2) (Value2 . PointerValue3) (Value3 . PointerValue1)
We have been taught to create such a lists with:
(defun cykl (l)
(setf (cdr (last l)) l)
)
The Lisp software (Lispbox) I use does not support this kind of lists. I have also tried clisp on Debian but it has crashed after creating such a list.
What lisp implementations do you know that does support this (freeware, os independent)?

All lisp implementations, including clisp, support circular lists.
When you say "crashed", you probably mean a stack overflow error (or an out-of-memory error), which you will always get when you try to print (remember the 'read-eval-PRINT' loop?) a circular structure when *print-circle* is nil. Setting it to t forces Lisp to use the #n# notation:
[1]> (defparameter l (list 1 2 3))
L
[2]> l
(1 2 3)
[3]> (setq *print-circle* t)
T
[4]> (setf (cdr (last l)) l)
#1=(1 2 3 . #1#)
See also function LIST-LENGTH

Related

How to understand lists(cons structure) in lisp [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I'm just starting to learn common lisp. I do not understand well about lists' structure when I'm reading the Set and Tree chapter. Now I wonder what's exactly it like in the list. I know the cons include two values which can be any types of object. Here I found a picture by google.
But I'm very confused. It seems like general lists in data structure.
lists in lisp
lists are build using cons cells you mentioned. single cons cell with nil in second part is the simplest list:
(cons 'A nil)
=> (A)
if you want to extend list, you cons another cell to it:
(cons 'C (cons 'B (cons 'A nil)))
=> (C B A)
so list is a series of linked together cons cells.
also, to make life easier there is list function. this way you don't have to write all those cons invocations and can create list like this:
(list 'C 'B 'A)
=> (C B A)
but inside it's still series of cons cells.

return a factorial of list numbers [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am trying to implement a function which when called return a factorial of numbers in list. For example (fact '(2 4 3)) => (2 24 6) but mine is not working. I am pretty sure that the logic is correct for my implementation just I can not find my syntax error. So if you could have a look and give some feedback it would be great here is the code:
(defun fact (list)
(cond ((null list) 0)
((zerop (first list) 1))
(* first list(fact (rest list)))
))
What you appear to be trying to do is apply a factorial function to each member of a list and collect the results in a list.
For this you need a factorial function and mapcar.

What is the answer to this list? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
3::[4;5;2;7];;
List.length [4;5;2;7];;
[4;5;2;7]::3;;
This gives the error This expression was expected to have type int list list but here has type int
I know that it can be solved by doing : [4;5;2;7];; List.append it [3];; but is there any other way to do it?
This is a really bad question. However, The last line in your code will fail with
This expression was expected to have type
int list list but here has type
int
.. because :: concatenates an element to a list. It can only do it from the front because the list is a singly linked list. If you want to add an element at the end of the list, you're probably using the wrong data structure. If you really want to add an element to the end of a list, you could do
[4;5;2;7] # [3];;
Be aware that this will be really inefficient.

C++ undirected graphs [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
I've searched a lot over the past few days on this theme and I don't understand how could I make an undirected graph without having a weight. Can anybody tell me which structure should I use and a simple algorithm? Thanks in advance!!!
There isn't any specific requirement that forces you to give weights to your edges. Your adjacency matrix could have binary entries 1-0 or true-false to specify a connection between nodes. All graph algorithms apply as normal.
A VERY helpful lecture about graphs: http://www.youtube.com/watch?v=ylWAB6CMYiY
In a slight elaboration on P.R.s answer. The standard matrix representation can easily be interpreted as the top (north-east) being from top to left (B to A), left being left to top (south-west, none here). A one (1) in any position (boolean internally perhaps) would indicate an unweighted connection.
x A B C D
A 0 1 0 0
B 0 0 1 1
C 0 0 0 1
D 0 0 0 0
Would imply that A is connected to no nodes. B is connected to A. C is connected to B. D is connected to B and C.
This particular example would create a tree with D as the root with children B and C, A as a child to B. A and C leafs.
Note that the unweighted property really does not simplify much. Only in a pure pointer implementation exercise, but that is quite pointless FAPP. An adjacency list instead of this adjacency matrix might give you memory usage advantages.

Haskell function which takes a list and return tuples [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 12 years ago.
I have ordederd to make a function, which takes a list ex [3,4,6,1,29] and returns a list of tuples [(3,4),(4,6),(6,1),(1,29)]
This is a very easy question, it's really hard to help without defeating the purpose...
If you are allowed to use predifined functions, there is already one which can do almost all work for you (if you don't know which one, try finding it with http://www.haskell.org/hoogle/ ). Take a step back and think about the easier question how to produce a list [(3,3),(4,4),(6,6),(1,1),(29,29)].
If you can't use predefined functions, then recursion is your friend: What do you need to do for an empty list? What for a list with one element? With two elements?
Without any own effort I can't give more hints. If you're stuck, extend your question and show what you already got, and we'll try to help.