This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1651 2010-09-02 13:31:47

MathWizz
Scratcher
Registered: 2009-08-31
Posts: 1000+

Re: BYOB 3 - Discussion Thread

OH! I have an idea for a block that can come it a lot of handy! (touching sprites) It reports a list of all the sprites that are touching the calling sprite. This one can't really be... I have a better idea! A (sprite names) block! Oh, wait if everything will become first class the this might become obsolete.  hmm  O'well. At least I gave you a few ideas...  tongue

EDIT: 23rd page! (At least for me...)

Last edited by MathWizz (2010-09-02 13:32:49)


http://block.site90.net/scratch.mit/text.php?size=30&text=%20A%20signature!&color=333333

Offline

 

#1652 2010-09-02 13:42:59

fullmoon
Retired Community Moderator
Registered: 2007-06-04
Posts: 1000+

Re: BYOB 3 - Discussion Thread

bharvey wrote:

Our general policy is to add the features you need to build what you want, rather than to add what you want.  Given lists of lists, you can make associative lists -- there's a project by fullmoon on the byob web site.

What you need for full hash tables is a way to turn text into numbers, and we provided that with the char-to-ASCII block.  And you need list code to be blindingly fast, which we're working on.

What we need is for BYOB's arrays to be sparse (and correct me if they already are), i.e. that you can add an item to any index of the array even if it's not within the current bounds of the array. Right now (as far as I know), this doesn't work:

Code:

a = list("a","b","c")
set item 447 of a to "d"

Even if we had a function to convert strings to integer values and vice versa, we still wouldn't be able to build a native dictionary/hash/whatever with string keys, because the integer values would be outside the bounds of the array. I don't know what this would do to listwatchers, however.


http://i302.photobucket.com/albums/nn100/fullmoon32/wow.jpg

Offline

 

#1653 2010-09-02 14:07:46

bharvey
Scratcher
Registered: 2008-08-10
Posts: 1000+

Re: BYOB 3 - Discussion Thread

fullmoon wrote:

Even if we had a function to convert strings to integer values and vice versa, we still wouldn't be able to build a native dictionary/hash/whatever with string keys, because the integer values would be outside the bounds of the array.

The way hash tables work is that you make a list whose size is a prime number bigger than the number of entries you expect, with an empty list in each element.  Then to hash something, you convert its key to an integer modulo the size of the list, i.e., you use the REMAINDER function to get the value.  Then you add the new element to the list that's in the chosen element of the hash table.

So you can do it with the tools you have.

The classic hash function for text strings is that for each letter/character you say

SET <hashindex> TO (<hashindex> * 32) + (ASCII CODE FOR <char>)

It's 32 rather than, say, 128 or 256 because the high order bits of letters are all the same, so you want to make sure each bit of the result is actually affected by the key, to make sure the values are evenly spread out.


http://cs.berkeley.edu/~bh/sig5.png

Offline

 

#1654 2010-09-02 14:09:35

bharvey
Scratcher
Registered: 2008-08-10
Posts: 1000+

Re: BYOB 3 - Discussion Thread

MathWizz wrote:

OH! I have an idea for a block that can come it a lot of handy! (touching sprites) It reports a list of all the sprites that are touching the calling sprite. This one can't really be... I have a better idea! A (sprite names) block! Oh, wait if everything will become first class the this might become obsolete.

Only partly.  The block would be SPRITES rather than SPRITE NAMES and would report a list of all the sprites.  And TOUCHING SPRITES reports a list of the sprites that touch this one.  I think we do need both of those.


http://cs.berkeley.edu/~bh/sig5.png

Offline

 

#1655 2010-09-02 14:12:05

bharvey
Scratcher
Registered: 2008-08-10
Posts: 1000+

Re: BYOB 3 - Discussion Thread

fullmoon wrote:

What we need is for BYOB's arrays to be sparse

Sparse arrays are a good idea, but I think they can be implemented on top of the lists we already have.  Basically a sparse array is a hash table in which the keys are numbers.


http://cs.berkeley.edu/~bh/sig5.png

Offline

 

#1656 2010-09-02 17:44:12

Jens
Scratcher
Registered: 2007-06-04
Posts: 1000+

