Skip to content

Latest commit

 

History

History
176 lines (107 loc) · 3.18 KB

intro-functional.md

File metadata and controls

176 lines (107 loc) · 3.18 KB

Introduction to Functional Programming

THIS IS WORK IN PROGRESS

Basics

Venice is a Lisp dialect and recognizes two kinds of structures:

1. Literal representations of data

1                   ; a number
"foo"               ; a string
true                ; a boolean
["abc" "de" "fgh"]  ; a vector of strings
{"a" 100 "b" 200 }  ; a map with strings as keys and numbers as values

2. Operations, this is how you do things

All operations take the form (, operator, operands, ) and return always a value:

(operator operand-1 operand-2 ... operand-n)

For example, if you want to add numbers

(+ 1 2 3 4 5)

or concatenate strings:

(str "Hello" ", " "user") 

Operations can be nested:

(+ 1 2 (* 3 4) (/ 20 4) 6)

This is all about the syntax in Venice. Control flow operations follow this structure too. Other languages like C, Java, Scala use different structures depending on the operator and the operands like for if, for, case, ... statements.

Venice is structurally uniform, no matter which operator you’re using or what kind of data you’re operating on, the structure is the same.

In functional parlance Venice is based on symbolic expressions, in-short s-expression.

Control Flow

In Venice, flow control operators are expressions too. if and do are the two basic control flows. More complex control flows like case are based on these fundamental operations.

if

if is the most important conditional expression. It consists of a predicate, a "then", and an "else" part. if will only evaluate the branch selected by the predicate. It returns the value of the evaluated branch.

(if predicate then-expression else-expression)

Example:

(if (< 10 100) 
  "lower than 100" 
  "equal or larger than 100")

do

do blocks sequentially execute multiple expressions. The value of the last expression is returned.

(do
  (println 100) 
  (println 200) 
  (println 300)
  20)

if only takes a single expression for the "then" and "else" parts. Use do to build larger blocks that are a single expression.

(if (even? 10) 
  (do  
    (println "10 is even")
    "even") 
  (do  
    (println "10 is odd")
    "odd"))

Introduction for Beginners

TODO

Variables

Functions

Immutability

Let and Local Variables

Filter-Map-Reduce

Control Structures

Loops

standard functions: (apply + [1 2 3 4 5]) (reduce + [1 2 3 4 5]) (filter even? [1 2 3 4 5])

recursion: loop-recur

Functional Concepts

Functional Programming is all about programming with functions.

Features

  • Pure Functions / Referential Transparency
  • Function Composition
  • Anonymous Functions
  • Higher Order Functions
  • Partial Function Application
  • Closures - returning functions from functions
  • Data Immutability
  • Lists are the fundamental data structure

Pure Functions

TODO

Recursion

TODO

Referential Transparency

TODO

First-Class Functions

TODO

Immutability

TODO