blob8108 wrote:
Okay, this is getting complicated. Are you thinking something like "set x to (1 in front of (2 in front of x))"?
No, in that case the circular part reaches all the way back to the head of the list. You can't set up the thing I mean in one step. It has to be
set X to (1 in front of (2 in front of X))
set Y to (a in front of (b in front of X))
Then Y is the list (a, b, 1, 2, 1, 2, ...)
Actually even that might not really work -- the SET X will fail because X doesn't already have a value. Certainly it doesn't already have the right value.
Jens, we need a set-cdr! block.
Or find whether a list is circular in time linear in the number of unique elements.
I don't understand this one. (Which is probably ironic... ) If it's not nested, you can just do:
for item in list:
if item is list:
return item
Again, you are assuming that the circularity is at the head of the list. For each item in the list, you have to check whether that item is either the list or any tail of the list.
It's not hard to solve this problem in quadratic time. (But you have to be careful not to take infinite time if the list is, in fact, circular. We want an answer either way, in finite time.) Making it linear time (oh, and, btw, constant space) is one of those problems that hardly anyone gets -- someone has to show you, as part of your initiation into the mysteries.
I thought the same — I've been reading the first chapter of SICP over the past few days. (The new Kindles have this awful feature that predicts how long it will take you to finish, and for SICP it claims about 16 hours...) Maybe I should try some of the other exercises from that? Though I thought it was more about language theory...
SICP is about the deepest mysteries of the universe. It explains the meaning of life. Those who haven't read SICP are ignorant of computer science. (Yes, ignoramuses are legion in highly paid jobs in the computer industry.) (Ignorami? Ignorames?) They are callow youth, gambolling in the fields.
Do the exercises. All of them. It's not like your high school algebra book, with 50 identical-except-for-the-numbers equations to solve in the same way. Every exercise teaches a specific new idea or skill.
Last edited by bharvey (2012-11-29 13:15:11)
Offline
Okay, okay! I'll read SICP... *serious face*
bharvey wrote:
blob8108 wrote:
Okay, this is getting complicated. Are you thinking something like "set x to (1 in front of (2 in front of x))"?
No, in that case the circular part reaches all the way back to the head of the list. You can't set up the thing I mean in one step. It has to be
set X to (1 in front of (2 in front of X))
set Y to (a in front of (b in front of X))
Then Y is the list (a, b, 1, 2, 1, 2, ...)
Actually even that might not really work -- the SET X will fail because X doesn't already have a value. Certainly it doesn't already have the right value.
Jens, we need a set-cdr! block.
Okay, this is definitely making my brain hurt now.
I think I understand Snap! lists a bit more. They're, so they either take the form of a "cons", having attributes:
first -> first item
rest -> a list containing the remaining items
Or a normal JS array, where all the items are found on the "contents" attribute.
Right? (Sorry, I'm being slow here.)
So what I think you're trying to do is set up a list "list" where "rest" = "list". Am I right?
Offline
bharvey wrote:
SICP is about the deepest mysteries of the universe. It explains the meaning of life.
Where? On page 42? I didn't see it...
Offline
blob8108 wrote:
bharvey wrote:
blob8108 wrote:
Okay, this is getting complicated. Are you thinking something like "set x to (1 in front of (2 in front of x))"?
No, in that case the circular part reaches all the way back to the head of the list. You can't set up the thing I mean in one step. It has to be
set X to (1 in front of (2 in front of X))
set Y to (a in front of (b in front of X))
Then Y is the list (a, b, 1, 2, 1, 2, ...)
Actually even that might not really work -- the SET X will fail because X doesn't already have a value. Certainly it doesn't already have the right value.
Jens, we need a set-cdr! block.Okay, this is definitely making my brain hurt now.
I think I understand Snap! lists a bit more. They're, so they either take the form of a "cons", having attributes:
first -> first item
rest -> a list containing the remaining items
Or a normal JS array, where all the items are found on the "contents" attribute.
Right? (Sorry, I'm being slow here.)
So what I think you're trying to do is set up a list "list" where "rest" = "list". Am I right?
He's trying to make a list like this:
{ // 1 first: 'a', rest: { // 2 first: 'b', rest: { // 3 first: '1', rest: { // 4 first: '2', rest: … // 3 } } } }
Offline
blob8108 wrote:
Okay, this is definitely making my brain hurt now.
Always a good sign.
I think I understand Snap! lists a bit more. They're, so they either take the form of a "cons", having attributes:
first -> first item
rest -> a list containing the remaining items
Or a normal JS array, where all the items are found on the "contents" attribute.
Right? (Sorry, I'm being slow here.)
So what I think you're trying to do is set up a list "list" where "rest" = "list". Am I right?
You're not being slow. I'm being slow, for not remembering that REPLACE ITEM (n) OF (list) WITH (value) converts the list into array format. (Jens just emailed to remind me that I wrote this code.) That's a misfeature, I now think. I'll work on it.
The goal of the complexity of list implementation is that Scratch-style iterative list processing should continue to work efficiently (constant time, not linear time, for ITEM, INSERT, etc.), but also Scheme-style recursive functional list processing should work efficiently (constant time IN FRONT OF and ALL BUT FIRST OF).
The only thing that's inefficient is mixing styles with the same list, first doing ALL BUT FIRST OF and then doing INSERT etc, because then the list gets converted from one form to the other repeatedly.
What's confusing you is that when we agreed on this design, I had in mind that the use of car/cdr lists would always be purely functional. So we didn't provide for SET-CAR! or SET-CDR! equivalents. I'll work on that... as soon as I get out from under writing grad school recommendation letters for Berkeley students. It's that season, as you know.
So, don't feel bad, you're doing as well as possible, given the current limitations of Snap! list processing.
Offline
joefarebrother wrote:
Where? On page 42? I didn't see it...
Ah, my son, that was a comic novel. The meaning of life isn't a sound bite; you have to study the entire book to understand it fully.
And bring me a towel, a piece of fresh fruit, and $100.
Offline
bharvey wrote:
Or find whether a list is circular in time linear in the number of unique elements.
I don't understand this one. (Which is probably ironic... ) If it's not nested, you can just do:
for item in list:
if item is list:
return itemAgain, you are assuming that the circularity is at the head of the list. For each item in the list, you have to check whether that item is either the list or any tail of the list.
It's not hard to solve this problem in quadratic time. (But you have to be careful not to take infinite time if the list is, in fact, circular. We want an answer either way, in finite time.) Making it linear time (oh, and, btw, constant space) is one of those problems that hardly anyone gets -- someone has to show you, as part of your initiation into the mysteries.
I did it in linear time and linear space (and JavaScript ).
function isCircular(list) { var flag = {}; // any unique value !function f(list) { if (isEmpty(list) || isList(car(list)) && car(car(list)) === flag) return; setCar(list, cons(flag, car(list))); f(cdr(list)); }(list); return function f(list) { if (isEmpty(list)) return false; if (!isList(car(list)) || car(car(list)) !== flag) return true; setCar(list, cdr(car(list))); return f(cdr(list)); }(list); }
I'm still working on constant space.
Last edited by nXIII (2012-11-30 07:26:36)
Offline
bharvey wrote:
Those who haven't read SICP are ignorant of computer science. (Yes, ignoramuses are legion in highly paid jobs in the computer industry.) (Ignorami? Ignorames?) They are callow youth, gambolling in the fields.
This made me laugh. I've been trying a bit of Android development lately and the lack of first-class procedures is driving me nuts!
Offline
fullmoon wrote:
and the lack of first-class procedures is driving me nuts!
But of course lambda isn't all of SICP; it's in Chapter 1 (of 5). Really understanding OOP, for example, comes in Chapter 3. Understanding compilers in Chapter 5. Declarative (logic) programming, Chapter 4.
And so on.
Offline
Found a linear time, constant space, tail recursive test for circularity written in scheme.
(define (circular? x) (let race ((hare x) (tortoise x)) ;;hare and tortoise algorithm (if (pair? hare) (let ((hare (cdr hare))) (if (pair? hare) (or (eq? hare tortoise) (race (cdr hare) (cdr tortoise))) (circular-on-cars? x)) (circular-on-cars? x))))
It's linear time because as it traverses the list the hare is one more ahead of the tortoise each time, so if it is circular it will go round the list once before it catches up, otherwise it will hit the end of the list.
My circular on cars procedure is linear space (it probably has too many opening or closing parenthesis too but i can't be bothered to check)
(define (circular-on-cars? x) (let ((unique-value (cons '() '()))) (let helper ((x x) (callback (lambda () #t))) (cond ((not (pair? x)) (callback) #f) ;;just call for the side effect of putting all the cars back and report false instead of true ((eq? (car x) unique-value) (callback)) (else (let ((old-car (car x))) (set-car! x unique-value) (helper (cdr x) (lambda () (set-car! x old-car) (callback)))))))))
Last edited by joefarebrother (2012-12-07 17:13:39)
Offline
bharvey wrote:
joefarebrother wrote:
(let race ((hare x) (tortoise x)) ;;hare and tortoise algorithm
Great! Does "found" mean, like, "found in Google," or did you independently invent this?
I found it when reading through a few CS textbooks but I changed a letrec for a named let and added circular-on-cars?
Last edited by joefarebrother (2012-12-07 17:15:33)
Offline
technoboy10 wrote:
Hey all! I've been playing with Snap! on the iPad mini, and it works pretty well except for a few things. The text inputs default to caps lock, and the iOS touch-and-hold to move the text cursor doesn't work. Any way to fix this?
Offline
What would the cloud thing on snap eventually do when it works? Will it be saving projects in the cloud, or something like cloud variables in 2.0, or some way to connect projects like mesh via the cloud, or is it something else?
Offline
joefarebrother wrote:
What would the cloud thing on snap eventually do when it works? Will it be saving projects in the cloud, or something like cloud variables in 2.0, or some way to connect projects like mesh via the cloud, or is it something else?
Aargh I just wrote a long reply to this and the stupid forum software logged me out before I hit "submit"!
Trying again... The initial impementation will do two things: (1) Allow you to save your projects (and block libraries, costumes, anything exportable) privately. (2) Allow us to provide "public" block libraries and media that anyone can access easily.
(Backstory: The ability to save projects reliably and conveniently is the biggest thing standing in the way of us declaring beta status. So we want that now rather than waiting for a super-featureful back end.)
Later, of course we want people to be able to publish projects, block libraries, etc. to the world, even though I'm still terrified about having to police the site.
In a certain sense, cloud variables are just a special case of block libraries, since a variable is a kind of block. For example, I imagine one of the libraries we'd provide would be called "constants" and would have blocks such as PI, E, and PHI. But thinking of cloud variables that way would require projects to save the variable after every change of value. So, Scratch-2.0-style cloud variables are something for us to think about later.
Worldwide mesh would be super cool, of course, but also a security hassle. You don't want strangers hijacking your collaborative project. Keeping the mesh behind a local firewall implicitly solves that problem; once we make it global, we'd have to invent a login mechanism for local virtual meshes. Another thing for us to think about after 4.0 is out the door.
Of course, once people can publish projects, we'd want to do all the Scratch-front-page sorts of things, and fora, and person-to-person messaging so I can argue with Shadow about politics -- if Club Penguin can do it, so can we, imho.
Offline
bharvey wrote:
So I can argue with Shadow about politics
Looking forward to it.
Offline
Hi technoboy10, I've just gotten a new iPad mini myself and I'm delighted how well Snap! works on it! I'll look into improving text input gradually, right mow you have to explicitly turn caps on and off, and tap on the character where you want the caret to be. If you zoom the entire window make sure to use two fingers for scrolling. Cheers!
Offline
On my iPad (2 running iOS 6) Snap! works brilliantly (for a web app!), but it's dreadfully slow. Maybe I'm spoiled with Apple's fluid scrolling, but I would really like some speed optimizations.
Offline
Ah, which generation of the iPad 2 are your running Snap on? It's quite fast (I'd even say surprisingly fast) on the iPad mini. So I'm inclined - as I did with Smalltalk and Squeak - to just wait for computers to become faster. But hey, I'm open to any suggestions about speed optimizations you might have or come up with! Is there anything in particular that you can point me to?
Offline
You can now "snap" comments to blocks in http://snap.berkeley.edu/run, in the main scripting area. Then if you move the block the comment follows.
Enjoy!
Offline
Jens wrote:
Hi technoboy10, I've just gotten a new iPad mini myself and I'm delighted how well Snap! works on it! I'll look into improving text input gradually, right mow you have to explicitly turn caps on and off, and tap on the character where you want the caret to be. If you zoom the entire window make sure to use two fingers for scrolling. Cheers!
Ok, thanks.
Offline
I don't know if that is worth of interest or not ? I've installed on a Windows XP PC a server , xampp, which allows to use Snap! locally. It works pretty well, better with Firefox than Chrome. Snap! projects can be loaded and saved as usual.
Offline
xly wrote:
I don't know if that is worth of interest or not ? I've installed on a Windows XP PC a server , xampp, which allows to use Snap! locally. It works pretty well, better with Firefox than Chrome. Snap! projects can be loaded and saved as usual.
Yes, that's awesome! Does that mean you keep a local copy of the Snap! source code, or does it automatically download the latest version when you have net access?
(It's especially awesome because it's platform-independent!)
Offline