Author Archives: Steve Downey

C++ code in Org-mode

1 C++

This is a simple example with one c++ file, all in one src code block.

#include <iostream>

int main()
    std::cout << "Hello World++! 2.9";

The src block looks like:

    #+HEADERS: :tangle hello.c++ :exports code :eval never
    #+BEGIN_SRC C++
    // source code

The HEADERS block is on a separate line because when the buffer is evaluated, code will get run, and the SRC blocks will get rewritten, as well as the RESULTS blocks. Since we want the headers to be preserved, we can’t make them part of the SRC block.

You can also tangle a Makefile.

    -rm hello

hello:  hello.c++
    clang++ hello.c++ -o hello

With the org mode headers exporting the code, and not evaluating this block. Just like the C++ code. We’ll evaluate the makefile, and run the program, a bit further down.

    #+HEADERS: :tangle Makefile :exports code :eval never
    #+BEGIN_SRC makefile
    # Makefile

Now, we tangle the code out to the files.

    #+NAME: tangle-buffer
    #+HEADERS: :exports both :results value
    #+BEGIN_SRC emacs-lisp

That will write out two files when the buffer is evaluated using org-babel-execute-buffer, bound to \C-c \C-v b.

Give this block a name, so that where the results go can be controlled. Do that by giving the RESULTS block the name of the SRC block. Org will then produce a table of the results of executing the elisp, which is the two files produced.

And put the results here:

hello.c++ Makefile

Next, we run make with the target to compile the code. You could also simply write the compiler command here.

    #+NAME: make-clean-hello
    #+BEGIN_SRC sh :exports both :results output
    make clean
    make hello
make clean
make hello

And make will run our compilation as spec’d in the Makefile we just tangled out.

rm hello
Makefile:2: recipe for target 'clean' failed
clang++ hello.c++ -o hello

And now get the output by running the program.

    #+NAME: run-hello
    #+BEGIN_SRC sh :exports results

Which prints out our hello, world text. Which is has version number to convince myself it gets updated.

Hello World++! 2.9

2 Raw Document

Org mode in org is a little tricky, since to show the examples, you have to quote them.

Original document is available on GitHub

Building Emacs 25.1 on Ubuntu 16.10

1 Why notes

Making notes so I don’t forget, although the key problem is fixed upstream.

Ubuntu 16.10 (Yakkety Yak) has made a critical change to the system compiler, and everything is by default built with position independent executable support (PIE), in order to get better support for address space layout randomization. Here are the security notes for PIE. Emacs does not dump successfully with this. The compiler option -no-pie needs to be added into CLFAGS.

The option also means that static libraries you’ve built before will probably need to be rebuilt. See the link above for typical errors.

2 Getting Ready

First get dpkg-dev, g++, gcc, libc, make:

sudo apt-get install build-essentials

Then get the full set of build dependencies for last emacs, emacs24:

sudo apt-get build-dep emacs24

Decide if you want to build just this version, or track emacs. I track from git, because. So I have a directory for emacs where I have master, emacs25, and build directories. I try to avoid building in src dirs. It makes it easier to try out different options without polluting the src.

mkdir -p ~/bld/emacs
cd ~/bld/emacs
git clone git://
cd emacs.git
git worktree add ../emacs-25.1 emacs-25.1
cd ..
mkdir bld-25.1

3 Configure and build with magic option

Now configure in the build directory:

cd bld-25.1
../emacs-25.1/configure \
  --prefix=~/install/emacs-25.1 \
  --with-x-toolkit=gtk3 \
  --with-xwidgets \

I built with xwidget support to play with the embedded webkit widget. It’s not really useable as a browser, but has uses for rendering. I also install into a local program directory, under my homedir.

Build and install:

make install

I have a bin directory early in $PATH so that I can select versions of local software ahead of system software.

cd ~/bin
ln -s ~/install/emacs-25.1/bin/emacs
ln -s ~/install/emacs-25.1/bin/emacsclient

Now you should have a working emacs 25.1 available.

Real World Haskell – Chapter 3

These are the exercises from chapter 3 of
Real World Haskell
by Bryan O’Sullivan, Don Stewart, and John Goerzen

> module RWHChapter3 where

{-# OPTIONS_GHC -XMagicHash #-}

Some useful things to check my work:

> import Test.QuickCheck
> import Data.List
> import GHC.Prim
> import GHC.Base

1) Write a function that computes the number of elements in a list. To test it, ensure that it gives the same answers as the standard length function.

> lengthList (x:xs) = 1 + lengthList xs
> lengthList [] = 0

check that it works by running

quickCheck (\s -> length s == lengthList s)

at the interactive prompt

> ghclength l = len l 0#
> where
> len :: [a] -> Int# -> Int
> len [] a# = I# a#
> len (_:xs) a# = len xs (a# +# 1#)

is the definition used by GHC.
It doesn’t stack overflow on ghclength [1..1000000]
lengthList [1..1000000] does, at least in ghci

2) Add a type signature for your function to your source file.

> lengthList :: [a] -> Integer

3. write a function that computes the mean of a list, i.e. the sum of all elements in the list divided by its length. (you may need to use the fromintegral function to convert the length of the list from an integer into a floating point number.)

> listMean x = sum x / fromIntegral (length x)

4. Turn a list into a palindrome, i.e. it should read the same both backwards and forwards. For example, given the list [1,2,3], your function should return [1,2,3,3,2,1].

> makePalindrome x = x ++ reverse x

5. Write a function that determines whether its input list is a palindrome.

