Introduction to Functional Programming with Scheme (Part 1)

Scheme is a multi-paradigm language, developed in the 1970s by Guy L. Steele and Gerald Jay Sussman. Scheme is a minimal dialect of the LISP family, and although it supports both functional and procedural paradigms, Scheme is mainly functional. It is a language that is built with the purpose of learning about the core concepts of programming languages.

Procedural programming focuses on statements

Functional programming focuses on expressions

Expressions have values. A functional program is an expression who’s value is a sequence of instructions for the computer to carry out.

Statements don’t have values and instead modify the state of some conceptual machine.

omnimike

In this tutorial we will learn a dialect of Scheme called Racket.

To follow along, feel free to download DrRacket, the IDE of Racket. I will proceed to call Scheme and Racket terms interchangeably.

Racket

In programming languages such as C, Java and others, the procedure call is f(x,y)

In Scheme the equivalent is (f x y)

The basic primitives of Scheme are:

  • Booleans: #t, #f
  • Characters: #\a, #\B
  • Numbers: 123, 1.23e+10, 3/4
  • Vectors: #( 1 2 3 4 5)
  • Strings: “Hello world!”
  • Symbols: symbol-a, symbol-b
  • Pairs: (x . z)
  • Lists: (1 2 3 4 5)

To write comments we use ;

Writing our first procedure

+ is a built-in procedure, hence, to add two numbers we can call (+ 1 2) which yields the expression 3

We can create our own procedure that adds two to a number

#lang racket

(lambda (x) (+ x 2))
; Output => #<procedure>Code language: PHP (php)

Now this lambda is an anonymous function that we can use directly instead of the above +

#lang racket

((lambda (x) (+ x 2)) 1) 
; Output => 3Code language: PHP (php)

We can do better, we can store the lambda in a variable:

#lang racket

(define add2
  (lambda (x) (+ x 2)))
;And then we can reference it
(add2 1)
; Output => 3Code language: PHP (php)

Lets make a define a procedure the calculate the cube of a number

#lang racket

(define cube
   (lambda (x) (* x x x)))
;And then we can reference it
(cube 3)
; Output => 27Code language: PHP (php)

We use a quote ‘ if we would like to stop the evaluation of a list, for example, if we would like to find if a number exists in a list:

#lang racket
(member 10 '(1 4 5 10 9 7))
; If we don't use quote ', we will get an error because the compiler will evaluate it as (f arg1 arg2 arg3) and not as a list
; Output => '(10 9 7) ; It returns the sublist from which the number 10 begins

(member 50 '(1 4 5 10 9 7))

; Output => #f ; False since 50 doesn't exist within the list
Code language: PHP (php)

Recursion in Scheme

The classical recursion Hello World is the factorial. Please note that if is a keyword, and not a procedure, (if cond a b) executes a if cond is true, otherwise b

(define (factorial n)
  (if (= n 0) ; Checking for the base case
      1 ; if the base case is true we return 1
      (* n (factorial (- n 1))))) ; otherwise we recurse passing n-1 as an argument
Code language: JavaScript (javascript)

With that, we conclude Part 1 of the Introduction to Functional Programming with Scheme.

You can find the above code on my github

I would like to express my gratitude for professors Matteo Pradella & Michele Chiari who taught me the course Principles of Programming Languages at PoliMi

In the upcoming parts, we will dive deeper into Scheme and its constructs

Brace yourself for even more parentheses 😀

Thanks for reading! and stay tuned.

2 thoughts on “Introduction to Functional Programming with Scheme (Part 1)

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.