DarkMatter in Cyberspace
  • Home
  • Categories
  • Tags
  • Archives

Haskell Notes


Update in Nov 2018:

Install Haskell Platform with sudo apt install haskell-platform, which include ghc, cabal, and stack.

Or only install ghc, cabal and hoogle with sudo apt install ghc cabal-install hoogle.

Verified on Ubuntu 16.04 Server.


Written in 2011:

On Linux

Installation

sudo apt-get install aptitude
sudo aptitude install haskell-platform

verified on Linux Mint 14 Xfce.

Install hoogle (offline documentation of Haskell):

cabal update
cabal install alex happy && cabal install hoogle

Ref: http://stackoverflow.com/questions/11753979/cabal-install-hoogle-error

Very slow, you can use the online version;

modify ~/.cabal/conifg:

remote-repo: old.hackage.haskell.org:http://old.hackage.haskell.org

Ref:

http://hackage.haskell.org/

http://www.haskell.org/haskellwiki/Hoogle

$ cabal update
$ cabal install hoogle
$ ~/.cabal/bin/hoogle data # create database
$ ~/.cabal/bin/hoogle map # search "map"

Save the following lines to ~/.ghci to enable you use ":hoogle" or ":doc" to get doc info in ghci:

:def hoogle \x -> return $ ":!hoogle \"" ++ x ++ "\""       
:def doc \x -> return $ ":!hoogle --info \"" ++ x ++ "\""

Sample Project

Based on http://www.haskell.org/haskellwiki/How_to_write_a_Haskell_program;

  1. create home folder: take haq;

  2. create source file "Haq.hs";

  3. stick it in version control:

    git init
    git add Haq.hs
    git commit -m "add first source"
    
  4. add a build system: cabal init;

  5. modify .cabal file: add "Haq.hs" after "-- main-is:" and uncomment this line(delete "--"); If you donot specify the license type, comment out the line "license-file";

  6. cabal update

  7. cabal install --prefix= --user

  8. run app: bin/haq me under , or run app in-place (under current directory): dist/build/haq/haq me

verified on Haskell Platform 7.4.2, Linux Mint 14:

$ ghc --version

The Glorious Glasgow Haskell Compilation System, version 7.4.2

Miscellaneous

  • ghc: compiler

  • ghci: interactive shell;

  • runghc: run haskell script without compile

  • Compile haskell source file to executable:

    $ ghc --make <FileName>.hs -o <ExeFileName>
    

for example:

$ ghc --make Test.hs -o test

GHCi usage

  • get current working directory: :!pwd (you can use :! to run any shell command);

  • turn on/off type declaration: :set +t, :unset +t

  • get type information of a object: :t

  • define local variable and show bindings:

    let x = 3, 
    :show bindings
    
  • load module: :m +Data.Ratio

  • change cwd: :cd

  • load file: :l

  • quit ghci shell: :q

  • get function doc: :i

Syntax

  • comments: --

  • comparison: /=, >, <

  • boolean operation: not, || , &&

  • Haskell requires type names to start with an uppercase letter, and variable names must start with a lowercase letter.

  • To construct a rational number: 11 % 29 (after ":m +Data.Ratio")

  • values in "where", "case" and "let" clause must keep the SAME indentions;

Shell Interaction Library

shqq, shelly

Ref: Shelly: Write your shell scripts in Haskell

On Windows

Use sublime text 2 as text editor, use cmder as console.

In Sublime Text 2 main menu: [Preferences -> Settings -- User], remove "Vintage" from "ignored_packages" to enable Vintage.

Use intero as haskell plugin for vim

When open a .hs file in neovim aftering installed the plugin parsonsmatt/intero-neovim, the intero compiling failed:

...
Logs have been written to: /home/leo/.stack/global-project/.stack-work/logs/haskeline-0.7.4.3.log

$ cat /home/leo/.stack/global-project/.stack-work/logs/haskeline-0.7.4.3.log
/usr/bin/ld.gold: error: cannot find -ltinfo
collect2: error: ld returned 1 exit status

$ /usr/bin/ld.gold -ltinfo --verbose
Attempt to open //lib/x86_64-linux-gnu/libtinfo.so failed

Google libtinfo.so ubuntu, find libtinfo-dev:

$ sudo apt install libtinfo-dev
libtinfo-dev : Depends: libtinfo5 (= 6.0+20151024-2ubuntu1) but 6.0+20160213-1ubuntu1 is to be installed


Published

May 13, 2011

Last Updated

Nov 9, 2018

Category

Tech

Tags

  • Haskell 11

Contact

  • Powered by Pelican. Theme: Elegant by Talha Mansoor