Imperative Language, Functional Language and Logical Language

Almost every computer science students know how to program in imperative language. They may ask "what is imperative language?" Till now, I couldn't explain it very clearly. But I can give you an intuition that C, C++, Java are imperative language. On the contrary, much fewer people would know what is functional and logical language.

In a pure functional language, such as Haskell, all functions are without side effects, and state changes are only represented as functions that transform the state. Although pure functional languages are non-imperative, they often provide a facility for describing the effect of a function as a series of steps. Other functional languages, such as Lisp, OCaml and Erlang, support a mixture of procedural and functional programming.

In logical programming languages, programs consist of logical statements, and the program executes by searching for proofs of the statements. As for functional programming languages, some logical programming languages such as Prolog, and database query languages such as SQL, while declarative in principle, also support a procedural style of programming.

In this article, I am not going to talk about languages themselves. I will emphasize on how to solve the same problem with 3 languages and maybe you can obtain some inspiration.

1. Obtaining a specified element in an array

 First, the code below is a Java fragment. Actually we could write array[index] directly. But I need to write some verbose part to make comparisons. And please forgive that I didn't do anything about errors.

Then, the code below is written in Haskell.

The first line specified types of parameters and return. From the second line, it is the body of function. "head:tail" means it is a array or list, "head" here is just a name that stores the value of the first element of array. The rest part is self-explained.

Then I'll give corresponding code written in prolog.

Annoying, isn't it? Actually, it can be thinner, but I changed some variable names to make things clear. As above, "[Head|Tail]" means array and "Head" is the first element in CURRENT array. It should be noted that all variables in prolog  starts with UPPER letters. Then, the "Index" is decreased everytime it invokes himself until it reaches 0. Finally, we have "Head" at the third parameter. I know what you are confusing now. We can ask for result like this: getItemAtIndex([1,2,3,4,5],3,X). You will get "X=4". Yes, prolog puts return in its parameter.

2. Reverse a list

Again, Java code:

And then, Haskell code:

Amazing, isn't it? The first line says that when parameter is an empty list then return an empty one. The second line puts "head" element to the last one in return and repeatedly do the same thing on its "tail" until the parameter becomes an empty one which satisfies the first clause. "++" is a concatenation operation in Haskell.

Prolog code:

The first line uses an empty list as a assistant to store temperary list. Then when the original list becomes empty, the last parameter which is used as return will take the same value of temperary list. The last line tells us, the process insert the head element into "Temp" list one by one until the first parameter becomes an empty list which conforms to the second line.

Now things becomes a bit harder.

3. Quick sort

The core of this algorithm is repeatedly putting elements greater or less than a given key on either side of the list and then do the same process on both sides.  Java version of quick sort is everywhere. I skip to Haskell code:

I know you may not believe this, but this is the attractive point of functional programming. "quickSort[y|y<-xs,y<=x]" in second line tells us function "quickSort" will take the parameter y that y is from tail "xs" of original list and moreover y should be not greater than x which is the head of original list and is picked up as the key. The return of this clause concatenate element "[x]" and the return from "quickSort[y|y<-xs,y<=x]". Eventually, the parameter would become empty, then the first line takes effect.

Then let's have a look at prolog code:

Telling the truth, this is not a efficient solution and I'll paste another one in few weeks. The third line first invoke "pivoting()" to seperate the list into "L1" and "L2" after comparing to "H" which is the head elemment of original list. Then, "quick_sort" the "L1" and "L2" part. At last, concatenate two sorted sub-list.

Then, I'll give you a more complicated problem.

4. The Domino Effect

The description of the problem can be found here. I won't follow the input and output format as the description because it's not what this article concerns about. For better understanding, I'll give a Java solution first.

 

[I've already have the answer. I'll write the rest part in few weeks. I hope my respectful readers can think about it.]

 

[Continue......]

 

Some helpful links:

1. Crossing borders-Explore functional programming with Haskell

2. Tutorial: Real world haskell

3. Introduction to the tool for programming in haskell ---- WinHugs.  Download here.

4. Tutorial: Prolog

5. Simple tool for prolog: SWI Prolog Editor

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值