Java to Haskell in Zero Daze

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.

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 you're 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
data MyType = MyType {
  a :: String,
  b :: String
}

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