Functional programming using JavaScript and the HTML5 canvas element
Michael Grady · Journal of computing sciences in colleges · 2010
Functional programming has several important benefits, including ease of testing and debugging, effective and robust implementation of concurrency and the ability to construct higher order functions. These advantages are illustrated at many points in the computer science classic Structure and Interpretation of Computer Programs (SICP) by Abelson and Sussman. For example, in chapter two, they implement a picture language invented by Henderson for creating images like MC Escher's Square Limit. This provides a beautiful example of the power of data abstraction and closure which also involves higher order procedures in a natural way. Their approach, based on the Scheme programming language, is easily adapted to JavaScript using the web browser as the graphics environment. Owing to the high level of interest that many students have in the web, this new setting has proven to be highly motivating and effective for illustrating the power of functional programming to undergraduates. This paper begins with a description of JavaScript programming using the functional style, inspired by the approach given in SICP. Along the way, a brief introduction to the HTML5 canvas element and its programmatic control under JavaScript will be given.