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

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.

Related

Clojure hashing algorithm [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I have working code where I get hash code of data structures with (hash data-structure) but it works in small amount of them. When I have few millions of data structures it will start giving me duplicate hashes (clash). How to get something more unique when structure of data structure is unknown?
You could always resort to md5 (or anything else, really).
(defn md5 [^String s]
(let [algorithm (MessageDigest/getInstance "MD5")
raw (.digest algorithm (.getBytes s))]
(format "%032x" (BigInteger. 1 raw))))
(defn md5-hash [s] (md5 (pr-str s)))
(md5-hash {:a [1 2 3 '(5 6 7)]})
=> "8f941424e629b876cdcb51509521870d"
also, you can use Java serialization instead of pr-str.
This library has existed for a while, and I've seen at least one alternative.

How to get a list as parameter of a function in haskell using GHCi [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 7 years ago.
Improve this question
I'm starting to learn Haskell now in college and I need to make anything in haskell. I tried to generate a random number but it seems too complicated. So I think I'll do something with lists but I can't make it work when trying to pass a list as parameter. I tried something like:
teste x[] = x
Just to receive a list and print it but I get some erros. What's the correct way to pass list as parameter ?
Also: Any ideia what I could implement? Just an example a little toy programm.
In order to specify the type of the parameters of a function, you use ::. You can make a function that takes a list and returns a list by saying
teste :: [a] -> [a]
teste x = x

How to make the pairwise product of two lists in OCaml? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 6 years ago.
Improve this question
How to write function that makes cartesian product of two lists?
For example, I have this
[a;b;c][d;e;f]
And need this
[a*d;b*e;c*f]
?
The function combine in the List module seems to fit your requirements. See documentation
Sample Usage:
let x = [1; 2; 3]
let y = [4; 5; 6]
List.combine x y (* => [(1, 4); (2, 5); (3, 6)] *)
Note that the function requires both lists to be the same length. If you want it to be more flexible, you may need to write this yourself.
If you wanted to combine the lists using an arbitrary function, one possible way would be to use List.combine to create this list of tuples, then use List.map to create a result from each pair.

Lisp cyclic lists [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 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

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.