Clojure at the Belarusian State University

    BSU logo
    Clojure logo
    In this semester (in the fall of 2012), an optional Clojure language was held in the Belarusian State University. The post will describe the optional: reasons, program, tasks, results, impressions.


    I am a 5th year student of the Faculty of Applied Mathematics and Computer Science at BSU. I am fond of functional programming, but in the course of our studies at the university, FP is not considered at all, and only OOP (java, c ++) passed from programming paradigms. I believe that students who study in the field of computer science should have at least some idea of ​​FP (and preferably other paradigms). Therefore, I decided to try to at least slightly correct the situation and hold an elective. And since As a whole I have little experience and knowledge for conducting electives on AF, I decided to focus on a more applied and simple one - in the Clojure language. Clojure is a functional language, a modern dialect of Lisp, executed mainly on the JVM.

    Screenshots of the tasks that had to be solved during the course will be used as pictures.

    Task 2. The snake.
    The snake. The task is to write a bot.

    Course program

    1. Introduction to Clojure. Description of the syntax. Functions Anonymous features. Simple arithmetic and logical operations. Recursion.
    2. Work with collections. Different types of collections: vector, set, associative array (map). Functions for working with them: map, reduce, filter, etc. The immutability of collections.
    3. Work with collections. Lazy computing. Destructuring.
    4. Tools for working with mutable state. Atoms, agents, refs (ref). Transactional memory.
    5. Macros.
    6. The principle of operation of immutable data structures. Dismantled the implementation of PersistentVector and PersistentHashMap
    7. Multimethods.
    8. Protocols, types, and records (protocols, types, records).
    9. A bit of λ-calculus. Arithmetic and logical values ​​in the λ-calculus.
    10. λ-calculus. Recursion. Implementation of lists in the λ-calculus. An example of a typed λ-calculus.

    Task 3. Fool.
    The game "Fool". Player bot - below.


    After each lesson, homework was given. There were 2 types of homework: solving tasks on 4clojure and solving interactive problems. 4сlojure - a site with a set of small tasks from very simple to very complex and online verification. There is a convenient feature on 4clojure - the ability to watch the decisions of other participants after solving a problem. I think it helped to learn how to solve the problem “more functionally”.

    Interactive tasks - tasks that must be solved on Clojure, while the solution to most of them is visualized in advance, i.e. one could look at the result of the decision. For visualization, we used the Quil libraries (wrapper over Processing for the treasure) and Incanter(used to plot charts).

    Task list:
    • Air defense. You need to shoot down the target by setting the angle of the shot. There were 2 options. The first - excluding gravity, the rocket flies in a straight line, the target also flies in a straight line. The second option took into account gravity (you can shoot a canopy), the target hangs in the air without moving.
    • The snake. Implement a bot for a snake. The bot must take into account the presence of walls.
    • Fool. Implement a bot for the card game "Fool". Basically implemented the bot without remembering previous states. But one person nevertheless wrote a bot with memorization.
    • Cellular automaton. To implement a “framework” for a 2-dimensional cellular automaton: write a visualization, make it possible to interact with the program (stop, change the state of cells), and most importantly, make it possible to easily connect arbitrary rules (such as Life, Brian's Brain , Wireworld and others).
    • k-means. Implement the k-means algorithm with a fixed k. And also try to write an algorithm that will select the best k.
    • Character differentiation. Write a function that takes an input klozhurnoe expression, for example (* 2 x), the output gives the derivative: (+ (* 0 x) (* 2 1)). Simplifying the result was not required. Visualization - plotting a function and its first and second derivatives.
    • λ-calculus. Implement arithmetic functions (+, -, *, ^). Write a recursive function to calculate the sum of the arithmetic progression of numbers from 1 to n. Implement functions for working with lists (head, tail, cons, empty?). An additional task is to write map and reduce for such lists.

    Task 4. Cellular automaton.
    Cellular automaton Wireworld. The machine implements a logical XOR.


    Classes were held on Saturdays, 1 time per week. For out-of-class communication, a Google group was used. 4clojure assignments were naturally checked on the site itself. To solve interactive problems, students forked a repository on a github and decided in fork. It was very convenient to check, especially considering that the github allows you to leave comments on commits. So I could point to inefficient or not very beautiful places in the solutions. Of course, at the beginning everyone had imperative decisions using explicit cycles, often using variable states and atoms where they were actually superfluous. Unfortunately, I can’t accurately assess how much the decisions have changed by the end of the elective, how much they have become more functional, but I hope they did.


    There was an idea - that everyone was divided into groups of 2-3 people and each group made its own project during the semester. The following options were proposed: a multiplayer snake, a simple physics engine, a utility for processing text documents (statistics, something else), to make an analogue of this application. And of course you could offer your own options. But the idea somehow did not find a response. Perhaps homework was enough, given that my elective was voluntary and was not related to studying at the university.

    Task 6. Derivatives.
    Character differentiation. f = sin (2x). Pictured f, f ', f' '

    Motivation and Gifts

    At the beginning of the elective, the homework was not controlled in any way. And it was somehow poorly implemented, although judging by the anonymous survey it was “interesting” ... Then the idea came up to reward some kind of goodies for solving homework. After discussion with friends, an idea was offered that I really liked: the one who does more than 50% of the homework will receive a personalized mug with his result. So there was an incentive to perform 100%, so that later 90% of the eyes would not be callous. To do this, a rating was created - a tablet in googlodok, which kept track of the success of students.

    By the way, at the end I asked if this helped in the assignment. Everyone said yes. Most motivated a rating in which they could see their progress. Some were motivated by the mug itself :)


    According to the final survey, we can conclude that I liked the elective. I think it was useful, even if they won’t use clojure or AF in everyday life. As for me, I really liked it. By the way, the idea of ​​electives arose in pairs of pedagogy, where we all complained that our higher education was bad and that we were taught nonsense to no one. Then our teacher asked why we ourselves are not trying to do anything about this.

    Thanks to my friends for ideas with tasks, circles and help in paying for circles :)

    And finally, a few facts:
    • About 10 people attended the elective, mainly 2 year students.
    • 65 tasks were set with 4clojure. On average, everyone decided 59.
    • 8 interactive tasks were set. On average, everyone decided 6.
    • One student found 1 serious bug in Quil.
    • 3 students completed the tasks 100%. Average result: 88%
    • Everyone who did the homework and held out until the end - got a mug. There are 8 such people. 2 people attended, but did not complete tasks.

    Named circles.

    If anyone would be interested in generating images for mugs, the script is available on the github:

    Also popular now: