Discuss Scratch

-Literal-
New Scratcher
24 posts

Create List Block in program.

Bump
-Literal-
New Scratcher
24 posts

Create List Block in program.

888888 88 88 8888 8888 88888888
88 88 88 88 88 88 88 88 88 88
88 88 88 88 88 88 88 88 88 88
88888888 88 88 88 88 88 888888
88 88 88 88 88 88 88 88
88 88 88 88 88 88 88
888888 88888888 88 88 88

*Writes whole thing by hand*
*Realizes this could be considered spam*
“Oops”
-Literal-
New Scratcher
24 posts

Create List Block in program.

-Literal- wrote:

Bump

-Literal- wrote:

Bump

-Literal- wrote:

Bump
Oops…

Sorry…

I must've accidentally not seen it appear and written it again…
15-MinuteGaming
Scratcher
100+ posts

Create List Block in program.

Jonathan50 wrote:

15-MinuteGaming wrote:

However, it has the limitation that lists must be the exact same size…
That can be done by storing the items consecutively in a list and keeping the index (into the Scratch list) of the first item, which serves as a pointer, and then to access an item of the array add the index to the pointer. (Then you can make as many arrays as you like, but if you want to reuse space taken by arrays no longer needed, you would have to delete arrays once you're finished with them and change the constructor to find a free space of adequate length. The alternative is to use linked lists, which don't store their items one after the other; this makes ITEM slow but adding items at known positions becomes fast, but you still need to keep track of what's used and what isn't if you don't want to waste memory. If Scratch had first class lists then the browser would automatically reuse space taken by lists no longer needed, which is what most decent programming languages do.)
This is an interesting idea, which does not seem to have many limitations (except for the 20,000 item limit in lists, which probably will not be exceeded). However, it can be difficult to implement. Since this is a commonly seen problem, I believe first class lists will save not only memory, but also effort, as the second suggested workaround can be tedious and slow to run. Also, with linked lists, each “list” must be the exact same size. If they do not need to be, please tell me how. However, I will attempt to add it to the list of workarounds when I have time.
Jonathan50
Scratcher
1000+ posts

Create List Block in program.

15-MinuteGaming wrote:

Also, with linked lists, each “list” must be the exact same size. If they do not need to be, please tell me how.
A linked list is either empty or consists of the first item and the list with all the items but the first. There's a wiki article about them.
15-MinuteGaming
Scratcher
100+ posts

Create List Block in program.

Sorry, I misunderstood what you meant about linked lists. However, this is quite a complex workaround for a solution which would be otherwise extremely simple.
imfh
Scratcher
1000+ posts

Create List Block in program.

Here's a simple workaround that supports variable length lists:

define locate list (x)
// gets the position of list x
set [j v] to [1] // the first item of a list holds its length
repeat ((x)-(1))
change [j v] by (item (j) of [Lists v] :: list)
end

define change length of (x) by (n)
// changes the length of list x
locate list (x)
set [k v] to ((item (j) of [Lists v] :: list)+(n)) // k holds the new length
replace item (j) of [Lists v] with (k)

define get item (x) (y)
// x selects the list, y selects the item
locate list (x)
set [item v] to (item ((j)+(y)) of [Lists v] :: list)

define add item (x) [item]
change length of (x) by (1)
insert (item) at ((j)+(k)) of [Lists v] // add the item

define delete item (x) (y)
change length of (x) by (-1)
delete ((j)+(y)) of [list v] // delete the item

define insert [item] at (x) (y)
change length of (x) by (1)
insert (item) at ((j)+(y)) of [Lists v] // insert the item

define replace (x) (y) with [item]
locate list (x)
replace item ((j)+(y)) of [Lists v] with (item) // replace the item
Nambaseking01
Scratcher
1000+ posts

Create List Block in program.

Support.

But as Res said, you should be able to also create and delete variables. I mean, you can do this in other programming languages so why not in Scratch?
Scratch-Coding
Scratcher
500+ posts

Create List Block in program.

Bump
Scratch-Coding
Scratcher
500+ posts

Create List Block in program.

Za-Chary
Scratcher
1000+ posts

Create List Block in program.

Scratch-Coding wrote:

This is a duplicate of https://scratch.mit.edu/discuss/topic/356259
Looks like that one you linked is newer than this topic, though. However, some good discussion appears to be happening there, so I'll attempt to merge the two threads. Here goes nothing!

EDIT: Success!

Last edited by Za-Chary (Aug. 8, 2019 00:07:18)

15-MinuteGaming
Scratcher
100+ posts

Create List Block in program.

The link is a 403 error.
Za-Chary
Scratcher
1000+ posts

Create List Block in program.

15-MinuteGaming wrote:

The link is a 403 error.
That's because I merged it to this thread. Now the other one no longer exists.
15-MinuteGaming
Scratcher
100+ posts

Create List Block in program.

WindOctahedron wrote:

space_elephant wrote:

WindOctahedron wrote:

With variables, it has already been done by other users.
Unfortunately, lists aren't first class* in Scratch. However, Snap! does have first class data.
*This means that lists can't contain lists.
Variables only work in 2.0.
But it's still easy to do it in 3.0.
define create variable (name)
add (name :: custom arg) to [names v]
add (0) to [values v]
define set (name) to (value)
replace item (item # of (name :: custom arg) in [names v] :: list) of [values v] with (value :: custom arg)
define change (name) by (value)
replace item (item # of (name :: custom arg) in [names v] :: list) of [values v] with ((name :: custom arg)+(value :: custom arg))
define delete (name)
delete (item # of (name :: custom arg) in [names v] :: list) of [values v]
delete (item # of (name :: custom arg) in [names v] :: list) of [names v]
Why is first class data not allowed? It can simplify a ridiculously large amount of stuff
Monniasza_spzoo
Scratcher
100+ posts

Create List Block in program.

Please add my solution here

This workaround is very slow, which might be useless in large volume processing.
MrFluffyPenguins
Scratcher
1000+ posts

Create List Block in program.

Can't you just make the list on your own?
Monniasza_spzoo
Scratcher
100+ posts

Create List Block in program.

Mr_PenguinAlex wrote:

Can't you just make the list on your own?
Nested lists will be needed for my new game
15-MinuteGaming
Scratcher
100+ posts

Create List Block in program.

-Literal- wrote:

Bump
ScratchCatHELLO
Scratcher
1000+ posts

Create List Block in program.

Bump (I think it's allowed, it's been longer than 24 hours)
TopicBumper
New Scratcher
100+ posts

Create List Block in program.

ScratchCatHELLO wrote:

Bump (I think it's allowed, it's been longer than 24 hours)
Ditto, but this topic seems similar to the local lists in this suggestion.

Powered by DjangoBB