Erlang Learning

I will be keeping my learning of Erlang here. So that I can access it anywhere.

1. Erlang conventions.

Atom -> Starts with lowercase letter or put a single quotation mark  to make something treated as atom.

The value of atom is atom. Example of atoms sunday, ‘Monday’, ‘this is an atom with spaces’.

Tuples -> Similar to C structure. They are used for grouping some information together

Stock_MS = { current_value 114,

put 10,

call 20}

Map ->  Map in Erlang works as

lists:map(fun(X) -> 2*X end, lists:seq(1,10)).

output will be : [2,4,6,8,10,12,14,16,18,20]

( Map in Haskell is also very similar)

map (\x-> 2*x) [1..10]

Lambada Expressions in Erlang and Haskell ->

Both languages support lambada expressions and a cool thing that I just learnt today is that like other normal functions, lambda functions can have higher order-isms as well

Erlang times function looks like


times = fun(Times) -> (fun(X) -> Times * X end) end.

Haskell times function looks like

let times = (\t-> (\num-> t*num) )

both are pretty similar in structure but I think Erlang way is more readable as there is an end instead of “)” so makes it easier to understand the boundaries of inside function.

List Comprehension -> 


This is one the the coolest feature I like about Erlang and Haskell (In fact I use it very often as I started to understand it)

Its closely related to set theory

Example Statement : I want to double of each number where number belongs to 1 to 10

Erlang ->


[2*X || X <- lists:seq(1,10)]

Haskell -> [2*x | x<- [1..10] ]

read “||” in Erlang and “|” in Haskell as : “such that”  and <- as “belongs to

so both read as “Give me a list of 2*X/x “such that” X/x “belongs to ” a sequence 1 to 10 ”

Example Statement 2 : List of Pythagorean triplets such at base, perpendicular and hypotenuse belongs to 1 to 10 real number 

Erlang ->


[ {X,Y,Z} || X<- lists:seq(1,10), Y<-lists:seq(1,10), Z<- lists:seq(1,10), X*X + Y*Y == Z*Z].

Haskell ->

[ (x,y,z) | x<-[1..10],y<-[1..10],z<-[1..10],x*x + y*y ==z*z]

To Do List…

1. Understand the Try Throw and Catch in Erlang and write some basic codes for that [done]

2. Try to make a .erlang file so that I can change the default directory when I open werl through cmd. [done]

3. Start writing Quantlib in Erlang [ Before June 201] [Started] — If someone wants I can provide beam files for testing or feel free to join.

Advertisements

Windows and Gvim custom setting

Windows autocomplete in cmd and explorer 

The history of most recently used commands from the Run command on the Start menu are stored in the following registry location:

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\RunMRU

To enable the Inline AutoComplete feature:

  1. Click Start, point to Settings, point to Control Panel, and then click Internet Options.
  2. Click the Advanced tab, and then click to select the Use inline AutoComplete check box.
  3. Click OK.

Registry hack if you don’t have access to these options

[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\AutoComplete] –> “Append Completion”=”yes”

vimrc useful codes

[Color theme pack]

http://www.vim.org/scripts/script.php?script_id=625

“This code map alt + space bar to work the same way as it works for other windows in Win os

” Alt-Space is System menu
if has(“win32”) && has(“gui”)
noremap <M-Space> :simalt ~<CR>
inoremap <M-Space> <C-O>:simalt ~<CR>
cnoremap <M-Space> <C-C>:simalt ~<CR>
endif

set autoindent

syntax enable

Right click to Open vim at a folder 

Modify your registry and you can make a handy option when you right click at any folder, to open gvim at the selected directory.

Create - >
[HKEY_CLASSES_ROOT\Folder\shell\VimHere]
Name the key as Something like "Open Vim here"

Create ->
[HKEY_CLASSES_ROOT\Folder\shell\VimHere\command]
Give the full path of installation here 
"%GVIM_PATH%/gvim.exe ."

Getting Rid of Maybe(annoying) Tilde (~)  files in gvim

These files are created because gvim is setup to keep backups they should be invisible really. (May be I will search a way soon to make them invisible rather than not having them)

1. Go to _vimrc file in your setting

2. Append a line set nobackup at end which foce gvim not to take a backup after each commit. 🙂

How to set gvim as a default editor for visual studio and MsSQL

1. Go to Tools -> External Tools

2. Add your gvim path there

3. Set arguments = -p -o –remote-silent +$(CurLine) $(ItemPath)

4. Note make sure to make your title intuitive like Gvim or something.. [You can use my setting as G&vim so that while editing the file I use keyboard shortcut <M><T><v> ]

Automatically insert Header file while coding.

I made a header file for my Erlang library that I wanted to use for each file that I create.

The quickest way I found was using :r command in gvim

so I added it in my _vimrc file as

map <F5> :r HeaderTemplate

So every  new file that I create can have it if I press F5 and enter.

I will be storing links of some amazing resources here which I found really helpful for learning vi

http://yannesposito.com/Scratch/en/blog/Learn-Vim-Progressively/

[Great resource for vim with nice visual effects as well ]

Maybe someday when I will be very efficient and free

http://vimgolf.com/

Solving project euler using functional programming

I will be solving all the problems in project Euler in functional style while I learn Haskell and Erlang 🙂

bb in all my code signifies for “backbone”

Problem 6 : Difference b/w sum of square and square of sum of first 100 natural number

%%% Haskell implementation

sumsquarebb [] = 1 % boundary condition
sumsquarebb  (x:xs) = x*x + sumsquarebb xs % main statement for sum of squares
sumsquare range = sumsquarebb [1..range] % Actual function that  takes the number 100
squaresumbb [] = 0 % backbone boundary condition
squaresumbb xs = (foldl (+) 0 xs)^2 % statement
squaresum range = squaresumbb [1..range] % the function that takes 100

diffinsum range = (squaresum range) – sumsquare range % This one just find the diffs

Problem 9 : Pythagorean triplet such as b + p + h = 1000 find b*p*h

I used list comprehension for this problem and it takes around 2 sec to calculate. It can be done in better ways as well but  this one is very dear to me as I first learnt list comprehension while finding Pythagorean triplets 🙂

[(x*y*(1000-x-y)) | x<-[1..1000], y<-[1..1000], x^2 + y^2 == (1000-x-y)^2, x<y]