Share your thoughts in the 2021 Clojure Community Survey!

Welcome! Please see the About page for a little more info on how this works.

–1 vote
in Clojure by
closed by

In this scenario, you are given a single-linked list data structure. It contains only a reference to the head node. Each node in the list contains only a single numeric value and a reference to the next node in the list. The "next" reference in the last node contains nil value.
Given this list, examine the following problems:
1. finding an element with a given numeric value,
2. listing the cartesian product of the list with itself - i.e. list all possible pairs created from the elements of the list. The order does matter here - the pair (1,2) is distinct from the pair (2,1).

for each of these problems:
State the algorithm you would use. Write it in the form of pseudocode or actual code and explain all steps, conditions, functions' arguments and all other features used.

thank a lot!

closed with the note: done

1 Answer

+1 vote

This looks like a homework

Here the solution of each problem

  1. (first (filter #{42} my-list))
  2. (for [i my-list j my-list] [i j])


define a "raw" type: (deftype MyNode [value next-node])
build your list: (def my-list (->MyNode 42 (->MyNode 55 nil)))

Try to execute 1: (first (filter #{42} my-list)).
It will throw because your type do not implement ISeq
Use extend-type to extend your custom type MyNode into ISeq protocol
And repeat these procedure until MyNode implement all interfaces needed for the algorithms

thank you for you quietly answer
I don't know how to do the part 'Use extend-type' on terminal ..
can you explain it to me ?

I thank you by a lot !