Arbtirary thoughts on nearly everything from a modernist poet, structural mathematician and functional programmer.

Showing posts with label Computers. Show all posts
Showing posts with label Computers. Show all posts

Thursday, May 6, 2010

Mu/Wu...

Mu is a Japanese word. I'll let you look it up there, since it's easier.

Programmers tend to like to re-imagine koans and Buddhist stories; so here's a classic question re-envisioned.

Prelude> Does the dog have the Haskell nature?

Couldn't match expected type `Human' against inferred type `Dog'.
In the first argument of `Haskell nature?', namely `dog'
In the expression "Does the dog have the Haskell nature?"


Cheers.

Monday, November 24, 2008

Developing in "impractical" languages

So... functional languages, in the case of ones I know, Scheme and Haskell, are often called "impractical" for real applications. Due to the small libraries, this is mostly true of scheme, but it is only because scheme libraries are small... It is not true of CommonLisp, or Haskell, or OCaml, or ...

The most common argument is that they run slower. This falls flat for OCaml, which outpaces basically everything (that means you, imperative language) except C; also, CL is one of the faster languages around. But everywhere I look in relation to Haskell has some sort of argument about it being "slow and impractical".

Let's look at the cost ($$) of developing and running a piece of software written in C vs one in Haskell. Also let's assume that it is computationally expensive (uses a lot of CPU power).

Now, everywhere I've looked, suggests developing in functional languages is at least 2 times (actually closer to 3 or 4 times...) as fast as developing in their imperative counterparts. But! We'll give C the benefit of the doubt, and say that developing in Haskell takes 2/3t (e.g. if a C program takes 3 months to develop, it will take 2 months to develop it in Haskell).

Now, we'll continue with a conservative estimate of $40000 a year for a programmer. For a job which requires a BS in CS and "several years" of experience... this is a modest (and, quite frankly, bad) pay... but programmers love their jobs, right?

Now, a decent (but not huge) project is going to take, say, 9 months for 5 programmers to write in C. So 6 months in Haskell. This means the development cost of the C program is 5*40000*(3/4) = $150000. The Haskell program? 5*40000*(1/2) = $100000. Obviously, since we're looking at 2/3 the cost.
Now, we've just saved ourselves $50000. That's more than the salary for a programmer for a year!

But! We've got to take into account the extra amount of computation required by the Haskell program. Since the differences are constant (not asymptotic), and Haskell is ~3 times more "expensive" than C, we're looking at just getting a better computer here... We'll say one that's "twice as good" (whatever that means), which isn't quite enough, but we've been giving C the benefit of the doubt this whole time...

So, getting a computer that's "twice" as good is going to cost the average person less than $1000 extra-- $150 for RAM, $150 for a CPU and $200-$300 for video card: less than $600. Let's say $750.

So, in order for the imperatave program to be more efficient, all around, economically, there need to be roughly 50000/750 = 75 copies...
Hmm... that's not too much, now, is it?

Giving programmers more money (hurray!) increases this number some, but not all that much... However, there are a few things to note:

(1)In reality, every study/anecdote suggests about 2x speed with functional development. This means we have ourselves $100000, so 150 copies... still small...

(2)Giving programmers $50000 a year (on average... this includes project managers, etc) means an extra $25000 (given (1), above)

(3)The cost of hiring good programmers is not decreasing very fast... the cost of upgrading computers is. So in 5 years, this whole thing is going to be stupid.

(4)Most substantial programs take closer to 10-20 people 1 to 2 years (in C). Given (1) and (2), that's at least 400000 (taking 15 for 1.5 years, that's 1.125Mil). In Haskell? For 15 people it will take .75 years, so that's 562500. We've saved just under one million. So we can now sell almost 1000 copies... still not much.

(5)This is Haskell vs C. In reality, development is done in a combination of C and C++. OCaml performs only moderately slower than C (and faster than C++), and CL is only slightly slower than C++. So, working in OCaml will save about the same amount of time, and won't cost much (any) more to run; developing in Lisp will only cost a tiny bit more to run, and won't be much (if any) more than OCaml... especially with Macros, and CLOS.

(6)There are very, very few applications where performance matters that much... Rendering is just about the only common task. So the "It's too slow" argument is dumb anyway.


My point is: People see that Functional Languages are experimental (theoretical) hotbeds, and say "Oh, they must only be good for language theory"... This is not the case. They are, mostly, only a tiny bit slower to run, and are significantly faster to develop in...
Also, pugs, and darcs

