Haskell data structure folding

When i started to learn functional programming and use functional programming languages like Erlang, Haskell and ocaml, i met some difficult things for my understanding. There are functional primitives map/filter and folds. After spending little time i understand map and filter functions, because it’s very similar to each other and their signature:

(a -> b) -> [a] -> [b]  - map signature, we can see that map has tow arguments functions from type a to type b and list with type - [a]

(a -> Bool) -> [a] -> [a] - and filter function signature, it’s only difference to map that filter functions must pass argument with type a, and return Bool type. And list with type [a] and return list with type [a].

Some little examples in haskell:

It’s not very hard. Other functional languages provides this functions in their standard library too. For example lists:map and lists:filter in Erlang, and map and filter from List Ocaml module.

But some time i didn’t understood list and other data structure folds. Let’s see in Haskell folds.

Haskell language provide 2 general (not only 2) functions to fold: foldl (left fold) and foldr (right fold). This folds for lists. Let’s see to signature of this functions:

(a -> b -> a) -> a -> [b] -> a - this is foldl signature. We can see that foldl passed 3 arguments: binary functions which take first argument with a type, second argument with b type, and return a type. Second function’s argument has a type, and list with b type. And return this functions a type. This functions combining functions with data structure (foldl/foldr with list).

The right fold has following signature:  (a -> b -> b) -> b -> [a] -> b

This is foldl and foldr implementation in Haskell:

As wee can see foldl start with head of list: … f z x … and then recursive go by tail: foldl f (f x z) xs. Foldr call function f on head - x and result of foldr f z xs, we got: f x (f x (f x (f []))).

Let’s to see some examples:

For example we have list of five elements - [1,2,3,4,5] and we must find product of this elements, let’s try to find it with foldl and foldr:

Very simple. But we can fold not only lists. For example in haskell module Data.Foldable, we have Foldable class type which providing fold behavior to different data structure:

If you have additionall or question, write me in comment.

java interfaces, haskel type classes and more

Fortunately, and perhaps to my regret, last time i work only with functional programming languages like erlang and haskell. Already a lot of time i didn’t use object oriented techniques. A lot of time i didn’t write class word :) only in Haskell, but we know that it’s not oop class. But in last time gaining increasing popularity scala programming language. If you read wiki about this progamming lanuage, you can find there:

Scala (pronounced /ˈskɑːlə/ skah-lə) is a multi-paradigm programming language designed to integrate features of object-oriented programming and functional programming

If scala part is oop, i think that i must refresh my memory about this paradigm. I read about scala classes it’s ok, then i read about scala object, it’s ok too, and now i read about scala traits. I read that scala trait very similar as java interface and i remembered that i never did not understand the purpose of these interfaces in most other object oriented programming languages like java or C# when i started to learn this languages.

And the most interesting in this story that helped me to understand the purpose of the interfaces. This is nothing more that Haskell class types. Yes! It’s realy interesting that functional programming idiom helped me to understand object oriented pattern.

I try to explain how haskell class types helped me to understand java interfaces in this post.

Read More

erlang-github-api progress

As i wrote in last post i started to develop erlang-github-api project (https://github.com/onlyshk/erlang-github-api).

A week later in this post i want to tell about progress of this project.

Now this library is very early stage. I user ibrowse (https://github.com/cmullaparthi/ibrowse) http client to communicate with github. I think it’s better http client written in erlang now.

At that moment you can work with gist code paste github service (https://gist.github.com/) and some general features.

Now implemeted:

  • Authorization (basic, by username and password)
  • get gist by ID
  • get gist’s description by ID
  • get gist pull url
  • get gist push url
  • get count of comments
  • get created time of gist by ID (Year, Money, Day, Hour, Minute)
  • get author login of gist
  • create new gist
  • delete gist
  • star gist
  • unstar gist

Usage:

I’m going to implement full wrappen github-api v.3

If you’re interested welcome - https://github.com/onlyshk/erlang-github-api

Wrapper Github api in erlang

I started to develop github.com  API wrapper in Erlang language. 

Welcome - https://github.com/onlyshk/erlang-github-api

g711 implemetation in erlang

I implemeted g711 for audio companding in erlang programming language.

Quote from wiki:

G.711 is an ITU-T standard for audio companding. It is primarily used in telephony. The standard was released for usage in 1972. Its formal name is Pulse code modulation (PCM) of voice frequencies. It is required standard in many technologies, for example in H.320 and H.323 specifications. It can also be used for fax communication over IP networks (as defined in T.38 specification). G.711, also known as Pulse Code Modulation (PCM), is a very commonly used waveform codec. G.711 uses a sampling rate of 8,000 samples per second, with the tolerance on that rate 50 parts per million (ppm). Non-uniform (logarithmic) quantization with 8 bits is used to represent each sample, resulting in a 64 kbit/s bit rate. There are two slightly different versions; μ-law, which is used primarily in North America, and A-law, which is in use in most other countries outside North America.

If you’re interesting, welcome - g711 in Erlang.