This hasn't stopped me fooling around with it on my own time though. I think I'm familiar enough with Scala now to be able to give a talk on it at work and bring people to the dark side. I'm still not what I'd call good at it yet, though. Still get tripped up on syntax occasionally.
Interestingly, the other night my girlfriend expressed curiosity about programming, so I thought I'd show her how you get stuff done in Scala. She's not a programmer, but has done tons of mathematics, so I fired up the Scala REPL and started showing her how to do stuff, and she picked it up with no problem.
It occurred to me that shells like the Scala REPL or Ruby's IRB are great ways to teach people how to program, same as older languages Logo or C64 Basic. You get instant feedback, you know you're doing it wrong right away. You try something else and you learn.
Of course, where Scala really shines here is that you have an interactive shell that's completely typesafe. I don't beleive there's ever been a language this popular that's had a typesafe interactive shell - this is something new (correct me if I'm wrong!)
Imagine what an invaluable teaching tool this could be. Scala's friendly syntax allows you to express really complex concepts with a minimum of syntactic cruft.
So I started messing around with the shell, showing my girlfriend how easy it is to do various things. I started off with saying that programming is a lot like algebra - you have named variables to refer to values. Then moved on to expressions, if/else constructs, functions, passing functions around, simple value classes, and manipluating collections of them. An hour long demonstration covered quite a bit of territory and was rather well received and understood. Which is pretty good when you consider that functional programming concepts are something I didn't get for quite some time, yet I was able to explain them to a smart non-programmer using a syntactically sane language with little difficulty.
I got to thinking that this would make a really good live coding demonstration that I could do in person, or even a Youtube video. So I've been thinking about ways to do make that happen.
In the meantime, I saved a transcript of the session, have included some it it below (exluding the more complex stuff), and have added comments to describe what's going on.
Want to try your hand at programming? Try this.
Store the number 3 in the variable named x. Scala will show that it now remembers that x, an integer, has the value 3.
scala> var x = 3
x: Int = 3
Evaluate x, i.e. recall what it's storing.
scala> x
res1: Int = 3
Evaluate x+3, an example of simple arithmetic
scala> x +3
res2: Int = 6
Define a function called "bump", which given an integer (which we'll call "num"), will give back whatever num is plus six.
scala> def bump(num:Int) = num + 6
bump: (Int)Int
Use the bump function on x, and see what we get
scala> bump(x)
res3: Int = 9
We can remember groups of things together in arrays. Let's make an array called nums, to hold this bunch of numbers.
scala> val nums = Array(4, 7, 9, 3, 1, 2)
nums: Array[Int] = Array(4, 7, 9, 3, 1, 2)
Make a new array called newnums out of nums, by mapping each of its values to a new value. For each value in nums (let's say call it x), the corresponding value in newnums will be x+7.
scala> val newnums = nums.map { _ + 7 }
newnums: Array[Int] = Array(11, 14, 16, 10, 8, 9)
Find all the nums that are more than 5. This will return a new array containing the elements that meet this condition.
scala> nums.filter { _ > 5 }
res6: Array[Int] = Array(7, 9)
Say hi! What this really doing is supplying a string of characters (marked in quotes) to a function called println, which does the "saying" around here.
scala> println("hello")
hello
Let's do something for each of our numbers. For each one, let's call it x, and print out hello alongside x.
scala> nums.foreach { (x) => println("hello " + x ) }
hello 4
hello 7
hello 9
hello 3
hello 1
hello 2
Do the same as above, but only for the numbers more than 5. What this is really doing is creating a new array out of nums containing only the ones more than 5, and doing the printing operation for each element in that.
scala> nums.filter { _ > 5 }.foreach { (x) => println("hello " + x ) }
hello 7
hello 9
What if we needed to remember something more complex? Say we had to work with people, and we needed to remember their name and age? We need to create a new type of variable to do that. Let's create the class Person, which contains values for name and age.
scala> case class Person(val name:String, val age:Int)
defined class Person
Let's create a person and store him in the value "fred"
scala> val fred = Person("Fred Bloggs", 28)
fred: Person = Person(Fred Bloggs,28)
Make another for Jane
scala> val jane = Person("Jane Doe", 24)
jane: Person = Person(Jane Doe,24)
Show me Fred's name, then Jane's age
scala> fred.name
res21: String = Fred Bloggs
scala> jane.age
res22: Int = 24
Is Fred older than Jane? This shows it's true.
scala> fred.age > jane.age
res24: Boolean = true
Define a function to determine the older of two people, then use it to see who's oldest out of fred and jane. Clearly we see that Fred is older.
scala> def oldest(p1:Person, p2:Person): Person = if (p1.age>p2.age) { p1 } else { p2 }
oldest: (Person,Person)Person
scala> oldest(fred, jane)
res25: Person = Person(Fred Bloggs,28)
As you can see it's not that hard to get started learning programming with Scala.
I don't know much about Scala -- in what way is Scala's shell typesafe that Ruby's or Python's isnt?
ReplyDeleteAlso: functional programming often really shines at expressing mathematical ideas the way that a math person would think about them, so it might be a natural choice for teaching programming to s/b who has "done tons of mathematics".
This comment has been removed by the author.
ReplyDeleteRuby and Pyhton are not statically typed, so offer much less type-safety than scala.
ReplyDeleteHowever, OCaml has a really great type safe shell (which has been around for almost two decades) and is in some sense even superior to scala.