How do I make a list?

Hi, I'm Hekkn. I recently moved from scratch and don't know how to create lists. I know I should add more words, but I have no idea what else to say.

Edit: please stop replying to this, it's gone too far

you can get the list block from the variables section (the actual block should be red-ish), then in order to get more spaces for words you wanna press the arrows, click the one facing right to add words and the one facing left to remove words

yeah, I think you're right. thanks for the help!

okay so I think I might be on the right direction but idk how it works that well yet

If you're coming from Scratch, you're accustomed to the idea that when you create a list, it has a name permanently attached to it, and you use the list by referring to that name.

But attaching names to things isn't really the job of lists; it's the job of variables. So the equivalent of pushing the "make a list" button in Scratch and naming it FOO would be

  1. Push the "make a variable" button, naming the variable FOO.
  2. Then

Yep, I'm coming from scratch. Thanks for the quick reply!

8 posts were merged into an existing topic: How lists work

Okay, I'm having trouble doing this because I don't know where to start. Do you know what an array is? Do you know what a pointer is? Taken a data structures course? Help me out.

I know basically what an array is. Everything else, no.

I know about arrays and linked lists, and I (mostly) understand how Snap lists work.

Do I understand these correctly?:
A pointer gives a location ("points") to a particular location in memory (e.g. the next item in the list). A linked list has values and pointers. An array has all of the values next to each other in memory (I'm less sure about that one).

look, if you know scratch, you'll get this.

so you make a variable, name it what you'd name a list, in this case, "list."
set it to a list, I've found out that the name really doesn't matter that much.
to get the equivelant of "add (thing) to [list v]" on scratch:
instead of using untitled script pic (1)
you'd use untitled script pic (2)
the only time you'd use untitled script pic (1) is if you have more than 1 list you'd want to set [list] to

I moved my lecture series on how lists work here:

Hey, @hekknyt

You should definitely read Brian's thread to understand how lists in Snap! work :slight_smile:

For a quick start however, this might help:

Snap!'s list block is a reporter (ovally shaped block that reports a value and can be used as an input to other blocks (e.g. the item _ of _).

If you want to use the add block to add stuff to a list programmatically, you have to create a variable.
As the add block requires a list as its second input, you have to take care that your variable is set to a list first.

This script doesn't work, because the variable my list is not of the list type.

If you initialize the list first, by setting the variable my list to an empty list, it works:

Of course you can not only use the command block add to extend your list, but also the function in front of, e.g. like this:

However, as Brian mentioned, you shouldn't combine those 2 ways of dealing with lists, because the internal representation of these lists is different (linked list vs. array) whether you use the in front of block or the add block and you might want to avoid converting between two data types all the time.

Hope that helps,

My crazy idea would be, and I know you will not like it, but I am going to say it anyway. When the "ADD something TO Foo" command is used, it should automatically set the Foo variable to list type so the user doesn't necessary need to do it by previously SET-ting it to list type.

yeah, but the user's intention can be very ambiguous. What if the variable already has a value? Should we throw it away or treat is as the first item of a coerced list? And, thinking this even further, what if it's not a variable but the item of another list? Should we then silently turn the target list into a multi-dimensional one? I think this opens up pandora's box...

Can you, perhaps, make lists separate from variables, like scratch does? That would make it more straightforward for everyone.

We are talking newbies here, remember (I know it is your job as an experienced developer to take into account all possible scenarios, and I do appreciate it), and not many newbies are expected to be aware of the possibility of ADD-ing an item_of_another_list TO an_existing_list, let alone to try to do it. Or they wouldn't be newbies.

This capacity of the ADD to silently modify Foo type to list type would only be reserved for the cases meeting two criteria:

  1. the value_to_be_ADDed is either a plain text or a plain number (turning into an item in the process) and
  2. the target variable was not defined (i.e. SET to) as a list type yet.

heck no! That would totally defeat the purpose of teaching the idea of variables and first-class-ness.

I don't think so, because it is the purpose of Snap' to teach users that you can make a variable to point to various things, not only plain text or numbers, but also more complicated stuff, for example, a ringified (snippet of a) script (or block), an object (sprite) (if I am not mistaken), a list, etc.