Archive | technology RSS feed for this section

Part II: How not to Start Your Haskell Program

9 Sep

< Part I: Is There a Such a Thing as ‘Real World’ Haskell? Part: III   How Haskell Monads are Like a Muppet >

My last Haskell post had me thinking that I might as well create a really bad tutorial outlining my own strategies to deal with Haskell in the real world.    Some of the issues I outlined were:

  • Tutorials for Haskell focussed on solving problems, rather than getting it to ‘do stuff’ which is what alot of users expect from their computer software.
  • Haskell is emerging as an important player in the future of code.   Both as a type of code itself and as a way of improving skills in such languages as Java and Python.   However, it will not make the mainstream unless ‘regular folk’ start using it.
  • Functional programs (in theory) are apt to be more sustainable, less buggy and more consistently documented.   Regular folk, working with imperative and/or objective and pseudo objective languages probably don’t realize this -> not until their code breaks and they have to re-install the wheel.

While I have some experience in other languages like Ruby, and Python, I’ve decided to approach Haskell as if I were a PHP developer instead.     There are some very good reasons for this:

  • PHP is a ‘do something’ language.    Basically, it takes dynamic data and outputs it to a web page.   While math problems and recursion-like methods are possible – it’s usually input and output (ie. taking data in and out of a MySQL database) that is the most important.    Anyone expecting to use Haskell for primarily I/O related stuff is going to die from frustration.
  • PHP code can get very messy, even in the hands of an experienced coder.   Documentation of code is all dependent on comments and external manuals.     Haskell can help your average PHP coder, because its type-system offers a kind of in-source documentation.
  • PHP is a ‘regular peoples’ code.    It is adopted by alot of people with varying skills and experience with computer software, many of whom have very little understanding of its long run limitations.
  • Well into its 5th release, alot of the work has already been done for your average PHP coder.       It’s almost not even necessary to create your own objects and functions with all the pre-created libraries available.     Unfortunately, it’s also widely assumed that you are going to create a web application ->  what if you want to turn your code into an App?    or a desktop application that uses data from a php created api?   or an intense web universe ala Second Life or World of WarCraft?
  • As an interpreted program,   it’s pretty simple to solve problems through trial and error.    Just keep hacking away at php, and eventually you can make some convoluted function do what you want it to do (in an unscalable way, of course).

I am also going to make a number of assumptions:

  1. That you have read through at least some of the Haskell Wikibook.    I do not intend to explain what a Monad is, or impress everyone with my knowledge of complex mathematical theory.    This tutorial is an inquiry about how to approach a Haskell applications, not learning Haskell itself.
  2. You have a fair knowledge of some imperative-style computer language (possibly PHP).

As suggested, I am going to look at developing a one-level ‘rogue-like’ RPG game.      If I were to start such a game in php, I might start with something like:

$playerChar = array ("name" => 'Bob', "class" => 'fighter', "STR" => '18', "DEX" => '14', "WIS" => '11', "INT" => '15');

I suggest this because PHP coders want to get to the action as fast as possible.   The PHP coder (let’s call her PHiP) will know that her first problem is making data output in some consistent manner, so will create a variable that mimics a possible set of data that will eventually be displayed to the screen (likely a webpage).   Knowing that things like the name and class of the character will change, PHiP will put some variables inside the array.    Then PHiP will include some other ideas about what a character will need, and a print_r statement to watch the action as she lashes out blindly for the right outputs:

$playerChar ['traits'] = ("name" => $name, "class" => $class ...); $playerChar ['equip'] = ("weapon" => $weapon, "armour" => $armour ...); print_r ($playerChar);

If you are a Haskell coder, you probably already see countless disasters in store for PHiP if she uses this approach with Haskell.    Here are a few:

  • variables in Haskell are always constant.     Once called, the values will not change.   Believe it or not, functional programmers will see this as a good thing (see side effects in Wikipedia).
  • playerChar [‘traits’] would be seen as a function with an input [‘traits’] that, using Haskell syntax, is a list containing one string value ‘traits’.   Haskell would be wondering what to do if it encountered other lists containing one or more other string value(s).
  • All of the variables inside the associative array $playerChar  are irrelevant in Haskell.  Unless they are constant.
  • Deciding what to do with the array would be overwhelming.    A list could be used, but would require that all values inside be strings.   Or it could be a huge tuple of various kinds of values, or a list of tuples.
  • Without being sure what types of values are needed to create the playerChar, there are almost sure to be problems letting PHiP’s player character switch its weapon from a sword to a mace and back.

