Thursday 13 February 2020

Installing LaTeX

I wanted to use LaTeX today and since I'm using a new laptop and I don't have it installed. So here are my notes from how I went about installing LaTeX on Ubuntu 18.04 LTS.

My first stop was the LaTeX page on the Ubuntu Community Help Wiki. In summary the best way to get LaTex is as part of the TeX Live distribution, the alternative being teTeX which is now unsupported. The choice then is between whether to install TeX Live from Ubuntu package repositories or from the TeX Users Group. Install from repositories is simpler but the main reason is that repository packages can lag behind the latest version by quite some degree. This point I have found to be true for many software packages and increasingly I have found installing software manually to be preferable for this reason. It's worth noting that page was last updated in October 2013 so things might have changed since then.

I decided to check what the current latest version of Tex Live is and compare that from what is available from the Ubuntu repositories.

At the time of writing the latest TeX Live 2019 which was released in April 2019 [1]. From the Ubuntu repositories on the other hand it looks like the latest version is TeX Live 2017 [2]. Since the repository version is 2 years behind it is clear that installing manually will be the preferable option.

$ wget http://mirror.ctan.org/systems/texlive/tlnet/install-tl-unx.tar.gz
$ tar -xzf install-tl-unx.tar.gz
$ cd install-tl-20200213/
I tried first using the graphical installer,
$ ./install-tl -gui
But this looked terrible:
So I abandoned that and tried the wizard approach:
$ ./install-tl -gui wizard

That failed on the first attempt with the following message:
Cannot load Tk, maybe something is missing and
maybe https://tug.org/texlive/distro.html#perltk can help.
Error message from loading Tk:
  Can't locate Tk.pm in @INC (you may need to install the Tk module) (@INC contains: ./tlpkg /etc/perl /usr/local/lib/x86_64-linux-gnu/perl/5.26.1 /usr/local/share/perl/5.26.1 /usr/lib/x86_64-linux-gnu/perl5/5.26 /usr/share/perl5 /usr/lib/x86_64-linux-gnu/perl/5.26 /usr/share/perl/5.26 /usr/local/lib/site_perl /usr/lib/x86_64-linux-gnu/perl-base) at ./install-tl line 527.
But that just seemed to be a case of missing the perl-tk[3] package. So I installed that first:

$ sudo apt install perl-tk

Then tried again running the gui install script with the wizard option - et voila!



Since I am running the install without super user privileges the default installation path of /usr/local/texlive/2019 was not writable so I changed this to be relative to my home directory i.e. /home/daniel/opt/texlive/2019 . And since I live in the UK I set the default paper size to A4. I clicked install and off it went downloading things:


As advised I added made the following updates to the MANPATH; INFOPATH and PATH environment variables by adding these lines to my ~/.bashrc file:

# Path modifications for TeX Live
export MANPATH=$MANPATH:/home/daniel/opt/texlive/2019/texmf-dist/doc/man
export INFOPATH=$INFOPATH:/home/daniel/opt/texlive/2019/texmf-dist/doc/info
export PATH=$PATH:/home/daniel/opt/texlive/2019/bin/x86_64-linux

Then after opening a new shell I checked that LaTeX was functioning correctly:

$ latex --version
pdfTeX 3.14159265-2.6-1.40.20 (TeX Live 2019)
kpathsea version 6.3.1
Copyright 2019 Han The Thanh (pdfTeX) et al.
There is NO warranty.  Redistribution of this software is
covered by the terms of both the pdfTeX copyright and
the Lesser GNU General Public License.
For more information about these matters, see the file
named COPYING and the pdfTeX source.
Primary author of pdfTeX: Han The Thanh (pdfTeX) et al.
Compiled with libpng 1.6.36; using libpng 1.6.36
Compiled with zlib 1.2.11; using zlib 1.2.11
Compiled with xpdf version 4.01


I also checked that the Tex Live Manager was working correctly by running:

$ tlmgr -gui &

Tuesday 11 February 2020

FP101x - 0. Introduction

As I mentioned in my previous post I'm going to be following the online course FP101x. Here are the notes that I made from watching covering the 0. Introduction section.

This section just consists of one lecture split across two videos. The lecturer Erik Meijer introduces the course and discusses the content.

Introduction - Part 1

This video covers Functional Programming in general and introduces Haskell.

He stresses the fact that the course is not directly about Haskell but more Functional Programming in general and Haskell is just the chosen language for teaching purposes. In order to produce complex software all modern programming languages have features that enable abstractions to be made and help to make software clear and concise.

Functional programming is based on concepts from the Lambda calculus and Haskell as a pure functional programming language is a good choice for learning these concepts. The concepts can be applied to how you program in any language though.

Definitions of functional programming

He talks about different ways of defining functional programming. In a situation where you are talking about a purely functional programming language such as Haskell it is possible to use a more purist definition such as:
Purist definition: Functional programming is a style of programming in which the basic method of computation is the application of functions to arguments.
More generally speaking though if you working in a language that is not a specifically functional one you can use a looser definition.
General definition: Functional programming is a style of programming in which expressions are more important than statements.
A functional languages is one that supports or encourages the functional style. Many modern programming languages support this style of programming. A key feature of a programming language that makes it more amenable to programming in a functional style is if it has support for lambda expressions.

Example of Functional programming

Functional programming is contrasted with programming in an imperative style by using the example of summing the integers 1 to 10.

In Java the code would look like this:
int total = 0;
for (int i = 1; i<10; ++i) {
    total = total+i;
}
Here the emphasis is on statements and the computation method is variable assignment. The program relies on mutable program state, in this case a variable called 'total', which is repeatedly updated.

