Tag Archive : haskell

/ haskell

Haskell is one of powerful functional programming language. More specific, it is a polymorphically statically typed, lazy, purely functional language. It is the good language for expressing math equation and logic naturally. Haskell is based on the lambda calculus, thus it use lambda as the logo.

Despite of functional paradigm nature, Haskell is very good for network and distributed system application. It can also applied as application on cloud computing.

After installing haskell on Raspberry Pi, it is worth to test what Haskell can do. This article will discuss about creating a simple Echo Server on Raspberry Pi using Haskell programming language.

In this article I use:

  1. Raspberry Pi model B 512MB RAM
  2. Raspbian Wheezy 2013-07-26 hardfloat

Overview

What we really want to build in this article is a modified Echo server. This program will run forever (infinite loop) and listen on port specified by user when invoking program. Like any socket-based application, when a connection come, it will accept and process it according to our app’s logic. Our logic is so simple. Whenever user connect to server using telnet, this server will echo every line they write (echo). However when they write “ping” without quote, the server will reply with “pong”.

Preparation

Make sure you have install Haskell on your Pi.

Writing code to Pi can be done using any method. You can access your Pi remotely or access directly. At least, make sure you can create a file and write it.

Writing the Code

Write this snippet and save it as “echoserver.hs”:

import System.Environment (getArgs, getProgName)
import Control.Concurrent (forkIO)
import Network (Socket, PortID(PortNumber), withSocketsDo,
        listenOn, accept)
import System.IO (hPutStrLn, hGetLine, hFlush)
import Control.Monad (forever)

main = withSocketsDo $ do
    args <- getArgs
    prog <- getProgName
    case args of
        [port] -> do
            socket <- listenOn $ PortNumber (fromIntegral (read port :: Int))
            putStrLn $ "Listening for pings on " ++ port
            handleRequest socket
        _ ->
            putStrLn $ "usage: " ++ prog ++ " <port>"

handleRequest s = do
    (handle, _, _) <- accept s
    forkIO $
        forever $ do
            input <- hGetLine handle
            processPing (hPutStrLn handle) (init input)
            hFlush handle
    handleRequest s

processPing f msg = do
    if msg == "ping" then 
        f "pong!"
        else
            f msg

Also, make sure you pay attention to the indentation.

This code is simple and should run on any architecture supported by GHC, including our Pi.

Build and Testing

To build the above code, run this command (assume the code is saved as echoserver.hs):

ghc -O2 --make echoserver.hs

The -O2 flags are used to apply every non-dangerous optimization, but might make the compilation time longer.

After compilation successful, you will have a new file name echoserver. To run the server do this on your Pi:

./echoserver 5000

This will run a server and make it listen to port 5000.

Now on your local computer, open up command prompt / terminal and invoking telnet to connect to your Pi. Suppose our Pi is on address 192.168.1.11, do following:

telnet 192.168.1.11 5000

which will connect us to Pi on port 5000.

You can test by write any line there. Good luck 🙂

Running Haskell on Raspberry Pi

December 9, 2015 | Article | 1 Comment

Haskell is one of powerful functional programming language. More specific, it is a polymorphically statically typed, lazy, purely functional language. It is the good language for expressing math equation and logic naturally. Haskell is based on the lambda calculus, thus it use lambda as the logo.

Haskell can be installed on various operating system, from Linux, Windows, Mac OS. Most of them are Operating System for Personal Computer (PC). Now, can we install Haskell to different platform? ARM maybe? Yes it can.

In this article we will discuss about how to install Haskell on top of Raspberry Pi device. Specifically, the Raspberry Pi device which running Linux OS. The device I use as test is Raspberry Pi model B with 512MB RAM.

Preparation

Make sure you have a good preparation. You have downloaded Raspberry Pi’s OS images and write to your SD card. Therefore, I assume you have install Linux operating system on your Raspberry Pi.

There is no special treatment for installation. I assume you have a ready to play Pi with network support.

You can either use SSH or connect some peripheral (keyboard, mouse, display) to access Pi. We won’t cover much detail here.

Theory Behind

There are two package we will discuss on this article: ghc and haskell-platform.

GHC or Glasgow Haskell Compiler, is a package with compiler, and interactive interpreter for Haskell.

Haskell-platform is a platform, has a collection of haskell code and tools for developing Haskell application.

Installation

on Raspbian Wheezy

The raspbian I use here is 2013-07-26. To install Haskell on Raspbian, use:

# make sure you have root privilege to install
sudo apt-get install ghc haskell-platform

If you take it correctly, you has already install Haskell. It should be no problem at this point.

The real problem might occur here. The ghc used on Raspbian (per September 5th, 2013) are version 7.4.1 and according to Haskell official site ghci or the interpreter cannot be used for version lower than 7.4.2 on ARM based device.

Installing Haskell Platform on Ubuntu

December 9, 2015 | Article | No Comments

Haskell is advanced purely-functional programming language. An open-source product allows rapid development of robust, concise, correct software. It is also have strong support for integration with other language, built in concurrency and parallelism, debuggers, profilers, rich libraries, and an active community.

In this article we will discuss about how to install Haskell platform for Ubuntu. Any Ubuntu version and any architecture is fine. However in this article I will use Ubuntu 12.10 desktop edition.

I assume you have never install Haskell before. But if you have, you can read this section first.

In the rest of article, I assume you use / acquire root privileges to do installation. Otherwise, use

sudo su

to obtain it.

Prerequisite

Before we do installation, we have to fulfill the dependencies.

apt-get install libedit2 libedit2-dev freeglut2-dev libglu1-mesa-dev

Obtain the Material

The latest haskell version is 7.6.3 which can be downloaded from here. There are two choice depends on what architecture your computer has: i386 (for 32 bit) and x86_64 (for 64 bit). Any of them is fine, adjust it with your own machine.

Once you have download, extract it. We will refer this directory as ghc_dir

For example, this commands will download and extract for the 64 bit version

wget http://haskell.org/ghc/dist/7.6.3/ghc-7.6.3-x86_64-unknown-linux-n.tar.bz2
tar -xf ghc-7.6.3-x86_64-unknown-linux-n.tar.bz2

Next download Haskell Platform. The latest platform can be downloaded from here. Then extract it. We will refer this directory as ghc_platform

wget http://lambda.haskell.org/platform/download/2013.2.0.0/haskell-platform-2013.2.0.0.tar.gz
tar -xf haskell-platform-2013.2.0.0.tar.gz

Installation

This section will have compilation and installation from source.

First, go to ghc_dir and do

./configure
make
make install

Next, go to ghc_platform and do

./configure
make
make install

Along the way, the install scripts for Haskell Platform will give you prompts for the next step to take.

Have Installed Haskell Before?

If you happen to install Haskell before, you can read this section. If you are not, then you can finish reading the article.

If you have installed ghc6 before, you should remove that haskell package to avoid conflict

Then update the repository to get the latest index.

apt-get autoremove ghc6

If you install cabal separately (without haskell platform) you also have to remove it. The one you have to remove would be the executable only. For example we install it as /usr/local/sbin/cabal then:

Then update the repository to get the latest index.

rm /usr/local/sbin/cabal

Social media & sharing icons powered by UltimatelySocial