Summit steinberg cover?fm=jpg&fl=progressive&q=75&w=300

(Functional) Programming for Everyone

One of the grandest ambitions for Swift is the transformation of programming education. After being open sourced, Swift will run on many more kinds of devices, and it’s imperative that our children can learn to control these devices in an accessible and engaging way. In this talk, Daniel Steinberg draws on his history as an educator and student of programming, from the punched card onwards, to answer the question: How can we make programming accessible to everyone? Demonstrating his own Swift Playground version of Logo’s famous Turtle, we get a real glimpse of Swift and Playground’s educative potential.

Before Modern Programming… (0:00)

There are a lot of good things in Swift, but there are still a lot of wonderful things in Objective-C. Of course, there are some things that we can do only in Swift, and I want to talk about some of those things. The first line of code that I ever wrote was 10 print "Hello", and I wrote it on a Teletype Model 33. However, I didn’t run it on this machine, because this wasn’t the computer. The Teletype was just the input device. To stress how old I was, I wrote this code even before we had "Hello, World!". We stored our programs on punched tape, which we rolled up and took around with us.

What was your first computer? It is amazing that we use the word “your”, because there are a lot of people who can’t have their own computer. In the old days, we didn’t. We shared computers. And, in this room we can also ask, what your first Mac? I would guess that most of you were born after January 1984, and so you have always had Macs to choose from. My first computer was partly the Teletype that I mentioned earlier. Just think - in less than a month, we will all have more power on our wrist than all these machines in this room. It is stunning for that to have happened in my lifetime.

We Shared Computers (2:32)

The other striking thing about the Teletype was that in my high school class, we shared one machine between fifteen of us. As a result, we did a lot of coding on paper, and we did a lot of really making sure we had things right. Time at the terminal was valuable. Time at the terminal was time that we were taking away from somebody else. As we think of schools and shared resources, this is background on where we came from.

Get more development news like this

Much later, people began experimenting with some very interesting things. Seymour Papert and his group were working the first turtle. It was a very simple, but real device where you could command directions, as well as whether the pen was to be up or down. However, we had access to devices like the Atari 410, and so we taught with those. You stored programs on a cassette, recorded with sound signals, and viewed output by connecting it to a television screen.

Turtle Programming (3:04)

On our devices, our version of the turtle was written in Logo. Logo provided us a way of learning how to program in Pascal without really programming in it. We could tell the turtle to move forward by 100, turn right 90 degrees, and forward again. By doing that, we could program squares.

At the same time, Alan Kay was doing work on Smalltalk. His idea was based around teaching children and interacting with things. A big part of Smalltalk was Squeak. One of the most brilliant first projects included creating a steering wheel in a car, and having kids learn to use the steering wheel to turn the car. You would then teach the car how to stay on the road. If it veered too much to one side, it would correct. You could draw roads in one colour and make sure that your car stayed on it. This was genius programming.

Fast Forward to Today (5:22)

If we look forward to technology today, we have playgrounds. If you try to get kids to write a game, they don’t see anything interesting until further into development. With playgrounds, we can do something only in Swift. In a playground, I can get immediate feedback. In Xcode 6.3, value history now appears in place. I can see a turtle without having to tell the students to open up a console. All of a sudden, programming becomes immediate and compelling.

Teach Students Bit by Bit (7:10)

When you teach students, you don’t need to teach them the whole truth at once. You can start by getting them to write things like forward 10 to move the turtle. We have kids involved right away. Then you can expose more. At a certain point, it will be appropriate to show the students the code that is making the turtle work. In Xcode 6.3 beta 3, we no longer need to put supporting code in a workspace. Your playground will now have a disclosure indicator, which will contain a Sources and a Resources folder. You can drag images into the Resources folder, and you can write code in Sources. That code in Sources is automatically compiled into a module that the main playground automatically imports.

The students didn’t realize that the turtle was in this supporting code all along. After clicking on the indicator, they see “Secret Turtle Stuff” and “What a Turtle Understands”. I even have a place for them to write their custom code when it’s time. Inside of the “Secret Turtle Stuff”, I have Utilities.swift. This is not so interesting to the students. Here, I am converting from degrees to radians, and extending int to make it easier to repeat an action. I certainly am not going to start out by teaching students about closures. I also have Turtle.swift, where they can look at the turtle.

Expose APIs (10:05)

The API I don’t mind exposing them to right away is in ”What a Turtle Understands”. My turtle movement functions are forward, back, and home. Students can understand that! They tell the turtle to go forward or backward an amount, and here is where they can see how that is implemented. They don’t need to know that there are default values for the movement, because we are exposing a little at a time. In time, I can teach students about enumerations to show them how we can do these things with underlying values. We can even add methods to enumerations. These are pretty cool things that we could teach them about Swift.

Parameters (11:18)

We have some pen operations like penUp and penDown. We can clear the entire screen, and we can set the pen colour. We can show and hide the turtle. Here is where the students start. I can tell them, “You know what, you can change the colour. You can set the pen colour to red.” They play with it and see that you can set the pen colour in the middle of drawing, and lines that come after will be in the new colour. Then I show them that you can turn by a degree amount other than ninety degrees.

The feedback is immediate in playgrounds. Because I extended ints, students can repeat and understand what they are doing. Maybe they don’t want to always move forward by fifty, and so I can introduce students to variables and constants. Now, they have learned how to use a parameter.

Abstraction (13:28)

