Doodling With Swift: The Read-Eval-Print Loop

Written by: Reinder de Vries, September 15 2016, in App Development

Everything apps isn’t all Swift, and everything Swift isn’t all Xcode. You can code in the Swift programming language without Xcode – with numerous benefits.

In this article you’ll dive in the “REPL”, which is a way to code Swift without using Xcode (and all its overhead). Getting used to doodling Swift in a REPL can greatly increase your understanding of the language, and of programming, when you’re learning how to code.

Ready? Let’s go.


The REPL, or read-eval-print loop, is a simple interactive code editor in which you can type Swift code and let it execute immediately after. It does not have an extensive programming environment, like Xcode, nor does it have an iPhone Simulator. It’s pure Swift!

“REPL” looks like a weird name, but it is exactly what the editor does. First, it reads your code. It then evaluates that code, interpreting it line by line, and feeding it to a Swift compiler. Finally, the compiler executes the code and returns the output to your editor window.

Some REPLs literally read line-by-line, so you type one line, hit return, and the line is executed, returning the output back to you. Other editors allow you to type up an entire file of multi-line code, which is executed as a whole, and then returns the output to you, too.

If you’ve installed Xcode 8, you have access to a simple Swift REPL via the command-line. Start up Terminal, and type:

$ swift

(You don’t have to type the $, that’s just to indicate the example is a CLI command.)

The Swift REPL starts, and you see:

Welcome to Apple Swift version 2.2 (swiftlang-703.0.18.8 clang-703.0.30). Type :help for assistance.

You can immediately start typing. When you hit return, your code executes. Why don’t you try it?

var meaning = 42

Swift reports your code back to you, but nothing meaningful really happens – you’ve only declared and initialized a variable. Let’s have it print the variable back to us!



1> var meaning = 42
    meaning: Int = 42
2> print(meaning)


The Swift REPL has support for multi-line code, but it’s not very convenient. Try typing a opening squiggly bracket { and then hit return again. The REPL now indents your code for you, just like you would format your own code in Xcode.

Get 5 of my best practices

Learn how to build better iOS apps

I’ll show you exactly how I built a dozen professional iOS apps,
write extensible Swift code, and turn coffee into code.
Wait, what? Yup – into Swift code.

Crafting Swifty Sandbox Castles…

Let’s switch to a nicer REPL, the IBM Swift Sandbox:

It’s exactly what you think it is: a Swift editor in your browser window. Neat, right?

Remove the sample code, and start typing on line 1:

var meaning = 2 * 3 * 7

If you then press the Command-S key the editor executes your code and shows the result in the right window.


Now that we have a simple editor at our disposal, let’s code a bit more. Copy the following code over to your own sandbox.

func pythagoras(a:Double, b:Double) -> Double
    return sqrt(pow(a, 2) + pow(b, 2))

As you can see, we’re using the Pythagoras Theorem to calculate the length of a side “c” of a right-angled triangle based on sides “a” and “b”.

When you execute your code, you should see an error:

ERROR at line 3, col 12: use of unresolved identifier 'sqrt'
return sqrt(pow(a, 2) + pow(b, 2))

WHOAH! What happens here? The Swift compiler complains it doesn’t know the function sqrt – that’s because we haven’t imported it yet. Just like in Xcode, you need to import functions, classes etc. from the standard Swift library and Cocoa Touch SDKs. For the sqrt and pow functions, this comes from the Foundation library.

Add this at the top of the editor to import it:

import Foundation

Note: the sqrt simply takes the square root of the first argument of the function, and the pow returs the first argument x raised to the power y. The Pythagorean Theorem says a² + b² = c², so the square root of a² + b² equals c.

Next up, let’s actually use our function. Do you know by heart what a 3-4-n right-angled triangle’s c-side is? It’s 5 – one of those math tricks that got drilled into you during highschool. With it, we can test if our function does what it’s supposed to do:

Add this below the function, in the editor:

let c = pythagoras(a: 3, b: 4)

The entire code now looks like this:

import Foundation

func pythagoras(a:Double, b:Double) -> Double
    return sqrt(pow(a, 2) + pow(b, 2))

let c = pythagoras(a: 3, b: 4)

Execute! What does it say?


NICE! Well done.

(Another such “Pythagorean triple” is 5-12-n. What’s n, here?)

Build better iOS apps by mastering best practices and app architecture » Find out how

Let’s Nerd Out Over Maths, Code And Solving Problems

All experts paint and sketch artists “doodle” in their free time. Why wouldn’t a Swift coder play with a little code, too?

Programming “headless” Swift, on such a blank canvas, can seem daunting at first, but the sandbox and the REPL can greatly increase your skill in coding and help you come up with free-form snippets of code. The editor is bare-bones too, so it might help you increase your immediate knowledge of Swift syntax and keywords.

Programming and math are close cousins, because many programming principles found their roots in mathematics. Likewise, programming empowers math by adding the powers of automatic computation.

Ever heard of Project Euler? Take a look!

Project Euler is a set of maths problems that can be solved with programming. It helps you learn to code, learn how to solve problems, and hopefully will teach you something about code efficiency.

Writing effective algorithms is something too broad to cover in this article, but I want to point out Project Euler to you anyway.

Take the first problem, for example:

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.

Find the sum of all the multiples of 3 or 5 below 1000.

Can you code that in Swift? Good luck!

Reinder de Vries

Reinder de Vries is a professional iOS developer. He teaches app developers how to build their own apps at Since 2009 he has developed a few dozen apps for iOS, worked for global brands and lead development at several startups. When he’s not coding, he enjoys strong espresso and traveling.

Comments & Questions

Got a comment or question? Let me know! I read and respond to every one. Thanks!