Use commonly familiar ones, which do not require a math background. Like:
- Text and words
- Simple Websites
- Simple arithmetic
Not simple enough:
- Geometry with Processing
- https://www.infoq.com/presentations/Simple-Made-Easy (By Rich Hickey)
- Complex constructs: State, Object, Methods, Syntax, Inheritance, Switch/matching, Vars, Imperative loops, Actors, ORM, Conditionals.
- Simple constructs: Values, Functions, Namespaces, Data, Polymorphism, Managed refs, Set functions, Queues, Declarative data manipulation, Rules, Consistency.
- This means, teach mostly functional concepts
- Only introduce few important concepts to keep it simple
- Use simple problems first, which are well understood and felt
- Capitalize first letter of word, how can we do that for every word in the sentence
- Abstracting over repeating code with functions needs written repeated code first
- Like in "Seven Sketches in Compositionality: An Invitation to Applied Category Theory" (https://arxiv.org/pdf/1803.05316.pdf)
- Learning curve is increasing in each chapter, but then starting low again in the next one.
A simple program, which writes the text "Hello World." to the screen:
println("Hello World.")
println("Another text") // everything after two slashes is ignored
// This is called a "comment" and is useful for explanations
println(7) // numbers can also be printed
println(7 + 1) // a simple calculation. Prints 8
println("Hello 7 + 1") // prints: Hello 7 + 1
// (7 + 1 belongs to the text and is not a calculation)
val x = 7 // val stands for "value"
println(x) // prints 7
val myCalculation = 7 + 5 - 3
println(myCalculation) // prints 9
val myText = "I am a computer"
println(myText) // prints: I am a computer
val radius = 5
val diameter = radius * 2
val lengthOfCircumference = diameter * 3.141 // https://en.wikipedia.org/wiki/Circle#Length_of_circumference
println(lengthOfCircumference) // prints 31.41
val fruits = List("Banana", "Apple", "Orange")
println(fruits) // prints: List(Banana, Apple, Orange)
println(fruits.size) // prints: 3
val numbers = List(1,2,5)
println(numbers) // prints: List(1, 2, 5)
val names = List("John", "Maria", "Peter")
// take the first element is at position 0
println(names(0)) // prints: John
println(names(1)) // prints: Maria
println(names(2)) // prints: Peter
println(names(3)) // error: IndexOutOfBoundsException: 3
val subject = "The mouse"
val verb = "walks"
val sentence = s"$subject $verb." // construct new strings out of values
println(sentence) // prints: The mouse walks.
val random = util.Random.nextInt(6) // random number between 0 and 5.
val diceRoll = random + 1 // random number between 1 and 6
println(diceRoll) // example print: 3
val animals = List("Dog", "Cat", "Mouse", "Fish")
val randomPosition = util.Random.nextInt(4) // there are 4 animals
val randomAnimal = animals(randomPosition)
println(randomAnimal) // prints a random animal
val subjects = List("The dog", "My friend", "A pig") // 3 subjects
val verbs = List("eats", "hugs", "is looking at", "enjoys") // 4 verbs
val objects = List("an apple", "a fish", "a lemon", "a glass of water", "a program") // 5 objects
val randomSubjectPosition = util.Random.nextInt(3)
val randomSubject = subjects(randomSubjectPosition)
val randomVerbPosition = util.Random.nextInt(4)
val randomVerb = verbs(randomVerbPosition)
val randomObjectPosition = util.Random.nextInt(5)
val randomObject = objects(randomObjectPosition)
val randomSentence = s"$randomSubject $randomVerb $randomObject."
println(randomSentence)
// We wrote 3 times very similar code to get a random element from a list:
val randomObjectPosition = util.Random.nextInt(5)
val randomObject = objects(randomObjectPosition)
// We can create a new command for that:
def randomElement(list: List[String]) = { // "def" stands for "define"
// the command is called "randomElement" and only accepts lists which contain strings
val numberOfElements = list.size
val randomPosition = util.Random.nextInt(numberOfElements)
val randomElement = list(randomPosition)
randomElement // the last value is returned
}
val planets = List("Earth", "Jupiter", "Saturn")
val randomPlanet = randomElement(planets)
println(randomPlanet)
val subjects = List("The dog", "My friend", "A pig") // 3 subjects
val verbs = List("eats", "hugs", "is looking at", "enjoys") // 4 verbs
val objects = List("an apple", "a fish", "a lemon", "a glass of water", "a program") // 5 objects
val randomSubject = randomElement(subjects)
val randomVerb = randomElement(verbs)
val randomObject = randomElement(objects)
val randomSentence = s"$randomSubject $randomVerb $randomObject."
println(randomSentence)
val numbers = List(7, 3, 4, 2, 5)
val smallestNumber = numbers.min // 2
val numbers = List(7, 3, 4, 2, 5, 4, 3)
val removedFirstFour = numbers.diff(List(4)) // List(7, 3, 2, 5, 4, 3)
val removedFirstFourAndThree = numbers.diff(List(4,3)) // List(7, 2, 5, 4, 3)
val numbers = List(7, 3, 4, 2, 5, 4, 3)
val smallestNumber = numbers.min // 2
val removedSmallest = numbers.diff(List(smallestNumber)) // List(7, 3, 4, 5, 4, 3)
val list = List(2, 3)
val listWithPrependedElement = 1 :: List(2,3) // List(1, 2, 3)
val emptyList = Nil
val listWithTwoElements = 7 :: 3 :: emptyList // List(7, 3)
// Int stands for Integer
def moveSmallestNumberToFront(list:List[Int]):List[Int] = {
val smallest = list.min
smallest :: list.diff(List(smallest))
}
moveSmallestNumberToFront(List(7, 3, 4)) // List(3, 7, 4)
moveSmallestNumberToFront(List(1, 1, 2)) // List(1, 1, 2)
moveSmallestNumberToFront(List()) // UnsupportedOperationException: empty.min
def moveSmallestNumberToFront(list:List[Int]):List[Int] = {
if( list.isEmpty ) {
List()
} else {
val smallest = list.min
smallest :: list.diff(List(smallest))
}
}
moveSmallestNumberToFront(List(7, 3, 4)) // List(3, 7, 4)
moveSmallestNumberToFront(List(1, 1, 2)) // List(1, 1, 2)
moveSmallestNumberToFront(List()) // List()
val list = List(7, 3, 4, 2)
val smallest = list.min // 2
val rest = list.diff(List(smallest)) // List(7, 3, 4)
val smallest2 = rest.min // 3
val rest2 = rest.diff(List(smallest2)) // List(7, 4)
val smallest3 = rest2.min // 4
val rest3 = rest2.diff(List(smallest3)) // List(7)
val smallest4 = rest3.min // 7
val rest4 = rest3.diff(List(smallest4)) // List()
val sorted = List(smallest, smallest2, smallest3, smallest4)
def sort(list:List[Int]):List[Int] = {
val smallest = list.min
println(s"smallest: $smallest")
val rest = list.diff(List(smallest))
println(s"rest: $rest")
smallest :: sort(rest) // use sort again, but only on the rest
}
val list = List(7, 3, 4, 2)
sort(list)
// smallest: 2
// rest: List(7, 3, 4)
// smallest: 3
// rest: List(7, 4)
// smallest: 4
// rest: List(7)
// smallest: 7
// rest: List()
// java.lang.UnsupportedOperationException: empty.min
def sort(list:List[Int]):List[Int] = {
if( list.isEmpty ) {
List()
} else {
val smallest = list.min
val rest = list.diff(List(smallest))
smallest :: sort(rest) // use sort again, but only on the rest
}
}
val list = List(7, 3, 4, 2)
sort(list) // List(2, 3, 4, 7)