lunes, 18 de abril de 2016

Logic programming

Today's blog will talk about something that will make most programmers' minds melt (at least mine).

Since semester is almost over and my head is about to explode, today's song will be "Hold on" by "The Break", covered by the awsome cover band "Walk off the Earth", hope you enjoy, but mostly, if you're having a bad time, to remember you that you just have to hold on to what you got, and sooner or later, things are going to turn around great.



The main difference I see in logic programming compared to traditional programming paradigms is that you don't really program the solution to a specific problem, instead you program the logic rules and relationships of a certain problem and just as it if were magic, you get the result.

So, what's the magic behind logic programming? Well, in logic programming, once you set up the logic rules, the program tries to match using some sort of query and any sucessful match is considered a solution.

Let me explain this more clear:

(run* [q] (membero q [1 2 3]))​
The code above (taken from "Seven more languages in seven weeks" does the following:

First, run* means that it will try to find all solutions, [q] is a given item, the third argument is a list which contains the function membero, which tries to find a relationship between the two proceding arguments, then we have q again, which is an item, and [1, 2, 3] which is a collection of values.

Translated to english that statement would mean something like: "Give me all items which match [1,2,3] so the result would be [1,2,3]

However, if we switched order in the argumens of membero we would get something like

(run* [q] (membero [1,2,3] q))

Which would return an infinite sequence of solutions, if we restrict the search, we could specify to end the search using  ​run number where number is the upper bound limit of our solutions list, we could do something like:

(run 3 [q] (membero [1,2,3] q))

That would return the first three valid solutions of the relationship, which would be 
[1,2,3 . _0] where . means something like "I don't care what _0 is, because anything entered would be a valid solution.

As we can clearly see in the last example, the logic behind logic programming is a lot like relational programming, where the expressions can be evaluated from both sides to find a relationship. 
In logic programming, we search for a relationship between data and our logic rules to find a solution.


-Is logic programming really that flexible? 
 

-Yes, that is why it bends minds.

The second solution would be [_0 , 1, 2, 3, . _1] where _0 would be "I still don't care" and _1 would be "I don't care either".

And so on, so basically the algorithm would return ANY possible list that has a consecutive 1,2,3 as values (emphasis on ANY) .

So, to this point we've seen that logic programming is flexible, powerful and reliable.
 

What's the trade-off? (As I've said in many previous blog entries, everything in software is a trade-off).

The main trade-off is efficiency, because since you never tell the computer which steps to make, logic programming mainly involves making every possible permutation of the problem's values to try a find a solution.