In short, PHiP is using the wrong paradigm to code effectively in Haskell.    The issue is not a problem of syntax, or even understanding a problem.   The issue is the approach.

As the tutorial goes along, we will deal with PHiP’s player character.    But, to work in Haskell, I propose that you do not start with the data you want to put in and out.    Instead, you start with input and output itself – inside main – like this:

main :: IO() main = do x <- getChar if x == 'q' then putStrLn "Thanks for Playing" else pmove x >> main move :: Char -> String move a |  a == 'w'       = "UP!" |  a == 's'       = "DOWN!" |  a == 'a'       = "LEFT!" |  a == 'd'       = "RIGHT!" |  otherwise      = "HANG AROUND AND DO NOTHING!" pmove = print . move

Right now, this code is pretty useless, but it offers a few nice things to help make sure you don’t lose steam in your coding.

  • It creates an output that will work (sort of) like PHiP’s print_r string.
  • It offers a practical demonstration of point notation, which, in laymen’s terms creates a function pmove that prints (using print) the results of move.    Essentially, it keeps me from having to write “print (move x)” all the time.
  • I accepts an input ‘a’ that is a character.
  • It creates a strongly typed function ‘move’ that takes a Character and returns a String.   As we move forward, we can chain a series of functions together that will help PHiPs make her Haskell program to do more interesting things such as fight a monster.

(as it turns out, this code works slightly differently in Windows than it does in Linux.    Use the Linux version, because it makes me seem a little more competent.   If anyone can explain why Windows wants the CR for the getChar command, please  add it to the comments.)

I am not absolutely sure this is the very best approach to a rogue-like game, but I can guarantee it is better to start this way than it is to begin with creating variables.     As you read this tutorial, I hope you continue to use other documentations elsewhere to help you get your mind around my use of ‘do’, ‘getChar’, ‘putStrLn’ and guards ‘|’ here (pretty rudimentary Haskell code right now).

The next tutorial will look at shaping our dungeon and establishing where our player as he or she moves across the board.

Incidentally, as of writing this, I am working on a rogue-like game with my son called Rasghiosse (my son named it).    I have a public Darcs repository on Patch-Tag that you are free to join in on if you like.   As of writing this tutorial, I am not much further ahead than the above code, so I would appreciate your help.

A Kick in the H1N1 (hiney) – or How Social Media Can Help You When the Message Changes

18 Nov

The communications on the H1N1 (aka Hiney) vaccine in Canada has been a mess.    At first the message was ‘everyone should get the vaccine.’   Then it turned into ‘wait we don’t have enough vaccines for everyone, so it’s only young children and people with chronic illnesses.’   Doctors offices are getting calls all over the place.   H1N1 is over-publicized.   H1N1 is a real threat. And, there is actual evidence that Canada may be doing a better job than other countries at getting the vaccines out.

Some will argue that the problem is poor communication planning.   These problems  are no different from any communication problems.   Key messages change all the time.   Being prepared to change direction is all part of the PR game.   But I bet the people responsible here planned the heck out of this program.   I bet they had a communications plan that could make even the best firms blush at their prowess.   What they did not expect – and should have – is that the public expects faster, more personal and transparent responses to important public messages.   The public expects social media.

Here’s how an advanced social media plan would have benefitted the H1N1 campaign, even after all the messages changed.

Social Media is Fast

Twitter, Facebook, a blog, YouTube and other things like it bring out a message very quickly and easily.    That means the H1N1 message could have gotten out sooner, and offered an open and honest dialogue with the public about the risks, benefits and requirements for citizens to get the vaccine.   All of this could have happened *before* the big marketing push went out and got people all excited, and it could have switched gears as soon as people knew there were going to be problems with the supply.

Social Media Won’t Play “GOTCHA!”

People online like to bitch and complain.   They are skeptical and even jerky sometimes.   But one thing they tend not to do (because they will get their backsides whipped for it) is try and trap someone into a cheap gaffe just to sell newspapers.    If they do, political folks can call them on it easily and quickly.   In Canada, Health Care is political – there’s no end to how social media could have improved the message.

