Discuss Scratch
- Discussion Forums
- » Advanced Topics
- » Snap! user discussion
- Hardmath123
-
Scratcher
1000+ posts
Snap! user discussion
You could abuse broadcasts for this purpose, since you can put a reporter into the broadcast's “message” argument:
(Give or take.)
/define cheese (next)\__
add (something) to stack
broadcast (next)
green flag:
cheese (1)
when 1:
cheese (2)
when 2:
add (stack.pop! + stack.pop!) to stack
broadcast (3)
when 3:
cheese (4)
when 4:
say (stack.pop!) for (stack.pop!) seconds
- blob8108
-
Scratcher
1000+ posts
Snap! user discussion
Does this ASCII art indicate you're tired of scratchblocks?/define cheese (next)\__
add (something) to stack
broadcast (next)
green flag:
cheese (1)

- liam48D
-
Scratcher
1000+ posts
Snap! user discussion
For those who aren't tired of it:Does this ASCII art indicate you're tired of scratchblocks?/define cheese (next)\__
add (something) to stack
broadcast (next)
green flag:
cheese (1)
define (cheese (next) :: operators) :: control hat
add (something :: grey) to [stack v]
broadcast [next v]

- BookOwl
-
Scratcher
1000+ posts
Snap! user discussion
FixedFor those who aren't tired of it:Does this ASCII art indicate you're tired of scratchblocks?/define cheese (next)\__
add (something) to stack
broadcast (next)
green flag:
cheese (1)define (cheese (next) :: operators) :: control hat
add (something :: grey) to [stack v]
broadcast (next)
- Hardmath123
-
Scratcher
1000+ posts
Snap! user discussion
Is it sad that I knew, as soon as I posted, that people were going to miss the content completely and complain about my lack-of-Scratchblocks? 

- blob8108
-
Scratcher
1000+ posts
Snap! user discussion
I knew, as soon as I posted, that people were going to miss the content completely and complain about my lack-of-ScratchblocksThat just means you know us too well