Then, I can teach then to abstract 6.repeat into a function to create a hexagon. All of a sudden, they think Swift sucks because the hexagon is gone. When I remind them that they have to actually call the function, they understand something deeper. We can create these things that we call later.

Slowly, they’re learning functional programming, and they’re seeing the results right away. If you have ever seen kids in a classroom, you notice that they look at what the person next to them is doing. It’s very visual. They challenge each other to make a bigger shapes, different shapes - by doing this, they grow.

At the point that they have grown enough, you start introducing syntax to them. Instead of 6.repeat, you show them the actual code used in Swift: for. We don’t introduce this until they are ready. Education curriculum often introduces new subjects because that is what you are supposed to teach on this day. But now, you get students to drive you to a point where they want to know how to do something, and you can show them.

Access Modifiers (15:23)

You can eventually explain access modifiers. With the hexagon, you can show them how to save the function that created the hexagon. Inside of the user functions, you paste the function. Then, you tell them you actually have to declare it as public. I can explain what access modifiers are, because you’re somewhere else in your code but want to see something that is defined in a different location. So, you have to make that code accessible just so that you can see it and use it. That is an exciting moment.

Now the students have created a hexagon. The students have created what you did in the turtle. The students have created the same thing that we did to move forward and right. They have created routines, and they own this. Now they are on their way. This is just a small example of how we can use these new playgrounds to excite a whole new generation about what we are excited about.

Thank you.

Q&A (16:14)

Q: I have a little brother who picked up an Objective-C book, which is a little bit more in depth from what you showed. How do you do what you just showed, if someone is having trouble with jumping into much more detailed programming?

Daniel: UIView Dynamics are an amazing thing, because you can teach someone to make a square. Then you can give it gravity and behaviour. You can actually build up quite a bit, without having to create the turtle. I haven’t played with SceneKit, but the reason I thought of UIView Dynamics is because I can display that in a playground. Now, that is not Objective-C. But it isn’t an Objective-C versus Swift issue; it’s a tooling issue.

We don’t have playgrounds in Objective-C, which is why I am very excited for those. In Objective-C, if you have the money, you can get them a license. I don’t think anything is exciting as when you write your first dumb app in the world and see it on a real device. The other thing that we forget about is that they are coding on a Mac. You can write Objective-C for Mac apps. Then they can distribute it to their friends, because that isn’t locked down. It is very exciting for kids to see their friends using something they wrote. And so, writing a little compelling app that you can share with people is pretty cool.

Q: I tried to teach programming to my little brother. When they learn you can make apps and games, kids want to see Call of Duty right away. How do you respond to that?

Daniel: That’s hard. The same problem exists for adults who have tried to learn a musical instrument. The classical problem is that our tastes are so much better than our abilities. We have taught kids that with devices, even apps that aren’t so great can look really cool. It’s a challenge. I don’t know the answer to that.

Q: When people learn things like Scratch, they are exposed to GUI elements. When young people learn how to use Swift, what do you think they struggle with the most?

Daniel: One of the things I think is the hardest, even in what I just showed, is the fact that the function accepts a Double. I have to teach them that 100 has to be entered as a Double that they have to specify. It’s a hard concept - it’s a number, so who cares? Well, it is implemented as a struct. So, I think the type system is very hard.

Q: I have a general comment and observation. My kids are learning programming through Scratch, and one of the things that is very good about Scratch specifically is that they use it in a classroom _and they can take it home. If you want to bring Swift to kids in school, make sure that they can use it at home as well. We all know that the side projects is where you do a lot of your learning._

Daniel: That is why a lot of people do web-based projects. They can bring those home. Now, I have to say I was critical of Scratch only because I know its heritage. It came from Squeak, which I thought was just so wonderful and so powerful - and it didn’t require Flash.

Q: I’ve heard several times people say that it’s hard to pick up functional programming, because we learned programming through imperative programming. But in fact, functional programming is a model that is closer to the way we think. Does that make sense?

Daniel: A caveat at the end of Brian’s talk was that one of the downsides is code that is terse. Our eyes can get accustomed to this, if we teach kids from the beginning. But, just like I don’t want kids drinking, I don’t want them to do too much functional programming when you.

Q: At meetups, we learn about what might be good things to do in Swift. How might we transfer that back to syllabi we create?

Daniel: Andy Matuschak thinks a lot about pedagogical issues. Seymour Papert, who developed Logo, was teaching about the MIT Media Lab. The Japanese curriculum for math is brilliant in ways that we don’t have in the United States or in the UK. Just because multiplication is commutative, doesn’t mean you should teach one times a number at the same time you teach a number times one. These are different conceptually. As we teach functional and object-oriented programming, these are the things that I’d like us to think about. It’s not that one is better than the other. Students are ready from them and these difference abstractions at different times. They stress them in different ways, and we should understand if they are ready for the next step.

Next Up: New Features in Realm Obj-C & Swift

General link arrow white

About the content

This talk was delivered live in March 2015 at Swift Summit London. The video was transcribed by Realm and is published here with the permission of the conference organizers.

Daniel Steinberg

Daniel is the author of the books ‘A Swift Kickstart’ and ‘Developing iOS 7 Apps for iPad and iPhone’, the official companion book to the popular iTunes U series from Stanford University. Daniel presents iPhone, Cocoa, and Swift training and consults through his company Dim Sum Thinking. He is also the host of the CocoaConf Podcast.

4 design patterns for a RESTless mobile integration »