jmoenig / Snap

a visual programming language inspired by Scratch
http://snap.berkeley.edu
GNU Affero General Public License v3.0
1.49k stars 742 forks source link

[suggestion] more "in place" instructions #1336

Closed AlainBusser closed 8 years ago

AlainBusser commented 8 years ago

I know it may seem incompatible with pure fonctional programming but my pupils tend to find it easier to understand commands like "change x by 1" than the classical assignment "set x to x+1". So I began designing a programming language called Sofus (in french: https://github.com/AlainBusser/Sophus ) with commands in the same style:

But these commands must be applied to a variable which can be given by its name yet be global. Trying to build a block for, say, "double" (which, I insist, must not be a function computing the double of a number, but a command actually doubling the variable) with the "BYOB" facility of Snap! yields me to nothing:

Let's say I create a block in the "variable" category and call it "double" then add a variable called x (a numerical variable), then add to the definition the block "change x by x".

noside1

Well, if now I set y to anything then double it, it is still equal to 0 after the doubling:

noside2

I tried with an anonymous JavaScript function but as it is not a block I don't even know what to do with it (it was x *= 2 which surely wouldn't work as x might not have been passed by name, would it?)

I understand that for functionnal programming side effects should be avoided but these commands would be an advantage for teaching programmation to teenagers who like the concision of this kind of language. So is there at least a way to copy-paste this "change by 1" block and have more blocks like this at least "change by 1 percent" which is very useful for my pupils)?

There is no urgency as I already managed to program blocks of this kind with Blockly, and my gouvernment already choosed Scratch 2.0 for all the pupils...

Anyway you make a great job !

dekrain commented 8 years ago

Use: change <x> by x // <> is ring, but you must go to dev mode and change its spec to change %s by %n

AlainBusser commented 8 years ago

Thanks alot, I didn't imagine it would be that simple (and I understand better the meaning of "ringify" now :-) )

But how do I "go to dev mode"?

dekrain commented 8 years ago

Shift + Click a Snap! logo, select enter dev mode and close browser with no knowledge about this Wait: I now making block to do this

dekrain commented 8 years ago

Vua la:

<blocks app="Snap! 4.0, http://snap.berkeley.edu" version="1"><block-definition s="GIVE ME A NEW SET BLOCK" type="command" category="other"><header></header><code></code><inputs></inputs><script><custom-block s="let %upvar be %s"><l>block</l><block s="evaluate"><block s="reportJSFunction"><list></list><l>var block = this.blockForSelector(&apos;doSetVar&apos;)&#xD;block.userSetSpec(&apos;set %s to %s&apos;)&#xD;block.isDraggable = true;&#xD;return block</l></block><list></list></block></custom-block><block s="doRun"><block s="reportJSFunction"><list><l>block</l></list><l>block.setPosition(this.world().hand.bounds.origin);&#xD;block.pickUp(this.world());</l></block><list><block var="block"/></list></block></script></block-definition><block-definition s="let %&apos;var&apos; be %&apos;val&apos;" type="command" category="other"><header></header><code></code><inputs><input type="%upvar"></input><input type="%s"></input></inputs><script><block s="doSetVar"><l>var</l><block var="val"/></block></script></block-definition></blocks>

Save this to file and drag it to Snap!

cycomachead commented 8 years ago

The design of Snap! is supposed to encourage a certain type of programming, but not require it. For better or worse, that's a goal.

The goal is to also to have a nearly minimal set of blocks that are required to get the job done. We want to encourage users to write their own blocks. When things can be accomplished via custom blocks, as is this case, the preference is to build a library of blocks. If you were to write a library of cleanly written and documented custom blocks, I think there's a chance it would be added to the libraries menu.

We certainly don't want to hinder younger audiences, but adding too many blocks adds complexity for everyone.