Edit: So, I got my orders mixed up... (Actually, I was looking at an incomplete ranking when I said OCaml and Lisp are fast). The best way to look at things is: The Computer Language Shootout! Look at rankings... play around with what does what... etc.

Sunday, November 2, 2008

Euler and Haskell

I just started learning Haskell, which is a fun, and slightly ridiculous language-- it seems to be what happens when you let mathematicians design a programming language without supervision: It's too clever and uses way too much graduate level theory. Any language which makes frequent use of monads, functors and has a wikibook describing its relation to category theory is the result of an evil genius (or several, to be precise).

Anyway, I'm using Project Euler to learn it, at Anne's polite not-actually-a- suggestion. Which is to say, she brought up Project Euler, and I said "Oh! I can learn Haskell!" It's working rather well, and I recommend it.

Saturday, April 19, 2008

Functional programming in Perl!

Perl has Lisp-like lexical closures and it supports currying.

Both of these happen through perlref, a new feature in perl 5. Essentially, how it works is you can make anything a scalar ($) variable... anything. Sort of.

You can make a scalar variable that points to anything, which is, for the programmer, almost the same thing.

This way you can make a variable that is the curry of a function... I'm not going to go into the details, because my understanding of Perl is, well, yeah... It was fun to find out as I was searching for something else, however.

***
Perl, it seems, is a really fun language, for mostly opposite reasons from scheme. Perl's beauty comes from the amount of pre-imposed structure, but a very large language to work with. Pretty Perl is sort of like really well-written blank verse: it is going to have some obtuse words and awkward grammar, but it still flows well.

Scheme, on the other hand, comes from a small rule set, but a language which can be molded and formed as you please. Scheme would be comparable to some of Cummings better works: It follows (almost) no rules of language, but it drags you around the program nonetheless.

...
Or something like that. The poetry = programming analogy really falls flat under any rigorous scrutiny, but I hope I got the point across:
Perl- a lot of rules, but a lot of ways to exploit them
Scheme- few rules, meaning absolute freedom to exploit anything and everything.

Friday, April 11, 2008

Perl... and the pearl of great worth

Schlomo just sent me the scripts he's using to pick apart Shakespeare plays. They're really not that bad, but since (presumably) he's the only one who has had to deal with them, his documentation consists of #{file does this}. It makes it severely difficult to follow, when there are 10 scripts which assume things about each other. They are fair assumptions, but not helpful when you don't have the context to understand...

****
On another note, God has been active lately. We're all growing closer together, and it seems more people are slowly joining our community. It's really exciting

Tuesday, March 25, 2008

Lambda Calculus...

Is weird. Really weird.

Why is it that we can think of sets of sets intuitively, but operating on operators is nearly impossible to conceptualize?

On a related note: It's ridiculous how exactly LISP notation follows Church's function notation. Even things that aren't related (necessarily) to lambda calculus are borrowed from him.

Friday, March 21, 2008

CommonLisp; Also, The Silver Chair... and Abstract thought

So, OOP has been too ingrained in my skull for me to properly use Scheme for an AI project I have to do, so I've decided to use CommonLisp- It's still Lisp, right? I now know full well why people say "CommonLisp is more powerful than Scheme." There are some really useful things you can do easily with CommonLisp. However, I also know full well why people say "CommonLisp is ugly and convoluted compared to Scheme." Line-noise like this: (format t "~{~a:~10t~a~%~}~%" cd) is why I don't want to learn perl... And I now have to deal with it when programming in lisp. Granted, it does a lot, but is it really worth memorizing all the syntax? Probably, but also definitely not.

***********
On a (probably) more interesting note, I really, really love the scene in the Silver Chair during which the Queen tries to convince the children, the prince, and Puddleglum that Narnia is a dream. Her arguments, which fall absolutely flat when you're outside the scope of the novel are used so often. Of course we can't explain God's presence, it isn't something we have a concrete understanding of.
Questions:
How does someone with Synesthesia describe the shift and change of colors as they hear music? How does one describe the sun to a blind man? Music to the deaf? Snow to someone from the tropics? A computer to a tribesman?
Answer: In metaphor and simile.
But of course metaphors and similes will present the object in question as a twisted version of the concrete object it is being compared to, rather than a distinct object with a similar sensation.

