Wednesday, 16 March 2016

Programming Languages and programming

There are probably five main styles of programming, which may overlap, and often relate to particular languages. We have imperative, declarative, object oriented, functional and logic programming styles. These are more commonly called programming paradigms than styles. The list below has each style with an iconic sibling language to implement it.

  • imperative - C, or BASIC
  • declarative - SQL
  • object oriented - Smalltalk
  • functional - Haskell
  • logic - Prolog

Imperative programming begins with the 3 rules of coding: sequence (code executes left to right, top to bottom), selection (if, case) and iteration (loops), and may develop to structured programming. There's something very low level and intuitive about imperative programming but the best programming is rarely done in this style. With imperative programming, our code explains how a problem is solved - the solution can often be understood by tracing through the steps in the code. Imperative code often has a closer relationship to machine code than other styles.

With declarative programming, one declares a statement or code block to solve a problem. Declarative features are generally higher level. We see declarative features in SQL, many functional and logic programming languages.

Object oriented (OO) programming began in the late 1960s, cumulating in Smalltalk (Alan Kay: 1970 to 1980) and design patterns (1990s through to the noughties). 1980s onward saw several post-Smalltalk languages implementing OO features: C++, Java, C#, Javascript. These four now dominate the world of code. Yet your OO-purest is a Smalltalk coder at heart!

Functional programming (FP) began in academia. Elements of LISP (Designed by John McCarthy, 1958) are functional, Robin Milner created ML in 1975. 1980s saw a commercial FP language: Miranda (David Turner). Haskell began in 1987, mostly as a copy of Miranda, ever refining itself, yet staying lean and clean. Modern functional programming stresses, functions as "first class" elements of the language, declarative programming, prefers immutable data. Other commercial FP languages are: OCaml, F#, Scala, Clojure, ...

Logic programming languages began in earnest in the early 1970s with Prolog. Coding styles are very declarative and applications often have an AI basis, data analysis or both.

All these styles mix, to some extent, in actual languages in use. Yet a language is classified with the style which is most important to it. For example we call Scala a functional programing language, even though it is very OO too. Javascript has always had functional features but does not have immutable data. There's even a style called functional Javascript. Yet Javascript will always be an OO language first.

I began this in response to a question : what programming language should one learn first? It's not really a sensible question. You should learn each of the programming language styles, and sub-styles. [e.g. sub-styles: structured programming as a sub-style of imperative programming, design patterns as a sub-style of object-oriented programming, They are not really sub-, but more like meta-, or supra-styles]. We can also recognize certain idioms that span all styles: DRY, SOLID, ... A lot of good coding practices seem orthogonal to implementing specific use cases. You could begin with imperative programming (first six books in list below), and apply the wisdom in the books to your code. Object-oriented languages dominate business, so read books 7 to 12, but only 12 is specific only to OO (in a sense). Finally, you need at least some functional programming and the last two are good starters.

Beginner programmer reading list.

TBH, A beginner can begin with any programming, or programming language book. Yet you won't really have graduated from beginner until you've read most of the 14 books below. Read and apply the lessons of all 14 and you're past beginner/junior programmer level.

  1. Code Complete: A Practical Handbook of Software Construction, 2nd Edition, by Steve McConnell
  2. The Art of Readable Code, by Dustin Boswell and Trevor Foucher
  3. Programming Pearls (2nd Edition), by by Jon Bentley
  4. 97 Things Every Programmer Should Know, by by Kevlin Henney
  5. Clean Code: A Handbook of Agile Software Craftsmanship, by Robert C. Martin
  6. Test Driven Development: By Example, by Kent Beck
  7. Agile Principles, Patterns, and Practices in C#, by Robert C. Martin and Micah Martin
  8. Refactoring: Improving the Design of Existing Code, by by Martin Fowler
  9. Working Effectively with Legacy Code, by by Michael Feathers
  10. Growing Object-Oriented Software, Guided by Tests, by Steve Freeman and Nat Pryce
  11. Agile Java: Crafting Code with Test-Driven Development, by Jeff Langr
  12. Object Design: Roles, Responsibilities, and Collaborations, by Rebecca Wirfs-Brock and Alan McKean
  13. Programming in Haskell, by Graham Hutton
  14. The Joy of Clojure, 2nd Edition, by Michael Fogus and Chris Houser

Tuesday, 24 November 2015

The crossword that counts itself

I love puzzles. This is described by Alex Bellos at the Guardian.

