All posts
Phel: the Lisp that compiles to PHP

Phel: the Lisp that compiles to PHP

The new Functional Programming language build-in for PHP.

blog-cover

A new Functional Programming language build-in on the PHP ecosystem.

The Phel Language

Phel is a Functional Programming (FP) language that compiles to PHP. It is a dialect of Lisp inspired by Clojure and Janet.

Features

  • Built on PHP’s ecosystem
  • Good error reporting
  • Different Datastructures (Arrays, Tables, and Tuples)
  • Macros
  • Recursive functions
  • Powerful but simple Syntax
  • REPL

Why Phel?

Phel is a creation of Jens Haase, and as he mentions on the website, “it is the result of many failed attempts to do functional programming in PHP”. He was looking for something with these characteristics:

  • A LISP-inspired
  • Functional Programming language
  • That runs on cheap hosting providers
  • It is easy to write and debug

From my point, it was ending May 2020 when I finished the book Seven Languages in Seven Weeks, in which I learned the basics of Prolog, Erlang, Clojure, Haskell… I even created a repository in GitHub to store my progress over the weeks with these learnings.

Suddenly, one day beginning of June 2020, I saw a post in Reddit’s PHP official channel, with a message of someone mentioning this “Phel” project, claiming to be a “functional programming language” written in PHP!

I work with PHP for around 8 years so far, and I always try to combine OOP with FP as much as possible (because I believe both paradigms are better combined; they are not necessarily exclusive), and a new dialect of Lisp fully written in native PHP 7.4 sounded really exciting. I decided to take a look at it. I wanted to see the code.

The fresh readings from Mathias NoBack books such as Object Design Style Guide, and Advanced Web Application Architecture, gave me the courage to try out what I learned and put it for real in a project. This was (and still is) a beautiful challenge, which helps me to test myself and grow professionally at the same time.

My first commit was done on 6 June 2020, and since then I’ve been dedicating a little time each day during my free hours to its development. It helps me to learn more about FP, languages internal design, and best practices on testing and software design architecture in general.

I started applying minor changes until I ended refactoring the whole compiler structure (among other things) in order to make the modules easier to read and understand. Psalm, strict types, good test coverage, unifying code style… These were some of my favorite topics.

Example of Phel code

The following example gives a short impression of how Phel looks like:

# Define a namespace
(ns my\example)
# Define a variable with name "my-name" and value "world"
(def my-name "world")
# Define a func with name "print-name" and one argument
(defn print-name [your-name]
  (print "hello" your-name))
# Call the function
(print-name my-name)

Current status of Phel

Check out the official website with all documentation: https://phel-lang.org/


Ssearch Dtheme Llang Jolder Knewer Ttoc Ccopy ?help

Keyboard Shortcuts

Navigation

HHome
BBlog
RReadings
LToggle language

Actions

SSearch
DToggle theme
CCopy URL
GGScroll to top

Posts

JOlder post
KNewer post
TToggle TOC

General

?Show this help
EscClose