tangentially,
I can (sort of) understand how a synesthesiac thinks, because I have a similarly abstract and incommunicable thought-process. The best way, I think, to describe it is as imagism. I perceive, with all 5 senses and abstract thought an "emotional and intellectual complex" whenever reading, writing, drawing, seeing, doing math, coding, ...
My thought process is a string of Pound-ian images, which shift, move, fall, rise, appear and disappear in conjunction with everything that enters my brain through any medium. There are uncountably many stories, scenes, colors, sounds, tastes, ideas and so much more that pass through my mind every second, and over any period of time, and also distinct from time.

So, how does this relate to synesthesia? If I equate these images to colors (or whichever other sensation the synesthesiac perceives), I see the same thing.

Moving back to my string of questions, is there any way to truly describe my thought process? I've given a quick description; everything I said is true, but there is so much more to the mental "image" than the scenes and Pound-ian images I've described.

Monday, January 14, 2008

On video games, and adult content

This was supposed to be a post here

So... I've been playing video games for longer than I can remember. I'm sure I've played every game that any activist has ever said should be banned. As someone who is studying math and computer science, and plans on either doing research into fields that most of the world hasn't even heard of, or teaching at a Christian school in some random country, I really, really just don't see where the outrage comes from.

Lot's daughters rape him; Noah passes out drunk and naked; David (A man after God's own heart!) has his way with a woman (it never says how compliant she is, but since they marry, we can assume she didn't object) and kills her husband; Prophets rain fire from heaven; there are prostitutes; there are "male temple prostitutes"; there is a vile amount of sex and violence in the Bible. And yet, most of us here (myself included!) would encourage a child to read it. Why?
1) It represents a strikingly accurate picture of how vile and corrupt we humans are.
2) It is a song of praise to the glory of God.

Crime and Punishment contains a graphic depiction of a murder. 100 Years of Solitude contains multiple rapes, sex scenes, murders, massacres, ... Frankenstein is about a vile corruption of nature. Therese Raquine is horrifyingly gruesome
Many, many, classical books contain the same. Graphic depictions of sex and/or violence are an inherent part of literature.
And yet, most people here (myself included!) would encourage a child to read these things. Why?
There is an intellectual stimulation in reading literature; vocabulary improves, writing ability improves, speaking ability improves, etc., etc., etc.

Now, what we must remember is that sex is not evil; The portrayal of sex and the portrayal of violence are not necessarily evil, if in their proper place.

Now, mature people (I refuse the word adult) can handle portrayals of sex and violence, and either can see through them to the message (as in the Bible), or understand the entertainment or intellectual value of a story, which-- for immersion-- must necessarily contain sex and/or violence (as in Literature).

If you don't believe that there is intellectual stimulation in video games, find the name of a game and google search "[name of game] tactics" or "... strategy" or "... walkthrough". Look at the depth in the results you find.

Remember that there are no restrictions on ordinary literature, only on "dirty magazines". Remember that there is no *legal* restriction on the sale of R movies/tickets, or Parental Advisory music. What makes games any different?

There was a war with comic books. There was a war with movies. I'm sure the many centuries ago, there was a war with papyrus that sounded something like this:
Those rebels are using papyrus to write messages that blaspheme our gods and slander our king. We must control the use of this artificial memory.

I would have put something about "sex and violence", but that wouldn't be historical: the two things have been an integral part of nearly every civilization since the dawn of man. And certain people have the arrogance to claim that our "traditional morals" are being attacked? If those values are Elizabethan, Roman, Egyptian, Greek, Babylonian, Indian, Turkish, Germanic, Frankish, Nordic, Chinese, Arab...
If those morals were the morals of the crusaders, the zealots, the pharisees, the Conquistadors, or slave-owning Americans...
Well, if any of those are the case, I really don't think I mind seeing any of those corrupt, broken moral systems corroded.

Instead, I will continue to study group theory, Galois theory, and combinatorics; I will continue reading about those two harlots- Oholah and Oholibah- and when I need a break, I will read about gruesome murders and outrageous rapes in books that have been praised for centuries, or I will play video games- probably violent ones, or ones with "sex scenes" that are on prime time TV- and I will feel confident that when my kids experience these things, they will have been raised well enough to understand them.

