Welcome to Phel! These exercises ease you into prefix notation, basic types, and the REPL feel. Fire up the Phel REPL and try each one before peeking at the solution.
Exercise 1 easy
Compute 1 + 1.
(+ 1 1)
In Phel (like every Lisp), the operator comes first. This is called prefix notation.
Learn more: Arithmetic
Exercise 2 easy
Compute (3 + 4.0 / 5) * 6.
(* (+ 3 (/ 4.0 5)) 6)
; => 22.8
Nested expressions evaluate inside-out. No precedence rules to memorize - the parens make the order obvious. Using 4.0 keeps the result a float; (/ 4 5) in Phel returns a rational 4/5, not 0.
Learn more: Arithmetic
Exercise 3 easy
Use str to glue "hello" and " world" together.
(str "hello" " world")
; => "hello world"
str concatenates any number of values into a string.
Learn more: Basic Types
Exercise 4 easy
Print "Hello, Phel!" to the screen.
(println "Hello, Phel!")
; Hello, Phel!
; => nil
println writes its arguments to stdout followed by a newline. The expression itself returns nil.
Learn more: Basic Types
Exercise 5 easy
Call get with "hello" and 1. What comes back?
(get "hello" 1)
; => "e"
Strings are indexable. get returns the character at index 1 (zero-based).
Learn more: Basic Types
Exercise 6 easy
Check whether (+ 2 3) equals 5.
(= (+ 2 3) 5)
; => true
= compares values for equality and works across any types.
Learn more: Truth and Boolean Operations
Exercise 7 easy
Predict each result, then run them:
(string? "hello")
(int? 42)
(nil? nil)
(nil? 0)
(string? "hello") ; => true
(int? 42) ; => true
(nil? nil) ; => true
(nil? 0) ; => false (0 is not nil!)
Predicates return true or false. By convention, their names end with ?.
Learn more: Basic Types
Exercise 8 easy
Predict each result, then run them:
(not true)
(and true false)
(or false true)
(and (> 5 3) (< 10 20))
(not true) ; => false
(and true false) ; => false
(or false true) ; => true
(and (> 5 3) (< 10 20)) ; => true
and returns the last truthy value or the first falsy one. or returns the first truthy value. They short-circuit, just like in PHP.
Learn more: Truth and Boolean Operations
Exercise 9 easy
What happens if you evaluate (+ 1 "hello")? Try it. Then inspect (type 42) and (type "hi").
(+ 1 "hello") ; => Error! Numbers and strings don't add.
(type 42) ; => :int
(type "hi") ; => :string
Phel surfaces type errors loudly. type returns a keyword describing any value - useful when something behaves unexpectedly.