Re: BYOB 3 - Discussion Thread

I love sparse arrays! In fact I love them so much that I based my first prototype of how lists should work in Scratch on them (that was before there were lists in Scratch). They're easy to use and blindingly fast:

   http://www.chirp.scratchr.org/blog/?p=16

The funny part, of course, is that in order to simulate a sparse array I used Smalltalk's Dictionary class, instead of the other way round  smile

Last edited by Jens (2010-09-02 17:45:05)


Jens Mönig

Offline

 

#1657 2010-09-03 01:48:52

fullmoon
Retired Community Moderator
Registered: 2007-06-04
Posts: 1000+

Re: BYOB 3 - Discussion Thread

bharvey wrote:

The way hash tables work is that you make a list whose size is a prime number bigger than the number of entries you expect...

I'm still trying to wrap my head around that...


http://i302.photobucket.com/albums/nn100/fullmoon32/wow.jpg

Offline

 

#1658 2010-09-03 10:12:53

bharvey
Scratcher
Registered: 2008-08-10
Posts: 1000+

Re: BYOB 3 - Discussion Thread

fullmoon wrote:

I'm still trying to wrap my head around that...

You use a prime number because that minimizes the extent to which different keys hash into the same bucket.

At the opposite extreme, suppose you pick a power of two as the hash table size.  Then computing REMAINDER <something> <2^n> gives you the rightmost n bits of the number.  The high-order bits don't contribute at all to deciding which bucket gets this key.

Using a prime number guarantees that every bit of the original key is reflected in the remainder.

Does that help?  Or are you stuck somewhere else?


http://cs.berkeley.edu/~bh/sig5.png

Offline

 

#1659 2010-09-03 11:05:53

rubiks_cube_guy238
Scratcher
Registered: 2009-07-02
Posts: 100+

Re: BYOB 3 - Discussion Thread

I figured out a way to make a dictionary in BYOB without hashing or cycling!
http://i53.tinypic.com/2zrf5g3.gif]
It's always distorted, but I hope you can understand it.

This block takes advantage of the fact that you can use the (copy of [list]) block with a string input to get a list of the string's characters.

Also, you can only use a string with typical characters (ones that are on your keyboard) as indices, but the value can be anything.

Last edited by rubiks_cube_guy238 (2010-09-03 11:19:28)


The glass is never half full nor half empty; it is twice as large as it needs to be.

Offline

 

#1660 2010-09-03 12:28:34

nXIII
Community Moderator
Registered: 2009-04-21
Posts: 1000+

Re: BYOB 3 - Discussion Thread

I don't know why, but I'm getting so many bugs in BYOB.


nXIII

Offline

 

#1661 2010-09-03 13:36:20

bharvey
Scratcher
Registered: 2008-08-10
Posts: 1000+

Re: BYOB 3 - Discussion Thread

rubiks_cube_guy238 wrote:

I figured out a way to make a dictionary in BYOB without hashing or cycling!

Cool!  There's a name for your invention; it's called a "trie" (pronounced "try").  It's as fast as hashing, but usually uses more memory (because you're going to have a lot of mostly-empty dictionaries).

This block takes advantage of the fact that you can use the (copy of [list]) block with a string input to get a list of the string's characters.

Oh, weird.  I think that's a bug, but I suppose it doesn't hurt anything.  There's an ALL BUT FIRST LETTER OF block in the tools library, which would be the more straightforward way to do what you're after.


http://cs.berkeley.edu/~bh/sig5.png

Offline

 

#1662 2010-09-03 13:38:38

bharvey
Scratcher
Registered: 2008-08-10
Posts: 1000+

Re: BYOB 3 - Discussion Thread

nXIII wrote:

I don't know why, but I'm getting so many bugs in BYOB.

Report them in bugzilla!  Thanks.


http://cs.berkeley.edu/~bh/sig5.png

Offline

 

#1663 2010-09-03 13:44:52

nXIII
Community Moderator
Registered: 2009-04-21
Posts: 1000+

Re: BYOB 3 - Discussion Thread

bharvey wrote:

nXIII wrote:

I don't know why, but I'm getting so many bugs in BYOB.

Report them in bugzilla!  Thanks.