Social Media Builds Trust

The public will always be more receptive to changes in the message if they trust the source that’s saying it.    A clear, traceable road to the process of building, preparing, and distributing the vaccine would have been both inexpensive and indispensible to the messsage later on.

Let Networks Work For You

Having respected Health Professionals onside as the message was getting out would have been equally indispensable, and social media could have done a great deal to help establish those networks.   Networks can clear up questions before you have to, and maybe clarify things that you haven’t really been clear on (hey, nobody’s perfect) in a fair, constructive manner.

Social Media is Timely

Locally, both Capital Health and the IWK have made fair attempts at keeping their public informed about the wait times and availability of vaccines on Twitter.   It’s a modest effort, but an appreciated one, taking advantage of the timely nature of social media to keep the public informed.

Social Media Efforts Receive Feedback

If you are getting it wrong, your network will let you know.   Also, in the spirit of “there are no dumb questions – maybe someone else had that question but was afraid to ask,” the social media folks could have responded to some of those more nitpicky details without bulking up those press releases.

Social Media is Not the Same as Hype

Don’t believe what the media tells you.   Twitter did not ‘light up’ with all kinds of hype about H1N1.   The hype came from media outlets trying to sell news.   And when people told them that H1N1 was overhyped, they turned that into a news story too.   Social Media does not really get all excited about controversy.    People have opinions and sometimes it can be hard to filter through them all, but it doesn’t thrive on the kind of ‘fight or flight’ energy that traditional media does.

Social Media Thinks Long-Term

After flu-season is over, a good social media infrastructure could have been shifted into something more broad reaching.   For example, a communications effort to prevent all infectious disease.

Social Media Appreciates a Good Laugh

Social media give the messenger an opportunity to laugh at him or herself without losing credibility in ways that the traditional media does not.     People respond to joy.    They change their behaviors because of joy – even moreso than they do with fear.  Why not bring more joy into people’s lives?

In short, institutions with millions or billions of dollars in budgets cannot afford to let themselves down by ignoring social media in their communication plans.   At most, what I have proposed here would have cost $100,000 in staff time and expertise.   I am sure the H1N1 campaign had plenty more of that at their disposal and a big mess on their hands to show for it.

Four Gaming Sites to Destroy Your Social Life

3 Jul

I love online flash games.    The last time I owned a gaming console was back in the early 80s with Atari.   Seriously (though I would occasionally rent Supernintendo in the late 80s).

Finally, I am going to share the sites I know and love:

Kongregate

Maybe not always appropriate for small children (the chat has some potty-mouthing by 13 year olds, and some of the games are a tad violent), Kongregate is a standard site where budding and professional flash gamers strut their stuff.   As a bonus, the site offers “achievements” that you can perform (eg. get a certain score, pass a certain level or defeat a certain boss) to earn points that increase your “level” which in turn gives you additional street cred.

Games I like:  Music Catch ,  Gemcraft Level 0, Morningstar

Miniclip Games

Definitely more kid-friendly than kongregate, although I find some of the games a little bit too “advertisey” – meaning that they appear to have a specific commercial interest in mind.   There also seems to be a trend toward sprite-style games with neo-16 bit graphics.   Still, it is a standard and a good number of my favorite games are here.

Games I like:  Raft Wars, Bloxorz, Extreme Pamploma

Popcap

Popcap is unique in that they offer downloadable versions of their games – trial versions for free and then full versions for sale.   You can also just play their games online.    In general the focus seems to be around puzzle-style games, but they are very well designed and fun for just about everyone.

Games I like:  Bookworm

Newgrounds

This site is definitely not for small children without parental supervision, but it has definitely been one of my favorites for a long time.   Tom Fulp started the site with some rather riske flash animations and games (the site was very popular for a game that was a parody of the Columbine shootings), and grew up into a portal for Flash Games and movies showcasing the amazing creativity of internet users.     Make no mistake, many of the entries to the Flash portal still keep the spirit of Tom’s old site (parody, violence etc), but there are also ratings to help make sure you know when the offensive material is going to appear.

Games I like:  Portal Defenders (Ages 17+), Little Wheel (all ages), Exmortis 2 (Horror Game 17+)

What gaming sites do you like?

Follow

Get every new post delivered to your Inbox.

Join 3,521 other followers