Edit: it has been brought to my attention (see comments) that I have forgotten to point out how wrong the original article is. Not only is there no rape (according to everyone I've heard from who has played the game), but the "sex scene" only has (quoting my roommate) "a few seconds of ass."

Since someone posted as a response to the original article a youtube capture of the video in question (I did not watch it), I cannot accept that the game is as raunchy as Mr. McCullough makes it seem. I would like to call him a liar, but I'm much too fair, and a little wise for such name calling. The question is not a question of misrepresenting data to fool others, but a question of misinterpreting the data your senses feed to your brain-- He's irrational, not malicious.

You see, when someone has not been taught to critically analyze information they receive, they automatically jump to a conclusion based in part by what they do see, in part by what they expect to see, and in part by what they have been taught to see. Mr. McCullough (and others who have advocated censorship of interactive content) expect games to be such filth, so when they see the slightest evidence of it, they cache that-- confirmation bias. It then exaggerates itself, so that these people actually believe the nonsense that spews out from their mouth and pen. I could go on for a while about the state of an education system which does not teach one to think, but I want to make progress in 1st Chronicles; I want to make progress in an abstract algebra book, and I want to start a story (ironically and coincidentally, it's about preconceptions)

Mr. McCullough is wrong. He is wrong about what happens in Mass Effect; Were he right about the nature of Mass Effect, he would still be wrong.

Thursday, January 3, 2008

Dijkstra

Yesterday, I read both A Case Against the Goto Statement and On the Cruelty of Really Teaching Computing Science, by Edsgar Dijkstra. They were both rather eye-opening. One amazing thing about Dijkstra, is that he did not even own a computer until very late in his life, and rarely used them throughout his life... but he is one of the most important computer scientists in history. Why? Because "Computer Science is no more about computers than astronomy is about telescopes." Just like telescopes for astronomers, computers are the tool used by computer scientists to engage their science with the real world. Fortunately for us, Computer Science is abstract theorization, rather than concrete study of physical phenomenon. Because it is abstract theorization, a computer scientist is allowed to spend no time actually using a computer.

The point is that computer science is not a discipline, but a branch of mathematics. A branch that deals specifically with the generating, solving, and analyzing algorithms. Some people may try to disagree with me on this. "How is a professional [insert type of program here] an algorithm we can analyze?"
Firstly, this argument is ridiculous, because all a computer can do is manipulate symbols by a concrete, predefined set of rules. That is, all a computer can do is process an algorithm. If you don't believe me, read anything on the theory of computation (typically by mathematicians), specifically pay attention to Turing's work. Anything a computer can do must be describable by an algorithm... Would you be surprised, then, if I told you that all programming languages are just systematized algorithm languages? All compilers do is translate these algorithms to a form the computer can use.
Secondly, I would hardly call most of today's programming an exercise in computer science. To continue Dikstra's analogy, putting a satellite in orbit is not a job for astronomers. A computer scientist lays the groundworks: creates algorithms, creates languages, etc, and then the programmer uses these as tools for a finished product. Just as a physicist doesn't need to know why group theory works, just that it does, or an aerospace engineer doesn't need to know or why wings work, just how they work.
The why and how are for theoretical disciplines, primarily math and computer science (I know, I'm biased away from sciences). The engineer, or applied scientist doesn't need to know why,they need to know what and sometimes how.

Cheers

Saturday, December 22, 2007

Ideals, Scheme, and useful kludges: 3 unrelated topics

So... Today I met with a friend I've seen once (maybe) since highschool. We were never really that close, but 2 years age difference will do that... I've often complained about women, drama, etc, but I do find myself often preferring the company of women. I've long struggled to find an answer, and I think I've begun to understand.

It hinges around my idealistic hopes for the world; I have an almost kitschy ideal of human relationships, and there are certain types of interaction I find stimulating. I'm still having trouble pinning these down, but, I digress.

The longing after my ideal place in the world is something I've comet o call nostalgia, however (in)correct that term might be. It is certainly related to the concept of sensucht, angst, despair, nostalgia, discussed by many philosophers of the 19th and 20th centuries. Anyway, when I feel some sort of fulfillment of this ideal, I experience the emotion nostalgia, in all of its painful beauty.

Now, how does this relate to my preference for the company of women? Nostalgia, faith, romance, platonic love, and empathy all fall under a class of emotions; all of these ideals trigger the emotion nostalgia (as well as the emotion they are associated with.) I find myself able to sit down and converse with female friends much more often than male friends. I'm not sure why this is, but a personal connection is easier for me to establish with a woman. Something tells me that I find this connection easier to make because of a desire for romance with these women, but I find this very difficult to accept. Tiffany, Rachel, Katie, Kate, Haley, Tessa, Melissa, Sasha, Adriana, Adie. Of these, I've only felt a "crush" on Tiffany, Katie, Melissa and Adie. Of those, only Katie has developed into something I'd classify as romance. Tiffany was an emotional security blanket, Melissa was the last person I had an engaging conversation with before leaving the springs, and Adie was the first person I truly felt close to in Chicago, so it follows that I'd have had a short-lived crush on each of them.

What really makes sense, is that all of these girls represent an approximation of my ideals. They evoke nostalgia, which allows my to feel a solidarity with them. I haven't met nearly as many guys who do this, perhaps because men make friends in a fundamentally different way than women; the push for alpha status has to be set aside before this sort of friendship can develop, or it has to be clear that we think in a similar way for me to be able to relax around another guy; I guess I'm afraid of the guys "kicking me out".

Katie, then, seems to represent the closes approximation of this ideal. Perhaps it's her passion and faith; perhaps it's the warmth she radiates, but I can't seem to interact with her without getting excited...

*****
On another note, I'm having problems with my cell update functions for the Game of Life, so I've decided to sit down and force my way through the tedium of a whole Scheme tutorial, while of course supplementing it with "oo! I think I can do this!"
I'm still struck my how easy it is to do things in functional style. It's truly breathtaking.

*****
And finally, it ahs been explained to me why someone would want to use the kludges taught by koots. Granted, it doesn't excuse their use as a teaching element: good habits should be taught first, useful, yet inelegant kludges should be taught only after the concept they are useful for. So perhaps he's not a terrible coder, just an inelegant one: an engineer, not an artist.

Wednesday, December 12, 2007

Bad Coding, Bad teaching. OR: Why I want to be a professor

Edit: It seems this site hates my whitespace.... Sorry

I've been helping a friend with CS homework the last few weeks. He's taking "Object Oriented Programming I", which is the intro to programming class for CS and related majors, taught in Java.

I've been helping him because his professor is a hack, an absolute hack. Were he a brilliant man, but a bad teacher (e.g. Calinscu), or an idiot who knew how to teach, it would be acceptable, but he cannot code well and he cannot think well.

Firstly, my friend said "I do not know what object programming is. I don't know how object oriented programming languages work." Investigation shows that the problem is not on my friend's end, because he's understood everything I've taught him. It's that the subject has not come up in his lectures. He has mentioned "template classes", which is a bit redundant....

But I digress. Code samples and critique! These are the professor's, not my friend's... (Some variable names have been changed, but the logic is precisely the same.) The numbers in parenthesis mean I'm going to comment on the line after the segment.

First, a BubbleSort (Copied from a handout):

public int [] bubbleSort (int [] array){
(1) boolean done=false;
(2) int [] arraySorted=array;
(1) int p=0;
(1) int temp=0;
(3) while(!done&&p<=arraysorted.length-1){ (4) done="true"; (5) for(int j="0";j<=arraysorted.length-p-2;j++){ if (arraysorted[j]>arraysorted[j+1]){
temp=arraysorted[j+1];
arraysorted[j+1]=arraysorted[j];
arraysorted[j]=temp;
done=false;
}
}
p++;
}
return arraysorted;
}

1: booleans initialize to "false" and ints initialize to '0'. explicitly setting these values is unnecessary. Perhaps a good habit, but unnecessary. p is also in no way descriptive. It isn't following the lisp '-p' convention; why can't he use 'i' or 'count'? 2: Also unnecessary. the parameter "array" does not point to the same location as the array that was sent to it, so the line "int [] newArray = bubbleSort(oldArray);" will create a copy of oldArray, and pass that to bubbleSort().
3:This 'done' flag is absolutely unnecessary... It does nothing. To make matters worse, he handles the flag how drunken frat boys handle glassware: throws it around till it hurts something:
4:Every iteration of the outer loop sets "done" to "false". This means the operation is made n times in the outer loop, where n is the number of loops. It is also set to "false" in every inner loop (as long as flipping happens), this adds ~nlog(n) operations to the procedure. The operations that are done to this boolean flag (which I remind the reader, does not need to be there) have a complexity of O(nlogn). He has effectively turned a flag, which, if properly used (assuming it did need to be there) will take exactly n+1 [O(n)] operations into a flag which will take between 2n and (2n)^2 operations.
5:This one is fun. On the first page of the "how to write readable code" handbook (which doesn't-- but should-- exist) is the following statement:
"Do not use magic numbers!" In CS there are 3 numbers: 0,1,n. Any number besides 0,1 number should be specifically labeled because it's being used for something specific. Better, he has "<=/*some number*/-2" rather than "