Your first encounter with Phel! These exercises will get you comfortable with prefix notation, basic expressions, and calling functions. Fire up the REPL and follow along.
Exercise 1: Compute 1 + 1
(+ 1 1)
In Phel (like all Lisps), the operator comes first. This is called prefix notation.
Learn more: Arithmetic
Exercise 2: Compute (3 + 4 / 5) * 6
(* (+ 3 (/ 4 5)) 6)
Nested expressions are evaluated from the inside out. No operator precedence rules to memorize!
Learn more: Arithmetic
Exercise 3: Use the str function to join the strings "hello" and " world" together.
(str "hello" " world")
# => "hello world"
str concatenates any number of strings together.
Learn more: Basic Types
Exercise 4: Call the function get with arguments "hello" and 1. What does it return?
(get "hello" 1)
# => "e"
Strings are indexable! get retrieves the character at position 1 (zero-based).
Learn more: Basic Types
Exercise 5: Check if two values are equal: is (+ 2 3) the same as 5?
(= (+ 2 3) 5)
# => true
The = function compares values for equality. It works with any types.
Learn more: Truth and Boolean Operations
Exercise 6: Try these predicates and guess the result before running 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 are functions that return true or false. By convention, their names end with ?.
Learn more: Basic Types
Exercise 7: Use not, and, and or to evaluate these expressions. Predict the result first!
(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
Boolean operators work as you'd expect. and returns the last truthy value or the first falsy one; or returns the first truthy value.
Learn more: Truth and Boolean Operations
Exercise 8: What happens if you evaluate (+ 1 "hello")? Try it! What about (type 42) and (type "hi")?
(+ 1 "hello") # => Error! You can't add a number and a string.
(type 42) # => "int"
(type "hi") # => "string"
Phel will tell you when types don't match. The type function helps you inspect any value.