The corresponding program written in Haskell is much simpler:
sum [1..10]
In the Haskell version there are no statements, only two expressions. The expression [1..10] defines a list of the integers from 1 to 10. The function sum takes this list as an argument.

It was mentioned that a similar effect can be achieved in Java from versions 8 inwards using the streams feature of the language.

Introduction - Part 2

The second video in the Introduction section focuses more on the history of Haskell.

1930s

  • Alonzo Church developed the lambda calculus a simple but powerful theory of functions.

1950s

  • John McCarthy developed Lisp which is the first functional language. Lisp is influenced by lambda calculus yet also includes variable assignment.

1960s

  • Peter Landin developed ISWIM which was the first pure functional programming language. ISWIM was influenced strongly by lambda calculus and has no variable assignment.

1970s

  • John Backus developed the functional programming language FP which was designed to support higher-order functions and reasoning about the correctness of the program.
  • Robin Milner along with others developed the functional programming language ML. Considered to be the first modern functional language, ML introduced type inference and parametric polymorphism. ML is a hybrid programming language that includes functional programming ideas as well as variable assignment. It was originally conceived as a scripting language.

1970s - 1980s

1987

  • An international committee of researchers started Haskell with the intention of creating a standard platform for experimenting with functional language ideas.

2003

  • The committee published the Haskell 98 report defining a stable version of the language.

2003 - present (2016)

  • The Haskell Platform provides a standard implementation of Haskell GHC as well as the standard libraries, build system and other commonly used development tools.

A Taste of Haskell

To illustrate how Haskell works the later part of the video goes through an example program.
f []     = []
f (x:xs) = f ys ++ [x] ++ f zs
           where
              ys = [a | a ← xs, a ≤ x]
              zs = [b | b ← xs, b > x]
This code is an implementation of the quicksort algorithm. The program defines two cases.
In the first case:
f []     = []
the list [] to be sorted is empty  and so, trivially, it is already sorted so that the result is just the empty list.

In the second case:
f (x:xs) = f ys ++ [x] ++ f zs
The list (x:xs) is not empty but consists of a head element x and a tail list xs (which may be empty).

So for a non-empty list (x:xs), that list is sorted by taking the head of the list x , then combining three lists in sequence:
  1. the list that results from applying the sorting function to all those elements from the tail list xs that are less than or equal to the head element: f ys , followed by,
  2. a list that contains just the head element   [x]followed by,
  3. the list that results from applying the sorting function to all those elements from the tail of the list that are greater than the head element f zs .
Which overall is written as: f ys ++ [x] ++ f zs

The where clause:
           where
              ys = [a | a ← xs, a ≤ x]
              zs = [b | b ← xs, b > x]
defines the two lists: ys, which is all the elements from xs which are less than or equal to x; and zs which is all the elements from xs which are greater than x.

Since the definition of the algorithm makes use of itself calling the function again on the sub-lists of  ys and zs the algorithm is said to be recursive in nature.

This program illustrates the algorithmic structure of Quicksort and works by creating new lists. It differs from typical imperative implementations of the Quicksort work by modifying elements of the list in place.

Resources

Starting a blog and learning Haskell again.

For a while now, I've had it in the back of my mind, that I should learn Haskell again. By 'again', I mean that, in theory at least, I've already learnt Haskell once before since it was taught in the first year of my Computer Science degree. But that was back in 2005 which feels like a dim and distant memory now. I just about recollect muddling through some coursework assignments and I still have my copy of the textbook we used - Haskell: the craft of functional programming by Simon Thompson. That course was interesting because object-oriented and functional programming styles were taught in parallel and each assignment was to be completed in both Java and Haskell. For some reason, at the time, Java didn't make any sense to me and I recall solving the exercises relatively easily in Haskell and struggling or even failing completely to achieve the same results in Java. This was odd since the programming course from the preceding semester had been taught in C, which I got on with fine, and Java is supposed to follow on quite naturally from C. Java did eventually click though. Over a summer holiday I read a copy of Java Concepts by Cay Horstmann, which I thoroughly enjoyed, after which object-oriented programming finally made sense. So much so that it eventually led to my first software development job which was as a Java programmer.

Since that course at University I haven't really touched Haskell at all. Around 2008 it seemed to me that Haskell was still perceived as quite academic and not widely used in industry. Today however there seems to be a huge interest in functional programming and lots of companies are using it to greater or lesser extent. So it feels like there is no better time to dust of the textbooks at get stuck in again. I remember with fondness how different Haskell felt to the C family of languages. I liked how clean the syntax was and I liked how it made me think differently. I also remember that I don't think I ever really felt like I fully understood Haskell as a language - I was never proficient in it - and that is something I'd like to change with this second attempt at learning it.

I thought following some kind of online course would be a good idea and there seem to be online courses for everything these days. Strangely for Haskell they are in short supply. The best I could find was a MOOC on Functional Programming. This course is archived but the content is all still available so I have chosen to follow this as best I can given it appears to be the only available option. Fortunately this course is based on a textbook, which by coincidence, I happen to have a copy of - Programming in Haskell by Graham Hutton. Along side this I also thought it would be good idea to get some experience of a web-frame work so I also plan to read Developing Web Apps with Haskell and Yesod at the same time.

It also seems to be a good idea to keeps notes on how I'm doing - hence the blog. These will probably be very boring and may only be of any interest to myself but perhaps someone might find my experience useful.

Books

Resources

Installing LaTeX

I wanted to use LaTeX today and since I'm using a new laptop and I don't have it installed. So here are my notes from how I went abo...