But I don't know why they're happening, because I can't, for example, change the color of a custom block (which you can do normally? It gives me an error)


nXIII

Offline

 

#1664 2010-09-03 14:10:45

bharvey
Scratcher
Registered: 2008-08-10
Posts: 1000+

Re: BYOB 3 - Discussion Thread

nXIII wrote:

But I don't know why they're happening, because I can't, for example, change the color of a custom block (which you can do normally? It gives me an error)

Right, that works for me.  So, take a project in which it doesn't work, and send it to bugzilla along with a description of which block you tried to change.


http://cs.berkeley.edu/~bh/sig5.png

Offline

 

#1665 2010-09-03 14:16:47

rubiks_cube_guy238
Scratcher
Registered: 2009-07-02
Posts: 100+

Re: BYOB 3 - Discussion Thread

bharvey wrote:

rubiks_cube_guy238 wrote:

I figured out a way to make a dictionary in BYOB without hashing or cycling!

Cool!  There's a name for your invention; it's called a "trie" (pronounced "try").  It's as fast as hashing, but usually uses more memory (because you're going to have a lot of mostly-empty dictionaries).

This block takes advantage of the fact that you can use the (copy of [list]) block with a string input to get a list of the string's characters.

Oh, weird.  I think that's a bug, but I suppose it doesn't hurt anything.  There's an ALL BUT FIRST LETTER OF block in the tools library, which would be the more straightforward way to do what you're after.

I know that there's an a.b.f.l.t. block, but that uses a repeat. I did this because I wanted to be able to create a dictionary where no repeat blocks are used, and can't get any collisions (like you get from hashing).


The glass is never half full nor half empty; it is twice as large as it needs to be.

Offline

 

#1666 2010-09-03 14:32:05

fullmoon
Retired Community Moderator
Registered: 2007-06-04
Posts: 1000+

Re: BYOB 3 - Discussion Thread

bharvey wrote:

Does that help?  Or are you stuck somewhere else?

Ok...I reread your original post, and looked through rubiks_cube_guy's dictionary class, and I get it now!

bharvey wrote:

with an empty list in each element

was the part that I was missing.


http://i302.photobucket.com/albums/nn100/fullmoon32/wow.jpg

Offline

 

#1667 2010-09-03 14:44:52

rubiks_cube_guy238
Scratcher
Registered: 2009-07-02
Posts: 100+

Re: BYOB 3 - Discussion Thread

No, you don't understand. I don't understand hashing either.
My dictionary class doesn't use hashing.

bharvey wrote:

it's called a "trie" (pronounced "try").

Last edited by rubiks_cube_guy238 (2010-09-03 14:45:00)


The glass is never half full nor half empty; it is twice as large as it needs to be.

Offline

 

#1668 2010-09-03 16:16:35

bharvey
Scratcher
Registered: 2008-08-10
Posts: 1000+

Re: BYOB 3 - Discussion Thread

rubiks_cube_guy238 wrote:

I wanted to be able to create a dictionary where no repeat blocks are used