However, in exchange you get small, very, very powerful code. (Just imagine how many lines and how complicated compared to the previous code shown would it be to program that algorithm on some horrible, non-logical and non-smart language like Lua?

As a Lua programmer, my mind just melted the first time I saw logic programming (really).
It adds a new tool to the programmers mindset, and the advantages of logic programming language we read in Chapter 6 of  "Seven more languages in seven weeks", Minikanren is that it binds with a lot of other programming languages, so you can have the best of both worlds.


That's all for now my fellow reader(s).
See you next week, got to pick up what's left of my brain from the floor.
Wololo.

 

jueves, 31 de marzo de 2016

Language as the ultimate weapon


Today I will talk about the article "Language as the ultimate weapon in Nineteen Eighty-Four" by Jem Berkes.

The article mainly talks about how a specific language can manipulate people thoughts as George Orwell wrote about in his novel "1984", where Newspeak supressed ideas of fear or freedom that could be said in English (or "Oldspeak).
As the author suggests, language is a very important tool in expressing feelings, language can either empower or supress certain thoughts, there is even a theory that people can have different personalities depending on the language they are speaking.
Link to the article: http://www.dailymail.co.uk/sciencetech/article-3004943/Being-bilingual-really-two-minds-Researchers-say-people-different-personalities-language.html

The first time I heard about it was in middle school, I had a great Spanish teacher, he was still very young (about 20 years old) and was studying philosophy. He was teaching us how you could analyze people's real intentions by paying attention to the specifics words they said, his example was something like "English speakers usually focus mostly on the adjetives used to describe an object than the object itself, while Spanish speakers usually focus more on the object than the adjetives used to describe it, thats because of the language, in English you place the adjetives before describing the object, while in Spanish you first tell the object and then use adjetives to enhance its description, thats why in English you say "A red car" and in Spanish you say "Un carro rojo".

Even though it may be a very simplistic example, it really reflects how powerful a language can be. (How could you know if you're angry or in love if the language in which you are thinking doesn't have the words to describe either feeling?).

Another great example is programming languages, most like the entry where I talked about "The six men and the elephant". When facing a programming problem, a Java programmer will see a bunch of objects because the language in which he thinks represents it as objects, where a Lisp programmer will see data represented as lists being processed. The greatest power of Lips comes because Lisp doesn't recognize a difference between a program and data (programs are data, data are programs) where a C or Java programmer would blow their head up trying to understand what that idea means, in Lisp programs can easily write programs that wrote programs that write programs, because in the end programs produce data and data is programs so programs can produce programs (start sounding much like Inception).

As a main Lua programmer, I can tell that even though Lua tries to assemble Lisp's list processing power, Lua programmers mostly see things as glue or drugs, because you have to eat many glue as a child or be on drugs to consider using Lua. Bad jokes aside (even though Lua jokes never get old). I think the best thing about using Lua is that variables have to type and don't have to be defined.
I used Lua to program search algorithims for a simple AI game a few weeks ago, and that flexibility of the language helped me get everything done under 150 lines (the whole game). Since you have a lot of flexibility on variables, my board functions neither knew, either cared if the parameter they recieved was a real game, a simulated game or plain text, so I abused those functions in each way I could and used a lot less code than I may have used if I was programming in Java (lots of validations).

And the project had to be done in very short time, so I think Lua was a great choice, its list procesing features (that try to emulate Lisp and even the Lua manifest even brags about it) were great to process all the data.

I think there isn't a blog entry where I mention it, but the more I study different programming languages the more I get sure about it, everything in software development can be reduced in terms of a list (maybe a linked list or it could even be a dictionary) of trade-offs, so the more you know about any programming language, the more you understand the benefits and weaknesses of each trade-off and when the time comes where you have to make a choice, you are most sure you made the right call.

That is all for now, as an advice, try to read a lot about any new shiny programming language that comes out, not because every one of them becomes the great new C or Java or Pascal, but because they all try to make the best out a new way of thinking, and if you can understand the weakness of that way of thinking, you get to a point where you see the language as a tool to expressing ideas, and not the idea itself, and you get the most out the tool (the language) and the real idea behind it.

Wololo

The triumph of the nerds, part III

Today I will talk about the last part of "The Triumph of the Nerds".



I experienced a strong feeling of melancholy even though I hadn't even been born on that time, so the song of this blog's entry will be "On Melancholy Hill" by Gorillaz, enjoy.




First of all, the documentary is great, I found the last part to be perfect, seeing it right now, many years after it was produced gave me a feeling of melancholy, like I was remembering something I wish I could have lived. It is just great to hear the stories of the minds behind the P.C revolution, Steve Jobs' vision and search for becoming a legend, his devotion to the P.C was something out of this world, and Bill Gates' business ideas were much like seeing a chess game between two masterminds of computing history.

One of my favorite parts of the video was the part where the "Jobs vs Gates" relationship is explained, the great thing is that at the time of the video they were perceived as enemies, and now, many years after that we know both of them settled as they always ought to be, two masterminds of computing, both aiming for different things, both complementing each other.
And I may have a naive idea about it, but I think that their different ideas about computing were in a great sense to the computer revolution, Steve Jobs trying to push the limits of personal computers, searching for a faster, better, more beautiful computer (although always pricier), and Bill Gates, making computers accesible for everyone, I saw a video dated many years ago where Bill Gates was asked what were his thoughts about software piracy in India and his response reflected much what made Microsoft what it is now, he answered something like "Software empowers people, the great thing about it is that when India settles as an important software producer and people start buying software, we know it will be Windows' software".
So in a way Steve Jobs vision pushed the limits of computers, and Bill Gates business vision made computers accesible for everyone.

The documentary is dated before Steve Jobs returned to Apple, and before Windows invested on Apple, so it feels mostly like a time machine, and seeing it now just makes you want to go back in time just to say "In the end, everything went along well".

I like a lot the end, where Lawrence Ellison, founder of Oracle talks about the future (which now feels much like past) of the Internet.

I would like to add that there is a great article from "The Telegraph" that talks about Bill Gates' words about Steve Jobs' death, their relationship and a letter Bill Gates wrote to Steve Jobs and he kept by his bed, here is the link of the article if you would like to read more about it.
http://www.telegraph.co.uk/technology/bill-gates/9041726/Bill-Gates-I-wrote-Steve-Jobs-a-letter-as-he-was-dying.-He-kept-it-by-his-bed.html

Wololo.

jueves, 17 de marzo de 2016

Rich Hickey on Clojure

So, today I will talk about a podcast with Rich Hickey about Clojure.

Today's song will be one of my favorites, the song is "Giorgio" by Daft Punk, lots of soft funky bass slaps that get along well with the time I'm writing this entry.



Clojure is a Lisp dialect created on 2009 by Rich Hickey with a very bold promise, bring Lisp back more powerful than ever, and bring it to industry applications.
First of all, Rich Hickey defined Clojure as a language binded to the Java virtual machine, in a way that Clojure could run along with Java, the pointy haired bosses' favorite language, in a way that programmers could easily run Clojure code in a Java project, Rich even jokes about it. "Tell your boss that you're just using a new Java library".
Second, Clojure is focused on concurrency, with many programming languages having problems dealing with concurrency, and concurrency being a critical aspect of new generation programming languages due to the heavy use of multiple cores on computers and other electronic devices. Clojure attempts to solve that problem by using immutable data structures that can be processed by multiple cores without the use of locks or other complex mechanisms, by using atoms, agents or refs Clojure brings simple, yet powerful synchronization tools.
Third, Clojure extends Lisp by not only having lists as a primitive data structure but lists, maps, vectors, and sets, all of them work transparently with Clojure sequence API, which handles a lot of sequence processing.

In that way, Rich attempts to bring a more powerful tool, which works easily alongside other mainstream conservative languages, so you have the power of Lisp, macros and other obscure Lisp's uniqueness on a mainstream Java enviroment. Lisp has been reborn more powerful than ever.

And while I haven't had that "Eureka" feeling while working with Clojure, and I still find it somehow hard to adjust to the syntax (or should I say "lack of syntax"?) of any Lisp dialect. I find it very tempting to use with Java code. You could for example, code a videogame in Java and use native Java for UI and all the graphic related aspects and use Clojure for data processing like handling the enemies AI, that way, you could reduce a lot of coding time and deploy a powerful, platform independent game that could run virtually on any device (the one and only appealing feature about Java).

In that way I find Clojure to be a really promising programming language for the near future, I'm almost certain that in a few years Clojure will have its big boom. And so, the Lisp era will finally begin.

Thats all for now my fellow reader(s)
Wololo.

jueves, 3 de marzo de 2016

The promises of functional programming

Today I will talk about the article "The promises of functional programming" by Konrad Hinsen.

This blog's song will be "7 years" by "Lukas Graham", great song.

Enjoy.

The article talks about the differences between functional programming (as seen in Lisp, Mathemathica and other weird programming languages) and Imperative programming (as seen in C).

Lisp became a promise of better programming languages many years ago, however, that promise isn't still completely fulfilled, to anyone that has programmed in Lisp or any of its many dialects, it is very simple to see that Lisp has a completely different approach towards programming problems, and in many ways, a more powerful one.

I think I have said this many times, but there is always a tradeoff in programming languages, and Lips isn't the exception, it's killer feature is the same that makes it so hard, it is difficult to understand, no matter how many times Lisp gurus tell anything else, learning Lisp requires a lot of effort and thinking different, the main difference between Lisp and C is that in Lisp everything is made out of functions, pure functions (in C functions work more like methods or procedures).  So you have to think different to define a function, rather than a procedure. Hence why Lisp was considered a slow language many years ago, machines work internally using procedures, not functions, so the first thing that slowed Lisp's progress was how hard it was to translate Lisp code to machine language, sometimes, things get lost in translation.

However, as time passed by, hardware become more complex, right now parallelism is becoming the next big thing, and the main problem we have right now is that even if our machines have four or eight cores, most programs just use one or two, even games, which have always tried to push the hardware to its limits by using every obscure programming language they could be programmed (a couple of decades ago, a lot of games were still programmed in some sort of assembly language, to be more efficient, another great story is the engine behind Doom and Heretic games, programmed by the great Jogn Carmack, the engine wasn't purely 3D, instead, it displayed 2D textures and modified them using mathematical functions because computers those days weren't so great displaying 3D graphics, which made the game extremely fast and the objects would sometimes "bend"). I got a bit lost, returning to the point, even games nowadays use only 2 or at most 4 cores.

We still have many cores so we can run multiple processes at a time, but a single process almost never fully uses the computer, the main reason behind this is that programming parallel applications is a very hard thing to do, there are a lot of race conditions, data is often corrupted by other threads/cores.

Functional programming is a great promise to solve this problem because programs are interpreted as pure functions, not procedures, they don't use variables in a traditional way, hence being mostly thread safe, and I say mostly because there is still a lot of work to do regarding translating to thread safe, machine code. But any functional programmer can tell you that they have a lot less problems programming parallel applications than the guys who code in C.

In a couple of years, we will still use a lot of imperative programming, but it is certain that there will be a lot more functional programming as a common practice in programming.

Wololo.

miércoles, 24 de febrero de 2016

Triumph of the nerds, part II - Attack of the Clones

Today I will write about the documental "The triumph of the nerds, part II" which mainly talks about Microsoft/IBM early business relationship.

Recommended song, "She drives me crazy" cover by Miracles of Modern Science:

The triumph of the nerds, part II is a great video, so if you haven't seen it you can do it at https://vimeo.com/124201870

I think that IBM just wasn't suited for the P.C era, it's structure was too unflexible to tackle the quick changes in the industry. They built a bridge between personal computers and business, which made it easier for the clones such as Compaq to get into business, however, even if they hadn't done that, sooner or later the personal computer would enter the business world. It was inevitable since Steve Jobs and Woz built that P.C just to impress their nerd friends. It was just a matter of time.

Bill Gates wasn't much into hardware, his bet was on the software that made the hardware usable, computer programs and operative systems, which in the long run was a great decision, considering that computer hardware is one of the most competitive business as hardware has to become cheaper while doubling its capacity every two years.

Software business was at that point a blue ocean, particularly operative systems business. Everyone wanted to build computers, every computer had to be a replica of the IBM personal computer, the IBM computer ran a Microsoft O.S, so everyone had to buy the operative system from Microsoft.
Bill Gates wasn't only riding the bear, he was domesticating it.

IBM biggest mistake was to bet everything on the hardware that made up a computer, eventually they had to retreat to their main business of building mainframes. Their biggest weakness was their business structure. That is why today, startups are so important in the computer industry, they have more flexible business structures, they are usually made up of very few employees so they have a lot of control on what they are doing and can adapt very easily to changes, and since they are so small, they go for all or nothing, a startup usually bets everything on a new technology, and when everything goes fine, they hit the jackpot.
That sort of risks are usually imposible to tackle by the bear.

That is everything for this entry, don't ride the bear, domesticate it.
And as a bear, buy the startup, it is easier and usually better than fighting against it.

jueves, 11 de febrero de 2016

Revenge of the nerds

So, today I will write about the article "Revenge of the Nerds" from Paul Graham which talks mainly about the benifts of using a programming language that is not "industry standard".

Today's song is a tribute to my favorite, most hated/loved, language, Lua (the song title original translation is moon, but who cares).


At the begining of the article the author takes a blow on the "pointy haired boss" which takes decisions on software programming at some industry without knowing anything about software development, thus, choosing mainstream languages despite the fact that for each programming problem there is a different programming language which suits it best.

However, first thing I think about that argument is that it doesn't consider the "pointy haired programmer", which has just learned to program on a mainstream languages and encourages the boss' desition, the whole article is intented for a more intelligent programmer, one that agrees either directly or unconsciously that there isn't a perfect programming language.

The fact is that a lot of "industry standards" are not just some pointy haired boss who doesn't know about software development, but from a lot of pointy haired programmers who only know one mainstream programming language and agree to tackle every problem with the only language they know, they are the cornerstone of mainstream software development.

And I agree on that, not because I think everything can be solved with Java or the mainstream language but because for management purposes, most industy standard problems can be solved, and are best to be solved with industry standard languages programmed by industry standard programmers.

The obscure powerful languages ought to be used on obscure, hard problems.
And Lisp or any Lisp dialect may be the best to solve most of those problems, with some obscure, smart Lisp hackers you can tackle those problems industry standard organizations are scared to tackle, you may take those risks, do something bold and solve the problem, like Yahoo! Store or ITA.

So, there comes a point where a programmer has to choose if he wants to work at industry standards or become an obscure hacker, because no matter how much you trick your pointy haired boss that Lisp is XML, in the end industry standards will prevail, even if Lisp becomes industry standard in some weird way, there will be another obscure language borned that tackles all the obscure problems Lisp couldn't tackle as an industry standard, maybe it is something natural in computer programs, such as a way of evolution.