Top 10 programs – Haskell version

20 04 2008

Last week I saw on Tom Moertel’s Blog the command to show the 10 most used programs:

history | awk '{print $2}' |sort | uniq -c | sort -rn | head

I do not show my top10 because I was expecting to have time to do a Haskell version :)

Based on .bashrc of Joachim Breitners I came up with this idea:

history | hmapw 'drop 1 . take 2' | hmapl List.sort | uniq -c | hmapl 'take 10 . List.reverse . List.sort'

But this is a kind of cheating, because I’m using the unix uniq.

So, I changed Joachim Breitners .bashrc to this:

...
if which ghc > /dev/null
then
        function ust { ghc-6.8.1 "-e interact ($*)" ~/Ust.hs ; }
        function ustmapl { ust "unlines.($*).lines" ; }
        function ustmapw { ustmapl "map (unwords.($*).words)" ; }

        function hmap { ghc-6.8.1 "-e interact ($*)" ;  }
        function hmapl { hmap  "unlines.($*).lines" ; }
        function hmapw { hmapl "map (unwords.($*).words)" ; }
fi

And based on and Unix Simple Tools I did Ust.hs:

module Ust(tail10, pick, uniq_c) where

import Control.Monad.Instances
import Data.List

-- @http://www.haskell.org/haskellwiki/Simple_unix_tools
-- return the last ten lines of a file
tail10  = drop =<< subtract 10 . length

pick n = (:[]) . (!!n)

uniq_c l = [ nl (tam l) i s | (s,i) <- uniq_c' l]

tam = maximum . map snd . uniq_c'

uniq_c' [] = []
uniq_c' (h:t) = let (list,rest) = span (==h) t
                    n = length list + 1
                in (h,n) : uniq_c' rest

nl tam n line = let l = length $ show n
                    l_tam = length $ show tam
                    n' = replicate (l_tam-l) " "
                in concat n' ++ show n ++ " " ++ line

And here it is, the Haskell version:

history | sutmapw 'pick 1' | sutmapl 'reverse . tail10 . sort . uniq_c . sort'

Explanation

pick 1 ["a","b","c"] = "b"
reverse [1,2,3] = [3,2,1]
-- 'tail10' return the 10 last elements of a list
-- 'sort' you know...
-- uniq_c = uniq -c (from unix)

sutmapl convert a String (with a lot of ‘n’) to the list of Strings separated by ‘n’.
sutmapw converts a list of String in a list of lists of Strings separated by ‘t’ or ‘ ‘.

The composition in Haskell works in the same way as the mathematical composition.
In the Unix console the pipe acts like (;), so we must reverse the parameters of the composition (.) to be equal to (;).

Btw, here is my top10:


623 make
433 sudo
380 cd
247 xpdf
176 ./game
175 ./client
129 man
117 ls
76 ./server
11 history

About these ads

Actions

Information

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




Follow

Get every new post delivered to your Inbox.

Join 185 other followers

%d bloggers like this: