The question is incredibly hard to understand.
Is there a (convenient) way to program the number of elements and related conditions based on an earlier calculation?
The problem โprogramโ is not really an understandable verb in this sentence, because a person is programming a computer or programming a VCR, but you cannot โprogram a numberโ. Therefore, I do not understand what you are trying to say here.
But I can give you an overview of the code, and perhaps through the code review I can understand the question you are asking.
View unsolicited code
It looks like you are trying to solve the maze, possibly eliminating the dead ends.
Actually your code:
Create a list of cells that are not dead or adjacent to dead ends called filtered
Create a sequence of neighboring cells from step 1, sequences
Combining four such adjacent sequences into a route.
The main problem : this only works if the correct route is exactly eight fragments! Try to solve this maze:
[E] - [] - [] - []
|
[] - [] - [] - []
|
[] - [] - [] - []
|
[] - [] - [] - []
|
[] - [] - [] - [E]
So, working backwards from the code review, it looks like your question is:
How do I generate a list if I do not know how much time has passed?
Decision
You can solve the maze by searching (DFS, BFS, A *).
import Control.Monad -- | Maze cells are identified by integers type Cell = Int -- | A maze is a map from cells to adjacent cells type Maze = Cell -> [Cell] maze :: Maze maze = ([[1], [0,2,5], [1,3], [2], [5], [4,6,1,9], [5,7], [6,11], [12], [5,13], [9], [7,15], [8,16], [14,9,17], [13,15], [14,11], [12,17], [13,16,18], [17,19], [18]] !!) -- | Find paths from the given start to the end solve :: Maze -> Cell -> Cell -> [[Cell]] solve maze start = solve' [] where solve' path end = let path' = end : path in if start == end then return path' else do neighbor <- maze end guard (neighbor `notElem` path) solve' path' neighbor
The solve function works by searching in depth. Instead of putting everything in a single list comprehension, it works recursively.
To find the path from start to end , if start /= end ,
Look at all the cells near the end, neighbor <- maze end ,
Make sure we are not backing away from the guard (negihbor `notElem` path) cell guard (negihbor `notElem` path) ,
Try to find a path from start to neighbor .
Do not try to understand the whole function at once, just understand the bit about recursion.
Summary
If you want to find a route from cell 0 to cell 19, recurse: we know that cells 18 and 19 are connected (because they are directly connected), so we can instead try to solve the problem of finding a route from cell 0 to cell 18.
This is recursion.
Footnotes
Defender
someCondition a == True
Is equivalent
someCondition a
And therefore also equivalent
(someCondition a == True) == True
Or
(someCondition a == (True == True)) == (True == (True == True))
Or
someCondition a == (someCondition a == someCondition a)
The first, someCondition a , is good.
Footnote for do
The designation do in the above example is equivalent to list comprehension,
do neighbor <- maze end guard (neighbor `notElem` path) solve' path' neighbor
Equivalent code in list comprehension syntax,
[result | neighbor <- maze end, neighbor `notElem` path, result <- solve' path' neighbor]