Java to Haskell in Zero Days

Posted by: Seth Lakowske

Published:

Haskell is a language that is foreign to most programmers coming from an imperitive language like Java, C#, C++, Python and the like. You find new concepts that make learning Haskell feel like learning your first imperitive language. The first step is to acknowledge this, so you don't get frustrated right away when you aren't productive immediately. This acknowledgment allowed me to pass through many frustrating moments trying to do something useful. I realized, I wasn't trying to write a web service, I was trying to learn haskell by way of writing a web service.

If you're a productive Java or C# developer and learning Haskell, you might at times find yourself a little hazy on some of the concepts and terminology. This document is meant to clear up confusion by using terminology you are familiar with to describe Haskell's terminology and syntax.

Learn Haskell by picking a toy application you enjoy

Maybe you like to chain together processes, or write algorithms. Whatever it is, do that so that you have some fun at the end of each struggle. Bite off small toy problems, and when you get too ambitious, scale back and bite off something smaller. When you're stuck for hours, you know it's time to scale back and pick a smaller topic within your toy problem domain to learn.

Java to Haskell translation

Feature Java Haskell
Functions
static <A, B> String = f(A a, B b) {
  return "hi"
}
              
f :: a -> b -> String
f a b = "hi"
              
Structs
class MyType {
  String a;
  String b;
}
              
data MyType = MyType {
  a :: String,
  b :: String
}

λ> result = MyType "hi" "world"
λ> a
a :: MyType -> String
λ> a result
"hi"
λ> b result
"world"
              
Typedef
interface PhoneBook extends Iterable<Pair<String,String>>
              
type PhoneBook = [(String, String)]
              
Interface
interface Add2 extends Add1 {
  int add2(int number1, int number2);
}
              
class (Add1 a) => Add2 a where
  add2 :: Integer -> Integer -> Integer        
              

Haskell documentation

Haskell documentation is found on hackage. After some time you'll be able to read the definitions. Sometimes the author has documented their package well with examples. Until you're more familiar, you'll be better off learning the fundamentals, so go learn you a haskell.