But there's an implicit repeat inside the COPY OF primitive, and inside the AS TEXT primitive.  So it still takes time proportional to the length of the key.  In principle, an explicit repeat shouldn't be any slower if the block that uses it is marked atomic.  (That's probably not true in practice because the interpretation of the repeat block takes a little time.)

Don't get me wrong; I like your solution, especially its object-orientedness.  (Object-orientation?)

Hashing is a technique that provides a high probability, but not a guarantee, of avoiding collisions.  That's why each hash table entry is a list; we hope they'll be of length 0 or 1, but a few of them will be longer.


http://cs.berkeley.edu/~bh/sig5.png

Offline

 

#1669 2010-09-03 17:32:16

shadow_7283
Scratcher
Registered: 2007-11-07
Posts: 1000+

Re: BYOB 3 - Discussion Thread

I think we need to be able to create temporary variables in 3.1.

Why when you can just use a list?
Well, when I make a block, sometimes I want to parse something and turn the contents into a list using the (list ->) block. Unfortunately, I can't find a way to join an old list's items with a new list's items, turning it into one list. Ideally, there should be a block for that, but it wouldn't have many uses. And I can't find a way to do it by creating a custom block without creating a global list (which is a pain).

Temporary variables would solve that problem, and add more functionality to BYOB. I understand it isn't necessarily in line with the "First Class" idea, but it would be a really useful feature.

Does anyone have any alternative solutions in the meantime?

Offline

 

#1670 2010-09-03 18:35:01

bharvey
Scratcher
Registered: 2008-08-10
Posts: 1000+

Re: BYOB 3 - Discussion Thread

shadow_7283 wrote:

I think we need to be able to create temporary variables in 3.1.

We have that!  Use the SCRIPT VARIABLES block.  Or am I missing something?

Unfortunately, I can't find a way to join an old list's items with a new list's items, turning it into one list.

There's an APPEND block in the tools library that takes any number of lists as inputs and returns a new list concatenating all their elements.  If you want to reuse an old name to point to this new list, don't use "Make a list"'; set a variable to the original list and then re-set it to the new value when you do the append.


http://cs.berkeley.edu/~bh/sig5.png

Offline

 

#1671 2010-09-03 18:42:33

nXIII
Community Moderator
Registered: 2009-04-21
Posts: 1000+

Re: BYOB 3 - Discussion Thread

shadow_7283 wrote:

I think we need to be able to create temporary variables in 3.1.

Why when you can just use a list?
Well, when I make a block, sometimes I want to parse something and turn the contents into a list using the (list ->) block. Unfortunately, I can't find a way to join an old list's items with a new list's items, turning it into one list. Ideally, there should be a block for that, but it wouldn't have many uses. And I can't find a way to do it by creating a custom block without creating a global list (which is a pain).

Temporary variables would solve that problem, and add more functionality to BYOB. I understand it isn't necessarily in line with the "First Class" idea, but it would be a really useful feature.

Does anyone have any alternative solutions in the meantime?

You could use upvars in c-shaped command-block reporters.
Yes, I know, "WHAT?!"
Basically, you use upvars instead of return values (hey, you can have more than one!) and put a "Code [" as the last argument. "run (Code)" on the last line of your block, and you have variables for use just inside the reporter.

Last edited by nXIII (2010-09-03 18:44:12)


nXIII

Offline

 

#1672 2010-09-03 18:50:15

bharvey
Scratcher
Registered: 2008-08-10
Posts: 1000+

Re: BYOB 3 - Discussion Thread

nXIII wrote:

Yes, I know, "WHAT?!"

big_smile


http://cs.berkeley.edu/~bh/sig5.png

Offline

 

#1673 2010-09-03 19:14:53

shadow_7283
Scratcher
Registered: 2007-11-07
Posts: 1000+

Re: BYOB 3 - Discussion Thread

bharvey wrote:

shadow_7283 wrote:

I think we need to be able to create temporary variables in 3.1.

We have that!  Use the SCRIPT VARIABLES block.  Or am I missing something?

Unfortunately, I can't find a way to join an old list's items with a new list's items, turning it into one list.

There's an APPEND block in the tools library that takes any number of lists as inputs and returns a new list concatenating all their elements.  If you want to reuse an old name to point to this new list, don't use "Make a list"'; set a variable to the original list and then re-set it to the new value when you do the append.

You can't create script variables based on need. For instance, if I wanted to create a variable every time the space key was pressed, I couldn't.

Thanks for the append tip. I'll try it out.

Offline

 

#1674 2010-09-03 20:21:15

nXIII
Community Moderator
Registered: 2009-04-21
Posts: 1000+

Re: BYOB 3 - Discussion Thread

bharvey wrote:

nXIII wrote:

Yes, I know, "WHAT?!"

big_smile

I think I'll make it my new coding style, super-ultra-explicit contexts.

Last edited by nXIII (2010-09-03 20:23:28)


nXIII

Offline

 

#1675 2010-09-03 20:33:43

shadow_7283
Scratcher
Registered: 2007-11-07
Posts: 1000+

Re: BYOB 3 - Discussion Thread

nXIII wrote:

bharvey wrote:

nXIII wrote:

Yes, I know, "WHAT?!"

big_smile

I think I'll make it my new coding style, super-ultra-explicit contexts.

What? I'm confused.

Offline

 

Board footer