> testPalindrome x = x == reverse x

> testPalindrome2 x = (take (halfLength x) x)
> == (take (halfLength x) (reverse x))
> where halfLength x = ((length x) `quot` 2)

reversing the whole list isn’t exactly right

> testPalindrome3 x = (take (halfLength x) x)
> == reverse (drop (halfLength x) x)
> where halfLength x = ((length x) `quot` 2)

except now it thinks that odd length lists aren’t palindromes.

6. Create a function that sorts a list of lists based on the length of each sublist. (You may want to look at the sortBy function from the Data.List module.)

> sortByLength = sortBy (\a b -> compare (length a) (length b))

7 An intersperse with type intersperse :: a -> [[a]] -> [a], such that
ghci> myintersperse ‘,’ []
ghci> myintersperse ‘,’ [“foo”]
ghci> myintersperse ‘,’ [“foo”,”bar”,”baz”,”quux”]

> myintersperse :: a -> [[a]] -> [a]
> myintersperse _ [] = []
> myintersperse _ [x] = x
> myintersperse c (x:xs) = x ++ [c] ++ (myintersperse c xs)

8. Height of a tree

> data Tree a = Node a (Tree a) (Tree a)
> | Empty
> deriving (Show)

depth’ :: Tree a -> Int

> depth’ Empty = 0
> depth’ (Node _ b c) = 1 + (max (depth’ b) (depth’ c))

9. Consider three two-dimensional points a, b, and c. If we look at the angle formed by the line segment from a to b and the line segment from b to c, it either turns left, turns right, or forms a straight line. Define a Direction data type that lets you represent these possibilities.

> data Direction = DLeft
> | DRight
> | DStraight
> deriving (Show)

> data Point = Point {
> x::Double,
> y::Double
> } deriving (Show)

10. Write a function that takes three 2D Points and returns a direction.

> direction p1 p2 p3
> | signCross p1 p2 p3 > | signCross p1 p2 p3 > 0 = DLeft
> | signCross p1 p2 p3 == 0 = DStraight
> where signCross (Point x1 y1) (Point x2 y2) (Point x3 y3) =
> (x2 – x1) * (y3 – y1) – (y2 – y1) * (x3 – x1)

11. Define a function that takes a list of 2D points and computes the direction of each successive triple. Given a list of points [a,b,c,d,e], it should begin by computing the turn made by [a,b,c], then the turn made by [b,c,d], then [c,d,e]. Your function should return a list of Direction.

> directionList :: [Point] -> [Direction]
> directionList ([]) = []
> directionList (_:[]) = []
> directionList (_:_:[]) = []
> directionList (x:x’:x”:[]) = [(direction x x’ x”)]
> directionList (x:x’:x”:xs) = (direction x x’ x”) : directionList (x’:x”:xs)

I don’t particularly like the pattern match on the three x’s. How
about a more general function to do sliding windows of data across the

> window :: Int -> [a] -> [[a]]
> window2 :: Int -> [a] -> [[a]]

> window n xs = if (length xs) > then []
> else (take n xs) : window n (tail xs)

> window2 n xs | (length xs) > window2 n x@(_:xs) = (take n x) : window2 n xs

> direction2 (x1:x2:x3:[]) = direction x1 x2 x3

> directionList2′ (x:xs) = direction2 x : directionList2′ xs
> directionList2′ [] = []

> directionList2 x = directionList2′ (window2 3 x)

I’m still working on the convex hull problem

tabbed working notes


Electoral Votes and Poll Closing Times

7:00 PM         Georgia         15
7:00 PM        Indiana        11
7:00 PM        Kentucky 8
7:00 PM        South Carolina 8
7:00 PM         Vermont        3
7:00 PM        Virginia 13
7:30 PM        North Carolina 15
7:30 PM        Ohio        20
7:30 PM        West Virginia 5
8:00 PM        Alabama        9
8:00 PM        Connecticut 7
8:00 PM        Delaware 3
8:00 PM        Florida        27
8:00 PM        Illinois 21
8:00 PM        Maine        4
8:00 PM        Maryland 10
8:00 PM        Massachusetts 12
8:00 PM        Mississippi 6
8:00 PM        Missouri 11
8:00 PM        New Hampshire 4
8:00 PM        New Jersey 15
8:00 PM        Oklahoma 7
8:00 PM        Pennsylvania 21
8:00 PM        Tennessee 11
8:30 PM        Arkansas 6
9:00 PM        Arizona        10
9:00 PM        Colorado 9
9:00 PM        Kansas        6
9:00 PM        Louisiana 9
9:00 PM        Michigan 17
9:00 PM        Minnesota 10
9:00 PM        Nebraska 5
9:00 PM        New Mexico 5
9:00 PM        New York 31
9:00 PM        North Dakota 3
9:00 PM        Rhode Island 4
9:00 PM        South Dakota 3
9:00 PM        Texas        34
9:00 PM        Wisconsin 10
9:00 PM        Wyoming        3
10:00 PM Iowa        7
10:00 PM Montana        3
10:00 PM Nevada        5
10:00 PM Utah        5
11:00 PM California 55
11:00 PM Hawaii        4
11:00 PM Idaho        4
11:00 PM Oregon        7
11:00 PM Washington 11
1:00 AM        Alaska        3


Take a picture of yourself right now.
don’t change your clothes, don’t fix your hair…just take a picture.
post that picture with NO editing.
post these instructions with your picture.

Trying to operatate an iPhone backwards makes me frown.