posts: Add post about picture language.
[software/elephly-net.git] / posts / 2018-09-01-guile-picture-language.md
1 title: A simple picture language for GNU Guile
2 date: 2018-09-01 23:00
3 tags: planet-fsfe-en, free software, guile
4 ---
5
6 One thing that I really love about Racket is its [picture
7 language](https://docs.racket-lang.org/pict/), which allows you to
8 play with geometric shapes in an interactive session in Dr Racket.
9 The shapes are displayed right there in the REPL, just like numbers or
10 strings. Instead of writing a programme that prints "hello world" or
11 that computes the Fibonacci numbers, one could write a programme that
12 composes differently rotated, coloured shapes and prints those
13 instead.
14
15 I use [GNU Guile](https://gnu.org/software/guile) for my own projects,
16 and sadly we don't have an equivalent of Racket's picture language or
17 the Dr Racket editor environment. So I made something: a [simple
18 picture language for GNU
19 Guile](https://git.elephly.net/software/guile-picture-language.git).
20 It provides simple primitive procedures to generate shapes, to
21 manipulate them, and to compose them.
22
23 Download the single Guile module containing the implementation:
24
25 mkdir ~/pict
26 wget https://elephly.net/downies/pict.scm
27
28 To actually see these shapes as you play with them, you need to use a
29 graphical instance of [GNU Emacs](https://gnu.org/software/emacs) with
30 [Geiser](http://www.nongnu.org/geiser/).
31
32 Start geiser in Emacs and load the module:
33
34 M-x run-guile
35 (add-to-load-path (string-append (getenv "HOME") "/pict"))
36 ,use (pict)
37
38 Let’s play!
39
40 (circle 100)
41
42 If you see a pretty circle: hooray! Let’s play some more:
43
44 (colorize (circle 100) "red")
45 (disk 80)
46 (rectangle 50 100)
47
48 Let's compose and manipulate some shapes!
49
50 ,use (srfi srfi-1)
51 ,use (srfi srfi-26)
52 (apply hc-append
53 (map (cut circle <>)
54 (iota 10 2 4)))
55
56 (apply cc-superimpose
57 (map (cut circle <>)
58 (iota 10 2 4)))
59
60 (apply hc-append
61 (map (cut rotate (rectangle 10 30) <>)
62 (iota 36 0 10)))
63
64 (apply cc-superimpose
65 (map (cut rotate (triangle 100 300) <>)
66 (iota 36 0 10)))
67
68 There are many more procedures for primitive shapes and for
69 manipulations. Almost all procedures in pict.scm have docstrings, so
70 feel free to explore the code to find fun things to play with!
71
72 PS: I realize that it's silly to have a blog post about a picture
73 language without any pictures. Instead of thinking about this now,
74 get the module and make some pretty pictures yourself!