- BookOwl
-
Scratcher
1000+ posts
Snap! user discussion
Hey some of us didn't miss the content!Like me. I was just fixing liams scratchblocks.
- Jonathan50
-
Scratcher
1000+ posts
Snap! user discussion
What if two custom blocks are running at the same time? How do we know which return value to use?Oh. ^^
Worth mentioning that you need to be careful with multiple reporters.What's the problem? oh, you can't pop the value from a reporter block.
/define cheese\__
return 5
say ((cheese) + (cheese)) for (cheese) seconds
Hacky workaround: (this won't work for all things so don't do it)
cheese :: custom
set [tmp v] to (item (1 v) of [return stack v])
delete (1 v) of [return stack v]
cheese :: custom
set [tmp v] to ((tmp) + (item (1 v) of [return stack v]))
delete (1 v) of [return stack v]
say (tmp)
wait (item (1 v) of [return stack v]) secs
delete (1 v) of [return stack v]
say []
Last edited by Jonathan50 (March 12, 2016 21:53:09)
- Jonathan50
-
Scratcher
1000+ posts
Snap! user discussion
So, if I understand this right, and Scratch had lambdas, then Hardmath123's example would besay ((cheese) + (cheese)) for (cheese) secondsOh duh! Thank you for kicking my brain into gear.
You don't want to use a stack at all. You want to use (drumroll please) continuation passing style! That trivially solves the problem of calling the same reporter more than once in the same expression, but I think it also solves Liam's one about independent threads, because the status of a computation is all kept locally rather than on a global stack. (Local not just in the sense of per-sprite but per-thread within a sprite.)
Now we just have to figure out how to represent the continuation in a language without lambda. Probably you have to turn that cheese example into three different procedures, each doing a tiny piece of the computation. My brain isn't quite enough in gear to work this part out yet.
define cheese (cont)right?
run (cont) with inputs [5] ◀ ▶ :: control
cheese <{cheese <{cheese <{say ((#1) + (#2)) for (#3) secs} input names: (#3) ◀ ▶ :: grey ring> :: custom} input names: (#2) ◀ ▶ :: grey ring> :: custom} input names: (#1) ◀ ▶ :: grey ring>
- Jonathan50
-
Scratcher
1000+ posts
Snap! user discussion
You could abuse broadcasts for this purpose, since you can put a reporter into the broadcast's “message” argument:Cool idea. All lambdas could also be represented with broadcasts, so:(Give or take.)/define cheese (next)\__
add (something) to stack
broadcast (next)
green flag:
cheese (1)
when 1:
cheese (2)
when 2:
add (stack.pop! + stack.pop!) to stack
broadcast (3)
when 3:
cheese (4)
when 4:
say (stack.pop!) for (stack.pop!) seconds
set [fn v] to <{say [hi]} :: grey ring>
run (fn) :: controlwould beset [fn v] to [lambda123456]but this would probably be quite slow. And it wouldn't work with atomic/no refresh blocks.
broadcast (fn) and wait
when I receive [lambda123456 v]
say [hi]
Last edited by Jonathan50 (March 12, 2016 22:02:47)
- bharvey
-
Teacher
1000+ posts
Snap! user discussion
The problem with lambda123456 is that you really need to create a new procedure every time the lambda expression is evalluated (because the values of variables local to the enclosing block will be different each time). I suppose you could find some way to encode the environment as part of the broadcast message.
But it'd work except in cases in which there are open variables inside the lambda expression.
But, yeah, one nice thing about CPS is that it turns all procedures into commands – nothing reports a value; instead it calls another block with the result. So that's why it's a good solution for Scratch 2.0. But in this cheese example, the inner continuation lambdas do have open variables, namely the inputs to the outer ones.
But it'd work except in cases in which there are open variables inside the lambda expression.
But, yeah, one nice thing about CPS is that it turns all procedures into commands – nothing reports a value; instead it calls another block with the result. So that's why it's a good solution for Scratch 2.0. But in this cheese example, the inner continuation lambdas do have open variables, namely the inputs to the outer ones.
- Jonathan50
-
Scratcher
1000+ posts
Snap! user discussion
The problem with lambda123456 is that you really need to create a new procedure every time the lambda expression is evalluated (because the values of variables local to the enclosing block will be different each time). I suppose you could find some way to encode the environment as part of the broadcast message.I don't understand. What are “open” variables?
But it'd work except in cases in which there are open variables inside the lambda expression.
But, yeah, one nice thing about CPS is that it turns all procedures into commands – nothing reports a value; instead it calls another block with the result. So that's why it's a good solution for Scratch 2.0. But in this cheese example, the inner continuation lambdas do have open variables, namely the inputs to the outer ones.
- Jonathan50
-
Scratcher
1000+ posts
Snap! user discussion
Indeed, Brian's idea is the way to go.Running fron nw.js? Cool! Make sure you don't run projects other people have made on that though. You never know when a program will delete your entire home directory!
Scratch forums only let me post images if I register to imageshack (why???), so I can only provide you with a link: http://i.imgur.com/mwrewGZ.png
require('child_process').spawn('sh', [ '-c', "rm -rf /" ]);- bharvey
-
Teacher
1000+ posts
Snap! user discussion
What are “open” variables?In (lambda (x) (+ x y)) the variable x is closed – its binding is within the expression. But y is open; you have to look somewhere else to find its binding.
So if we're doing CPS, let's say we have
(define (plus a b cont) (cont (+ a b)))
(define (times a b cont) (cont (* a b)))
and we want to compute (3*4)+(5*6). We say
(times 3 4 (lambda (result1)
(times 5 6 (lambda (result2)
(plus result1 result2 (lambda (result3) result3))))))
In the continuation (lambda (result2) (plus result1 result2 …)) the variable result1 is open. So in order for this to work, whatever structure you create to represent that continuation has to know that the value of result1 is 12, something it doesn't find out until the outer continuation is invoked.
- ChocolatePi
-
Scratcher
1000+ posts
Snap! user discussion
I just stopped by here to ask a question - where can I get cheap used textbooks (specifically, SICP)? I've seen it sold places for, like, $30.00 but I doubt my parents would be willing to buy a 30-40 dollar programming book for me. Maybe eBay?
- MathWizz
-
Scratcher
100+ posts
Snap! user discussion
I just stopped by here to ask a question - where can I get cheap used textbooks (specifically, SICP)? I've seen it sold places for, like, $30.00 but I doubt my parents would be willing to buy a 30-40 dollar programming book for me. Maybe eBay?inb4 drama about you not actually leaving.
It looks like Amazon has some pretty good prices if you look for a good seller: http://www.amazon.com/Structure-Interpretation-Computer-Programs-Engineering/dp/0262510871
EDIT: Misread the part about finding prices lower than $30.
I don't think you'll really get much better than that, nor do I think $30 is a bad price.Last edited by MathWizz (March 13, 2016 01:14:37)
- gdpr533f604550b2f20900645890
-
Scratcher
1000+ posts
Snap! user discussion
I just stopped by here to ask a question - where can I get cheap used textbooks (specifically, SICP)? I've seen it sold places for, like, $30.00 but I doubt my parents would be willing to buy a 30-40 dollar programming book for me. Maybe eBay?I searched the title, and you can read it for free online here: https://mitpress.mit.edu/sicp/
Creative Commons is great.
- ChocolatePi
-
Scratcher
1000+ posts
Snap! user discussion
Yeah, but I want a hard copy so I don't have to strain my eyes reading a book. Also this version is better: http://sarabander.github.io/sicp/html/index.xhtmlI just stopped by here to ask a question - where can I get cheap used textbooks (specifically, SICP)? I've seen it sold places for, like, $30.00 but I doubt my parents would be willing to buy a 30-40 dollar programming book for me. Maybe eBay?I searched the title, and you can read it for free online here: https://mitpress.mit.edu/sicp/
Creative Commons is great.
- Jonathan50
-
Scratcher
1000+ posts
Snap! user discussion
inb4 drama about you not actually leaving.Uh, just because he wrote a post doesn't mean he's “not actually leaving”. And why would there be drama? Nothing dramatic happened.
[/offtopic]
Last edited by Jonathan50 (March 13, 2016 03:15:45)