It helps to write a spreadsheet to solve this.

  1. Set the column widths to be about the same as the height, making them look square.
  2. Draw out the puzzle by highlighting a phrase (there are 12 "phrases"), and formatting: format cells, borders, click outline, OK.
  3. Repeat for each of the other 11 phrases, highlight, Ctrl-Y (repeats the last format command for a new area)
  4. Count the lengths of the rectangles (these are "phrase length"), subtract (3 or 2) to get "word length"
  5. The area inside the blue box is named "puzzle"
  6. The formula (row 20) for "actual" is: = SUMPRODUCT( LEN(puzzle) - LEN( SUBSTITUTE(puzzle,"e","") ))
  7. This formula is copied for the 11 rows below and the letter edited, each time.
  8. The formula for "= target" is: =IF( E20 = F20, "Y", "N")
  9. When they all show "Y", the puzzle is solved.
  10. The sum in cell e32 just tallies the 12 "actual" numbers. It must eventually show 68 when the puzzle is solved
  11. The working between p18 : ae32 allows one to count the number of times each number word is used and tally them (must be = 68 too)
  12. The cells bordered in red are named: number
  13. The "word cells" formula is just: = number * R19
  14. This formula is copied down and across (to the 3 combos). The "3 combos" show the 3 possible combinations of possible words.
  15. The "# letters" fomula is just the length of the number. e.g.: = LEN( AD20 )

This is not so easy to solve using purely coded program, because the rules to narrow down the combinations are not so clear. After you've figured out what constraints must be met it, no doubt, becomes much easier to program.

Tuesday, 10 November 2015

Code spippets - Fsharp

GistBox Clipper

Clipper is the companion Chrome extension to GistBox. It allows users to create Gists from:

  • Example code blocks
  • Highlighted text
  • Any page on the web
Install Chrome Extension
Enter keyboard data, via console, with validation.
/// Read a valid age (between 0 and 150) from key input let readage (person: string) = let mutable continueLooping = true let mutable age = 0 while continueLooping do Console.WriteLine("Enter {0}'s age (between 0 and 150): ", person) let canparse, keyin = Int32.TryParse(Console.ReadLine()) age <- keyin if canparse && age >= 0 && age < 151 then continueLooping <- false age

Code snippets - css

Lists, without indents, with buttons, and over-handing firstline
ul { list-style: disc outside none; margin-left: 0; padding-left: 1em; } { list-style-type: circle; } /* for lists within lists */ li { padding-left: 0.5em; }
pre :- with CR wrapping a line
pre { white-space: pre-wrap; /* css-3 */ white-space: -moz-pre-wrap; /* Mozilla, since 1999 */ white-space: -pre-wrap; /* Opera 4-6 */ white-space: -o-pre-wrap; /* Opera 7 */ word-wrap: break-word; /* Internet Explorer 5.5+ */ }

Thursday, 25 June 2015

Snippet: Searching web pages.

The idea's from Professional F# 2.0, Chapter 17 (by Ted Neward, Aaron C. Erickson, Talbott Crowell, Richard Minerich). Because there are 4 of them I'll call them the fgang of four, or FoF. When searching a dropdown list via a form, a web page form may return a value or nothing at all. F# handles nothing at all with None. Suppose our database data contained:

type VacationLocation =
    { Name: string; Pop: int; Density: int; Nightlife: int }
let destinations =
    [ { Name = "New York"; Pop = 9000000; Density = 27000; Nightlife = 9 }
      { Name = "Munich"; Pop = 1300000; Density = 4300; Nightlife = 7 }
      { Name = "Tokyo"; Pop = 13000000; Density = 15000; Nightlife = 3 }
      { Name = "Rome"; Pop = 2700000; Density = 5500; Nightlife = 5 } ]

Our web page filters user selections by city name, population, population density, and night life. A user will often ignore a selection. So each selection will be an option. A first iteration of F# can handle it likewise, using the identity function id:

let getVacationPipeline nightlifeMin sizeMin densityMax searchName =
    match nightlifeMin with
    | Some(n) -> List.filter (fun x -> x.Nightlife >= n)
    | None -> id
    >> match sizeMin with
       | Some(s) -> List.filter (fun x -> x.Pop / x.Density >= s)
       | None -> id
    >> match densityMax with
       | Some(d) -> List.filter (fun x -> x.Density <= d)
       | None -> id
    >> match searchName with
       | Some(sn) -> List.filter (fun x -> x.Name.Contains(sn))
       | None -> id

