September 14, 2012
identica-mode is an Emacs mode to receive and submit updates to laconica microblogging site. It is now available in Fedora. Install it using:
$ sudo yum install emacs-identica-mode
To enter into identica-mode in GNU Emacs, you can use:
M-x identica-mode
Or, you can also add the following in your ~/.emacs:
(require 'identica-mode)
(setq identica-username "yourusername")
To send an update, use C-c C-s within GNU Emacs, or use:
M-x identica-update-status-interactive
To view the public timeline, you can use:
C-c C-a
To view a user’s timeline, you can use:
C-c C-u
To send a direct message to a user, you can use:
C-c C-d
You can press ‘G’ anytime to refresh the timeline in the *identica* buffer. The Identica mode manual has more examples and shortcuts to interact with identi.ca.
September 12, 2012
oeis package provides an interface to the Online Encyclopaedia of Integer Sequences - http://oeis.org. It is now available in Fedora. Install it using:
$ sudo yum install ghc-oeis-devel
To import the OEIS module, use:
ghci> :m + Math.OEIS
Few examples are shown below:
Prelude Math.OEIS> getSequenceByID "A000040"
Just [2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,
97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,
193,197,199,211,223,227,229,233,239,241,251,257,263,269,271]
Prelude Math.OEIS> description `fmap` lookupSequenceByID "A000040"
Just "The prime numbers."
Prelude Math.OEIS> lookupOEIS "2,3,5,7"
["The prime numbers.","[2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,
67,71,73,79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157,163,
167,173,179,181,191,193,197,199,211,223,227,229,233,239,241,251,257,263,
269,271]"]
Prelude Math.OEIS> extendSequence [2,3,5]
[2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,
101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,
193,197,199,211,223,227,229,233,239,241,251,257,263,269,271]
August 10, 2012
numbers package provides instances of numerical classes for different types of numbers - (computable) real numbers, precison fixed numbers, floating point numbers, differentiable numbers, symbolic numbers, and interval arithmetic. It is now available in Fedora. Install it using:
$ sudo yum install ghc-numbers-devel
For interval arithmentic, Interval is defined as a type constructor:
data Interval a = I a a
The ival function two arguments of the same type and returns an Interval a, while, the getIval function takes an interval and returns a pair.
ghci> :m + Data.Number.Interval
ghci> ival 1 2
1..2
ghci> getIval (ival 3 4)
(3,4)
The CReal type implements (constructive) real numbers. The showCReal function takes a number of decimals, a real number, and returns a string.
ghci> :m + Data.Number.CReal
ghci> showCReal 5 pi
"3.14159"
The Dif type is a type defined for differentiable numbers. The dCon function takes a number and constructs a Dif number with the same value, while the val function does the opposite.
ghci> :m + Data.Number.Dif
ghci> dCon 3
3~~
ghci> val (dCon 5)
5
The mkDif function takes a value and a Dif value and makes a Dif number as its derivative.
ghci> mkDif 4 (dCon 2)
4~~
The deriv function takes a derivative of a function. For example, if we have an equation f(x)=x2, then the first derivative, f’(x)=2x. This can be defined as:
ghci> let f x = x * x
ghci> let f' = deriv f
ghci> f 3
9
ghci> f' 3
6
August 2, 2012
readline is a Haskell binding to the GNU Readline library. It is now available in Fedora. Install it using:
$ sudo yum install ghc-readline-devel
The Readline library provides functions to for line editing, and managing history of commands entered interactively through the command-line. It is Free Software. A simple example of using ghc-readline is demonstrated:
import System.Console.Readline
readEvalPrintLoop :: IO ()
readEvalPrintLoop = do
maybeLine <- readline "% "
case maybeLine of
Nothing -> return ()
Just "exit" -> return ()
Just line -> do addHistory line
putStrLn $ "The user input: " ++ (show line)
readEvalPrintLoop
main = do
readEvalPrintLoop
Compile it using:
$ ghc --make read.hs
Linking read ...
Run it and test it:
$ ./read
%
The user input: ""
% Lorem ipsum
The user input: "Lorem ipsum"
% exit
$
July 27, 2012
unlambda package contains an interpreter written in Haskell for the Unlambda language. It is now available in Fedora. Install it using:
$ sudo yum install unlambda
Unlambda is a “nearly pure” functional programming language. There are no named functions in Unlambda. The s and k primitive functions are part of the core language, and are sufficient to make Unlambda Turing complete. The backquote(`) is used for function application. There are no variables in Unlambda. r prints a newline, and i is the identity function.
$ unlambda
`ri
$
The k combinator takes two arguments (by currying), say X and Y, and is expressed as ``kXY, which evaluates to X (Y is also evaluated). The s combinator takes three arguments and is applied as ``FXYZ, which evaluates to ``XZ`YZ. The .x functions prints the character x to the output. So, the r function is an instance of .x function where x represents the newline character.
$ unlambda
```k.Hii
H $
The classic hello world program:
$ unlambda
`r```````````.H.e.l.l.o. .w.o.r.l.di
Hello world
The following hello.unl program prints “Hello world!” followed by asterisk symbol, in incremental fashion.
```s``sii`ki
``s``s`ks
``s``s`ks``s`k`s`kr
``s`k`si``s`k`s`k
`d````````````.H.e.l.l.o.,. .w.o.r.l.d.!
k
k
`k``s``s`ksk`k.*
$ unlambda < hello.unl
Hello, world!
Hello, world!*
Hello, world!**
Hello, world!***
Hello, world!****
Hello, world!*****
Hello, world!******
Hello, world!*******
Hello, world!********
Hello, world!*********
Hello, world!**********
...
July 20, 2012
Stream provides functions to create and manipulate infinite lists. It is defined as:
data Stream a = Cons a (Stream a)
It is now available on Fedora. Install it using:
$ sudo yum install ghc-Stream-devel
The fromList function converts an infinite list to a stream. For example:
Prelude> import qualified Data.Stream as S
Prelude Data.Stream> let numbers = S.fromList [1..]
Prelude Data.Stream> S.head numbers
1
There are numerous functions available for building, transforming, extracting, sublisting, indexing streams. Most of them are analogous to the functions in Data.List. Few examples:
The map function applies a function uniformly over all the elements of a stream.
Prelude Data.Stream> S.take 5 $ S.map (*3) numbers
[3,6,9,12,15]
The cycle function can return an infinite repetition of a given list.
Prelude Data.Stream> S.take 10 $ S.cycle [1,2,3]
[1,2,3,1,2,3,1,2,3,1]
The take function extracts a sublist of n elements from the stream.
Prelude Data.Stream> S.take 10 numbers
[1,2,3,4,5,6,7,8,9,10]
We can use the (!!) function to return an element at an index n in the stream.
~~~~ {.haskell} Prelude Data.Stream> numbers S.!! 5 6 ~~~~
July 9, 2012
logict is a “continuation-based, backtracking, logic programming monad”. It is a library that provides backtracking computations to a Haskell monad. It is now available on Fedora. Install it using:
$ sudo yum install ghc-logict-devel
The observeAll function, for example, obtains all the results from a Logic computation:
import Control.Applicative ((<|>))
import Control.Monad.Logic (observeAll)
import Control.Monad (filterM)
powerset :: [a] -> [[a]]
powerset = observeAll . filterM (const (return False <|> return True))
You can compile and run the above example using:
~~~~ {.shell} $ ghci Powerset.hs GHCi, version 7.0.4: http://www.haskell.org/ghc/ :? for help Loading package ghc-prim … linking … done. Loading package integer-gmp … linking … done. Loading package base … linking … done. [1 of 1] Compiling Main ( Powerset.hs, interpreted ) Ok, modules loaded: Main.
*Main> powerset [1,2] Loading package transformers-0.2.2.0 … linking … done. Loading package mtl-2.0.1.0 … linking … done. Loading package logict-0.5.0.1 … linking … done. [[],[2],[1],[1,2]] ~~~~
July 2, 2012
I had organized a one-day Fedora workshop at Indira College of Commerce and Science, Pune, Maharashtra, India on Saturday, June 30, 2012. The college is affiliated to the University of Pune.
The participants were post-graduate students in computer applications. They were familiar with basic programming in C, C++, Java and PHP. Their syllabus now mandates the use of *nix. Fedora was installed on all their lab machines for students to use and learn.
The forenoon session was lecture-based, where I began with an overview of the system architecture. Installation concepts were discussed, along with common newbie mistakes that are made during installation. I then demoed the Fedora desktop, and showed the various software, and window managers that they can use. The need to use a version control system was emphasized, and also introduced them to the use of git, with few examples.
Post-lunch was a lab session, where students worked on shell scripts and C programming exercises. I gave them numerous examples on how they can improve their programs by composing functions that do one thing and do it well. The students were also advised to follow coding guidelines, and learn and improve by reading code from free/open source software projects. I have also given them the latest Fedora .iso images (32-bit and 64-bit).
Thanks to Antriksh Shah for his help in organizing this workshop. Few photos taken during the event are available in my /gallery.
July 1, 2012
lazysmallcheck is a demand-driven testing library for Haskell programs. It requires fewer test cases to verify properties for inputs for a depth. It is now available on Fedora. Install it using:
$ sudo yum install ghc-lazysmallcheck-devel
The depthCheck function lists the number of fewer tests required at a given depth. For example:
import Test.LazySmallCheck
import System
type Set a = [a]
empty :: Set a
empty = []
insert :: Ord a => a -> Set a -> Set a
insert a [] = [a]
insert a (x:xs)
| a < x = a:x:xs
| a > x = x:insert a xs
| a == x = x:xs
set :: Ord a => [a] -> Set a
set = foldr insert Main.empty
ordered [] = True
ordered [x] = True
ordered (x:y:zs) = x <= y && ordered (y:zs)
allDiff [] = True
allDiff (x:xs) = x `notElem` xs && allDiff xs
isSet s = ordered s && allDiff s
-- Properties
infixr 0 -->
False --> _ = True
True --> x = x
prop_insertSet :: (Char, Set Char) -> Bool
prop_insertSet (c, s) = ordered s --> ordered (insert c s)
main = do
[d] <- getArgs
depthCheck (read d) prop_insertSet
You can compile and run it using:
~~~~ {.shell} $ ghc –make ListSet.hs [1 of 1] Compiling Main ( ListSet.hs, ListSet.o ) Linking ListSet …
$ ./ListSet 2 OK, required 23 tests at depth 2 ~~~~
June 29, 2012
smallcheck is a testing library to verify properties for test cases to a certain depth. The depth for data values refers to the depth of nested constructors, while for functional values, it refers to the depth of nested case analysis and results. The test cases will be generated by smallcheck. It is now available on Fedora. Install it using:
$ sudo yum install ghc-smallcheck-devel
The following BitAdd.hs example demonstrates the use of smallcheck:
import Test.SmallCheck
and2 (a,b) = a && b
xor2 (a,b) = a /= b
halfAdd (a,b) = (sum,carry)
where sum = xor2 (a,b)
carry = and2 (a,b)
bit False = 0
bit True = 1
num [] = 0
num (a:as) = bit a + 2 * num as
bitAdd a [] = [a]
bitAdd a (b:bs) = s : bitAdd c bs
where (s,c) = halfAdd (a,b)
prop_bitAdd a as = num (bitAdd a as) == bit a + num as
main = smallCheck 3 prop_bitAdd
We ask smallcheck to run tests to a depth of 3. You can compile and run it using:
~~~~ {.shell} $ ghc –make BitAdd.hs [1 of 1] Compiling Main ( BitAdd.hs, BitAdd.o ) Linking BitAdd …
$ ./BitAdd Depth 0: Completed 2 test(s) without failure. Depth 1: Completed 6 test(s) without failure. Depth 2: Completed 14 test(s) without failure. Depth 3: Completed 30 test(s) without failure. ~~~~