I noticed that - thanks!
Question: How do you go about appending elements to a huge list in a timely manner? Should I use a vector or something instead?
Depends. Which operations do you need?
If you are always appending (and never prepending) you could just keep the elements in reverse order.
I’m just saving a bunch of numbers I want to get a mean value of later
If you don’t need the actual numbers, then just keep track of the sum and the number of numbers.
yeah, that would work nicely, thanks
Btw there is a data structure that is similar to vectors, but can be extended automatically: https://docs.racket-lang.org/data/gvector.html?q=grow
Good to know :slightly_smiling_face:
How does one prepend to a list in Racket? Is it just using list*()
? I can’t recall having come across another way :confused:
By prepend, do you mean construct (list 1 2 3)
from 1
and (list 2 3)
?
If so, you can either use list*
or cons
.
Note that these operations don’t mutate the existing lists, so it’s unlike, say, Python’s append
which does mutate the existing list.
I’m thinking along the lines of ::
from Haskell, F# and the like. Which should be the same as your first example :+1::skin-tone–2:
Haskell’s ::
is cons
So 1 :: 2 :: [3]
would be equivalent to (cons 1 (cons 2 (list 3)))
But to reduce parentheses, we have list*
which “flattens” the code that you need to write
So (list* 1 2 (list 3))
But wait, Haskell’s cons operator is :
, isn’t it? ::
is for type annotation. Idris on the other hand uses :
for type annotation and ::
for cons
Hackett’s ::
is cons
It might be :
in Haskell yeah. Some languages use one colon and some two, and I can never keep them straight in my head.