Lojic Technologies

Upgrading to Emacs 24.5

leave a comment »

I previously wrote a post about switching from Carbon Emacs to Gnu Emacs. Upgrading is pretty simple now, so I thought I’d record the procedure for installing Emacs from source on OSX for future reference:

  1. Go here and click on a mirror link
  2. Download the appropriate file, e.g. emacs-24.5.tar.gz
  3. tar xzf emacs-24.5.tar.gz
  4. cd emacs-24.5
  5. ./configure –with-ns
  6. make install
  7. drag nextstep/Emacs.app to the Applications directory

Emacs is still awesome!

Written by Brian Adkins

January 27, 2016 at 9:40 pm

Posted in Uncategorized

How to Write a Spelling Corrector in Racket

with one comment

In September, 2008, I translated Peter Norvig’s spelling corrector into Ruby. My current favorite language is Racket, so I thought it would be a good exercise to port it to Racket. After some helpful tips by Vincent St-Amour and Sam Tobin-Hochstadt in the #racket IRC channel, I came up with the following. I’ll show it two different ways, the first minimizes the line count (without sacrificing too much stylistically) to 27 lines, and the second is closer to how I’d normally format it:

#lang racket

(define (words text) (regexp-match* #rx"[a-z]+" (string-downcase text)))

(define (train features)
  (define model (make-hash))
  (for ([f features]) (hash-update! model f add1 1)) model)

(define nwords (train (words (file->string "big.txt"))))

(define (edits1 word)
  (let* ([alphabet "abcdefghijklmnopqrstuvwxyz"]
         [splits (for/list ([i (in-range (+ (string-length word) 1))])
                   (cons (substring word 0 i) (substring word i)))]
         [deletes (for/set ([(a b) (in-dict splits)] #:when (> (string-length b) 0))
                    (string-append a (substring b 1)))]
         [transposes (for/set ([(a b) (in-dict splits)] #:when (> (string-length b) 1))
                       (string-append a (substring b 1 2) (substring b 0 1) (substring b 2)))]
         [replaces (for/set ([(a b) (in-dict splits)] #:when (> (string-length b) 0) [c alphabet])
                     (string-append a (string c) (substring b 1)))]
         [inserts (for*/set ([(a b) (in-dict splits)] [c alphabet])
                    (string-append a (string c) b))])
    (set-union deletes transposes replaces inserts)))

(define (known-edits2 word)
  (for*/set ([e1 (edits1 word)] [e2 (edits1 e1)] #:when (hash-has-key? nwords e2)) e2))

(define (known words) (for/set ([w words] #:when (hash-has-key? nwords w)) w))

(define (nes set) (if (set-empty? set) #f set))

(define (correct word)
  (let ([candidates (or (nes (known (list word))) (nes (known (edits1 word)))
                        (nes (known-edits2 word)) (set word))])
    (argmax (λ (x) (hash-ref nwords x 1)) (set->list candidates))))

 

And here is a more aesthetically pleasing format:

 

#lang racket

(define (words text)
  (regexp-match* #rx"[a-z]+" (string-downcase text)))

(define (train features)
  (define model (make-hash))
  (for ([f features])
    (hash-update! model f add1 1))
  model)

(define nwords
  (train (words (file->string "big.txt"))))

(define (edits1 word)
  (let* ([alphabet "abcdefghijklmnopqrstuvwxyz"]
         [splits (for/list ([i (in-range (+ (string-length word) 1))])
                   (cons (substring word 0 i) (substring word i)))]
         [deletes (for/set ([(a b) (in-dict splits)]
                            #:when (> (string-length b) 0))
                    (string-append a (substring b 1)))]
         [transposes (for/set ([(a b) (in-dict splits)]
                               #:when (> (string-length b) 1))
                       (string-append a
                                      (substring b 1 2)
                                      (substring b 0 1)
                                      (substring b 2)))]
         [replaces (for/set ([(a b) (in-dict splits)]
                             #:when (> (string-length b) 0)
                             [c alphabet])
                     (string-append a (string c) (substring b 1)))]
         [inserts (for*/set ([(a b) (in-dict splits)]
                             [c alphabet])
                    (string-append a (string c) b))])
    (set-union deletes transposes replaces inserts)))

(define (known-edits2 word)
  (for*/set ([e1 (edits1 word)]
             [e2 (edits1 e1)]
             #:when (hash-has-key? nwords e2))
    e2))

(define (known words)
  (for/set ([w words] #:when (hash-has-key? nwords w))
    w))

(define (nes set)
  (if (set-empty? set)
      #f
      set))

(define (correct word)
  (let ([candidates (or (nes (known (list word)))
                        (nes (known (edits1 word)))
                        (nes (known-edits2 word))
                        (set word))])
    (argmax (λ (x) (hash-ref nwords x 1)) (set->list candidates))))

Written by Brian Adkins

October 16, 2015 at 12:36 am

Posted in programming

Tagged with

DSL Embedding in Racket

leave a comment »

Here’s a great, two part, video by Matthew Flatt about embedding DSLs in Racket. Being able to hack the language is one of Racket’s/Lisp’s killer features:

Written by Brian Adkins

October 15, 2015 at 6:39 pm

Posted in programming

Tagged with ,

Programming Language Popularity – Part Nine

leave a comment »

I made a number of Google searches of the forms below and summed the results:

"implemented in <language>"
"written in <language>"
"developed in <language>"
"programmed in <language>"

See Part Eight for prior results 17 months ago. I finally got around to writing a program (in Racket) to automate the collection of the search results, so it was much easier!

I’ve divided the table into sections based on percentage increases of more than 50% from one language to the next. To compute the rank delta, I excluded the newly added languages.

|------+-----------------+------------+-------+-------|
| Rank | Language        | # Search   | Prev. |  Rank |
|      |                 | Results    |  Rank | Delta |
|------+-----------------+------------+-------+-------|
|    1 | C               | 54,696,000 |     2 |     1 |
|------+-----------------+------------+-------+-------|
|    2 | R               | 22,784,600 |    12 |    10 |
|------+-----------------+------------+-------+-------|
|    3 | Java            |  1,490,000 |     6 |     3 |
|    4 | C++             |  1,256,600 |     3 |    -1 |
|    5 | PHP             |  1,082,000 |     1 |    -4 |
|    6 | C#              |  1,065,900 |     5 |    -1 |
|    7 | Python          |  1,016,500 |     4 |    -3 |
|    8 | Lisp Family (1) |    951,300 |    11 |     3 |
|    9 | JavaScript      |    764,100 |     8 |    -1 |
|   10 | FORTRAN         |    565,000 |     9 |    -1 |
|   11 | Perl            |    541,700 |     7 |    -4 |
|   12 | Ruby            |    509,900 |    10 |    -2 |
|   13 | ML Family (2)   |    445,716 |    13 |       |
|   14 | Scheme          |    381,200 |    19 |     5 |
|   15 | Go              |    378,200 |    16 |     1 |
|   16 | (S)ML (3)       |    376,116 |    21 |     5 |
|   17 | Lisp            |    357,900 |    15 |    -2 |
|------+-----------------+------------+-------+-------|
|   18 | Scala           |    236,770 |    23 |     5 |
|   19 | Haskell         |    220,580 |    17 |    -2 |
|   20 | Prolog          |    152,450 |    20 |       |
|   21 | Lua             |    133,360 |    22 |     1 |
|   22 | COBOL           |    113,700 |    14 |    -8 |
|   23 | Erlang          |    101,840 |    18 |    -5 |
|   24 | Common Lisp     |    100,580 |    25 |     1 |
|   25 | Rust            |     90,860 |   N/A |   N/A |
|   26 | Clojure         |     85,930 |    27 |     2 |
|   27 | Smalltalk       |     75,000 |    24 |    -2 |
|   28 | OCaml           |     69,600 |    26 |    -1 |
|   29 | Coffeescript    |     60,420 |    29 |     1 |
|   30 | Julia           |     51,315 |   N/A |   N/A |
|   31 | Forth           |     50,920 |    28 |    -1 |
|------+-----------------+------------+-------+-------|
|   32 | Racket          |     25,660 |    30 |       |
|------+-----------------+------------+-------+-------|
|   33 | Elixir          |      8,112 |   N/A |   N/A |
|------+-----------------+------------+-------+-------|

Update Oct. 6: I added Bing search results, and they’re significantly different. For a while Elixir was #2 because one of the four phrases resulted in no results, so Bing automatically switched to an unquoted search which skewed the results. I caught that and looked over the raw data and didn’t see anything similar for other languages. It’s interesting that C# came out #1 on Bing :)

|------+-----------------+----------|
| Rank | Language        | # Search |
|      |                 |  Results |
|------+-----------------+----------|
|    1 | C#              | 32100000 |
|    2 | C++             | 31900000 |
|------+-----------------+----------|
|    3 | C               |  4845000 |
|    4 | Java            |  3319000 |
|------+-----------------+----------|
|    5 | R               |  1219310 |
|    6 | Python          |  1123700 |
|    7 | Php             |  1093800 |
|    8 | Javascript      |   945200 |
|------+-----------------+----------|
|    9 | Lisp Family (1) |   525530 |
|   10 | Perl            |   460360 |
|   11 | Common Lisp     |   398039 |
|   12 | Ruby            |   301740 |
|   13 | Go              |   270593 |
|   14 | FORTRAN         |   245500 |
|   15 | Lua             |   214200 |
|   16 | Scala           |   188570 |
|   17 | Haskell         |   188520 |
|   18 | Lisp            |   160930 |
|   19 | COBOL           |   140910 |
|   20 | Scheme          |    98012 |
|   21 | ML Family (2)   |    97425 |
|   22 | Prolog          |    75740 |
|   23 | Erlang          |    63270 |
|   24 | (S)ML (3)       |    56375 |
|   25 | OCaml           |    41050 |
|   26 | Smalltalk       |    27960 |
|   27 | Julia           |    25319 |
|   28 | Clojure         |    23091 |
|   29 | Coffeescript    |    20322 |
|   30 | Forth           |    17529 |
|   31 | Rust            |    17086 |
|------+-----------------+----------|
|   32 | Racket          |     6388 |
|------+-----------------+----------|
|   33 | Elixir          |     2542 |
|------+-----------------+----------|

Here is the delta difference between Google and Bing:

|--------+------+-------+-----------------|
| Google | Bing | Delta | Language        |
|   Rank | Rank | G - B |                 |
|--------+------+-------+-----------------|
|      1 |    3 |    -2 | C               |
|      2 |    5 |    -3 | R               |
|      3 |    4 |    -1 | Java            |
|      4 |    2 |     2 | C++             |
|      5 |    7 |    -2 | Php             |
|      6 |    1 |     5 | C#              |
|      7 |    6 |     1 | Python          |
|      8 |    9 |    -1 | Lisp Family (1) |
|      9 |    8 |     1 | Javascript      |
|     10 |   14 |    -4 | FORTRAN         |
|     11 |   10 |     1 | Perl            |
|     12 |   12 |       | Ruby            |
|     13 |   21 |    -8 | ML Family (2)   |
|     14 |   20 |    -6 | Scheme          |
|     15 |   13 |     2 | Go              |
|     16 |   24 |    -8 | (S)ML (3)       |
|     17 |   18 |    -1 | Lisp            |
|     18 |   16 |     2 | Scala           |
|     19 |   17 |     2 | Haskell         |
|     20 |   22 |    -2 | Prolog          |
|     21 |   15 |     6 | Lua             |
|     22 |   19 |     3 | COBOL           |
|     23 |   23 |       | Erlang          |
|     24 |   11 |    13 | Common Lisp     |
|     25 |   31 |    -6 | Rust            |
|     26 |   28 |    -2 | Clojure         |
|     27 |   26 |     1 | Smalltalk       |
|     28 |   25 |     3 | OCaml           |
|     29 |   29 |       | Coffeescript    |
|     30 |   27 |     3 | Julia           |
|     31 |   30 |     1 | Forth           |
|     32 |   32 |       | Racket          |
|     33 |   33 |       | Elixir          |
|--------+------+-------+-----------------|

(1) combines Lisp, Common Lisp, Scheme, Clojure & Racket
(2) combines (S)ML & OCaml
(3) summed separate searches for standard ml, sml & ml

Written by Brian Adkins

October 6, 2015 at 3:53 am

The Three R’s

with one comment

I’ve read that ten years is a common “lifetime” for a technology. Whether that’s true, or not, it seems to be the case with me. After beginning with BASIC & S/360 Assembler, I programmed in C/C++ from ~ 1985 until 1995, then in Java until 2006, and since then I’ve been programming primarily in Ruby/Rails.

For a number of years, I’ve felt the need to consider what my next primary language should be, and after experimenting with various lisps, Haskell and OCaml, I found that Racket is the best fit for me. I’m still very much in learning mode now, but I hope to be using Racket professionally in 2016.

Despite the fact that Racket is significantly faster than Ruby, I do occasionally have compute-intensive tasks that require more raw speed, so for a high performance language to maximize all the cores at my disposal, I’ve recently settled on Rust. I expect I will mostly be developing in Racket with the occasional performance hungry lib/program in Rust.

A current interest of mine is statistical programming, big data, data science, etc. Both R and Python are on the short list here with Julia trailing behind. I really like what I’ve seen in Julia, and I wouldn’t at all be surprised if I end up there; however, as a newbie learning the field, I think I’ll find much better support (educational info, existing libraries, community support, etc.) with R, so I’m starting with that until I’m convinced of switching.

At my stage, the language isn’t as important as becoming more educated in probability & statistics, machine learning, etc.

I didn’t plan it this way, but it appears that I’m going to be spending a lot of time with Racket, Rust and R.

Written by Brian Adkins

September 24, 2015 at 10:00 pm

Programming Language Popularity – Part Eight

with 2 comments

I compiled some programming language popularity statistics in April 2009October 2009October 2010September 2011August 2012, Februrary 2013 and September 2013. Here’s an update for May 2014:

I made a number of Google searches of the forms below and summed the results:

"implemented in <language>"
"written in <language>"
"developed in <language>"
"programmed in <language>"

I’ve divided the table into sections based on large percentage drops from one language to the next.

|------+-----------------+------------+-------+-------|
| Rank | Language        | # Search   | Prev. |  Rank |
|      |                 | Results    |  Rank | Delta |
|------+-----------------+------------+-------+-------|
|    1 | PHP             | 41,418,000 |     1 |       |
|------+-----------------+------------+-------+-------|
|    2 | C               | 22,892,000 |     2 |       |
|    3 | C++             | 15,498,000 |     4 |     1 |
|    4 | Python          | 13,313,000 |     3 |    -1 |
|    5 | C#              | 12,279,000 |     5 |       |
|------+-----------------+------------+-------+-------|
|    6 | Java            |  6,997,000 |     6 |       |
|    7 | Perl            |  5,857,000 |     7 |       |
|    8 | JavaScript      |  4,917,000 |     8 |       |
|------+-----------------+------------+-------+-------|
|    9 | FORTRAN         |  3,124,000 |    10 |     1 |
|   10 | Ruby            |  3,066,000 |     9 |    -1 |
|   11 | Lisp Family (1) |  2,106,580 |    11 |       |
|------+-----------------+------------+-------+-------|
|   12 | R               |  1,267,000 |    16 |     4 |
|   13 | ML Family (2)   |    874,760 |    12 |    -1 |
|   14 | COBOL           |    731,600 |    17 |     3 |
|   15 | Lisp            |    711,300 |    18 |     3 |
|   16 | Go              |    650,970 |    23 |     7 |
|   17 | Haskell         |    629,700 |    20 |     3 |
|   18 | Erlang          |    594,900 |    19 |     1 |
|   19 | Scheme          |    580,800 |    24 |     5 |
|   20 | Prolog          |    542,200 |    14 |    -6 |
|   21 | (S)ML (3)       |    505,160 |    21 |       |
|   22 | Lua             |    490,700 |    13 |    -9 |
|   23 | Scala           |    472,300 |    25 |     2 |
|   24 | Smalltalk       |    407,400 |    15 |    -9 |
|   25 | Common Lisp     |    396,380 |    22 |    -3 |
|   26 | OCaml           |    369,600 |    26 |       |
|   27 | Clojure         |    302,220 |    27 |       |
|------+-----------------+------------+-------+-------|
|   28 | Forth           |    199,730 |    28 |       |
|   29 | CoffeeScript    |    192,366 |    30 |     1 |
|------+-----------------+------------+-------+-------|
|   30 | Racket          |    115,880 |    29 |    -1 |
|------+-----------------+------------+-------+-------|

(1) combines Lisp, Common Lisp, Scheme, Clojure & Racket
(2) combines (S)ML & OCaml
(3) summed separate searches for standard ml, sml & ml

Written by Brian Adkins

May 20, 2014 at 8:31 pm

My Top 10 Programming Languages of Interest for 2013-2014

with 6 comments

The End of Moore’s Law

For the last few years (since 2009), I’ve been pitching the idea to
my peers that language speed & concurrency/parallel capabilities
will become more important as CPU clock speeds plateau and
manufacturers add more CPU cores instead of advancing clock
rates. My 2+ year old Macbook Pro has 4 cores and
8 hyperthreads.

I, and many of my peers, program primarily in Ruby which is one of
the slowest languages, but is also one of the most productive. Even
though Ruby is often on the critical path (as opposed to the
database, network, ec.), with caching provided by Rails, more server
resources, client side processing with JavaScript, etc., the
productivity of Ruby has thus far outweighed the slowness
disadvantage (enter mantra “hardware is cheaper than developers”),
so I haven’t received much (any?) buy in from the programmers I hang
with.

Programmer productivity will continue to be important, but the
question that I’ve been considering has been, “What if a language
provides the same productivity as Ruby, but much better resource
efficiency?”

The minimum bar for efficiency is fuzzy & variable, but I’ve
nonetheless discounted any language below that bar. Except for R
which would be a special purpose language for me, I’m not
considering any language below the Racket/Clojure level of
efficiency for the future, and ideally the language has a good
concurrency/parallelism story.

Clojure

“A Lisp for Functional Programming symbiotic with an established
Platform designed for Concurrency.”

Reasons for interest:

  • A lisp
  • Macros
  • Functional programming
  • Concurrency
  • Efficiency
  • Concision
  • Clojurescript
  • Community

Common Lisp

“Common Lisp is a general-purpose, multi-paradigm programming
language. It supports a combination of procedural, functional, and
object-oriented programming paradigms. As a dynamic programming
language, it facilitates evolutionary and incremental software
development, with iterative compilation into efficient run-time
programs.”

Reasons for interest:

  • A lisp
  • Macros
  • Multi-paradigm
  • Efficiency
  • Huge standard library

Go

“Go is an open source programming environment that makes it easy to
build simple, reliable, and efficient software.”

Reasons for interest:

  • C replacement
  • Efficiency
  • Concurrency
  • Systems programming
  • Concision

Haskell

“Haskell is an advanced purely-functional programming language. An
open-source product of more than twenty years of cutting-edge
research, it allows rapid development of robust, concise, correct
software. With strong support for integration with other languages,
built-in concurrency and parallelism, debuggers, profilers, rich
libraries and an active community, Haskell makes it easier to
produce flexible, maintainable, high-quality software.”

Reasons for interest:

  • Functional programming
  • Concision
  • Purity
  • Advanced type system
  • Parallelism & concurrency
  • Efficiency
  • Monads
  • Community

J

“J is a modern, high-level, general-purpose, high-performance
programming language. J is particularly strong in the mathematical,
statistical, and logical analysis of data.”

Reasons for interest:

  • APL-like power with a normal keyboard
  • Array language
  • Concision
  • Big data

JavaScript

The language of the web.

Reasons for interest:

  • Only game in town for browsers
  • Compilation target for browsers

Prolog

“Prolog is a high-level programming language based on formal
logic. Unlike traditional programming languages that are based on
performing sequences of commands, Prolog is based on defining and
then solving logical formulas. Prolog is sometimes called a
declarative language or a rule-based language because its programs
consist of a list of facts and rules. Prolog is used widely for
artificial intelligence applications, particularly expert systems.”

Reasons for interest:

  • Logic language
  • A new paradigm
  • AI

R

“R is a language and environment for statistical computing and
graphics. It is a GNU project which is similar to the S language and
environment which was developed at Bell Laboratories (formerly AT&T,
now Lucent Technologies) by John Chambers and colleagues. R can be
considered as a different implementation of S. There are some
important differences, but much code written for S runs unaltered
under R.”

Reasons for interest:

  • Statistics
  • Big data
  • Extensive statistical/numerical library

Racket

“Racket (formerly named PLT Scheme) is a general purpose,
multi-paradigm programming language in the Lisp/Scheme family. One
of its design goals is to serve as a platform for language creation,
design, and implementation. The language is used in a variety of
contexts such as scripting, general-purpose programming, computer
science education, and research.”

Reasons for interest:

  • A lisp
  • Functional programming
  • Continuations
  • Hygienic macros
  • Concurrency
  • Efficiency
  • Built-in web server
  • Concision
  • Can be small & clean
  • pg used it to implement Arc

Ruby

“Ruby is a language of careful balance. Its creator, Yukihiro “Matz”
Matsumoto, blended parts of his favorite languages (Perl, Smalltalk,
Eiffel, Ada, and Lisp) to form a new language that balanced
functional programming with imperative programming.”

Reasons for interest:

  • Joy to program
  • Concision
  • Rails
  • Community
  • Primary revenue generating language currently
  • Dead slow, but lovable

Summary

I hope to decide on which of the three lisps I want to pursue,
determine if J can supplant R, and see if Go can be replaced with
one of the others, so that would leave six.

  • Haskell
  • J
  • JavaScript
  • A lisp
  • Prolog
  • Ruby

I’ve briefly researched Erlang, Factor, Lua, OCaml, Scala, Smalltalk
& Standard ML. At the moment, I don’t feel they should replace any
of the languages on the above list for subjective, practical and/or
technical reasons, but if you’re excited about those languages, or
others, comments are welcome.

Standard ML came close to bumping Haskell from the list, but despite
the nicety of SML and the advocacy of Robert Harper, I felt the
platforms (compilers, runtimes, libraries, etc.) were antiquated and
dividing/conquering themselves, so I couldn’t trust them to advance
quickly enough – particularly in the area of parallelism &
concurrency.

Update 1/17/2014:
For a lower level language, I think the D language is a candidate to replace Go. For numerical & statistical computing, I think Julia is a candidate to replace R. Lastly, I think other Scheme languages that are candidates to replace Racket include Gambit, Bigloo and Chicken.

Update 5/27/2014:
My final list of languages to spend time in, in order of priority is:

  1. Haskell, Racket & Common Lisp (concurrently)
  2. Julia
  3. J
  4. Prolog

Notably absent are Clojure (replaced by Racket and Common Lisp), Go (other languages on the list are efficient enough), R (replaced by Julia), JavaScript & Ruby (I currently use them in my work).

Written by Brian Adkins

September 27, 2013 at 2:21 am

Follow

Get every new post delivered to your Inbox.