This function is applied to the data like so:

let applyVacationPipeline data filterPipeline =
    |> filterPipeline
    |> (fun x -> x.Name)

with filterPipeline a general function to be applied for the where clause. The last line is the select clause.

let myPipeline = getVacationPipeline (Some 5) (Some 200) (Some 8000) None
applyVacationPipeline destinations myPipeline

This match ... with ... some ... None -> id is repetitive, so we abstract it out as a getFilter function. It becomes:

let getVacationPipeline2 nightlifeMin sizeMin densityMax searchName =
    let getFilter filter some =
        match some with
        | Some(v) -> List.filter (filter v)
        | None -> id
    getFilter (fun nlMax x -> x.Nightlife >= nlMax) nightlifeMin
    >> getFilter (fun sMax x -> x.Pop / x.Density >= sMax) sizeMin
    >> getFilter (fun dMin x -> x.Density < dMin) densityMax
    >> getFilter (fun sName x -> x.Name.Contains(sName)) searchName

We can invoke it like so:

let myPipeline2 = getVacationPipeline2 (Some 5) (Some 200) (Some 8000) None
applyVacationPipeline destinations myPipeline2

The FoF is more detailed, beginning with an imperative version (??). I suspect every C# programmer gave up such imperative code long ago for Linq.

F# automatically handles missing data. Because we can compose our queries, each may be simply tested too.

Monday, 5 January 2015

Russian propaganda (cyber-warfare?) example on twitter

Russia has been accused of funding and propagandising anti-fracking campaigns in the West1, 2. If the West can do it to Russia3, expect Russia to fight back. Natural gas is a famous Russian export but many energy exports (gas, oil, coal, nuclear power) are important to the Russian economy. The recent fall in the price of oil even precipitated an economic crisis there. Preventing fracking in Europe is a good way to keep natural gas prices high. They've already had a lot of success in that area.

Given they have the 'resources' in place, it seems obvious to turn against nuclear power too. One such example is their recent twitter campaign, using the hashtags #FukushimaAgain #Chernobyl2015. It's already been noted that the 'accident' to the reactor in the Ukraine was an incident not an accident4. Russian media sources are systematically misreporting the events in Ukraine, and at Ukraine nuclear reactors too5. As I write, I note that ans nuclear cafe is down. Can that be due to Russian cyber-warfare too? Unlikely, it's back up and it was giving a 502 server error. Nevertheless, who'd put anything past these people?

These are just the first 10 accounts I looked at tweeting those hashtags. I've no idea how many more there are of them.

These accounts don't, at first, look fake. A detailed investigation shows otherwise: they are hijacked or fake:

  • most of the accounts only began tweeting within the last few weeks
  • the accounts have a few hundred tweets each, often made very recently
  • they rarely, if ever, tweet to anyone else
  • they don't engage anyone in a conversation
  • many of the innocent looking tweets are garbage:
    • common quotes from famous people e.g.:
    • 'witty', or 'pseudo-intellectual' observations. e.g.:
  • here's an example of a tweet that belongs to a conversation. A message to a friend. Who might that be?:
  • tweets are often made with very short time intervals between them - indicating the person has nothing better to do than, essentially, repeat the same tweet. More like it's a bot using random time intervals.
  • tweets are likely to be gender neutral, geographically neutral. The bot doesn't know whether it's pretending to be male/female or whatever. The more specific the tweet, the more trouble a programmer must endure to fake it.

For more on fake twitter accounts see: 6


  1. NATO Chief: Putin Behind Anti-Fracking Campaigns
  2. Russia’s Quiet War Against European Fracking
  3. Russia says 'spies' work in foreign NGOs
  4. Reporting an Incident as an Accident
  5. Ukraine Nuclear Plants - Resources and Information
  6. 11 Easy Ways To Spot a Fake Twitter Account

Thursday, 26 June 2014

Today I discovered Elm

Web pages are broke. They always were, from the inception of html, to all the fixes: css, JavaScript, DHTML, Ajax. Now we have an explosion of fixes: sass, lass, Jade, JRX, an explosion of MVM and, even MVVM frameworks. An army of languages to write JavaScript with. Are we doomed to ever more complexity; cludge piled upon fix glued together by compromise?

Is there a light at the end of the tunnel? Today I discovered Elm. Video presentations:

Some links

Academic (papers)

And a Haskell page to remember.

F# computation expressions