Skip to content

Hasekell for Java programmers tutorial by code samples

License

Notifications You must be signed in to change notification settings

micwypych/java-vs-haskell

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

java-vs-haskell

Hasekell for Java programmers tutorial by code samples

Recursion

Hasekell implementation

showList' :: [Int] -> String 
showList' number 
   | length number == 0 = ""
   | otherwise = show (head number) ++ showList' (tail number)

showList :: [Int] -> String 
showList [] = ""
showList (n:numberButOne) = show n ++ showList numberButOne

Java implementation

Recursive definition of showList together with definition of head and tail methods

Remarks

  1. Two implementations with the same behaviour in Haskell are provided. The very first one is more similar to the one provided in Java
  2. Apostrophe (') is a valid character in Haskell's literals
  3. All the function names should start with non capital letter
  4. Type specification of a Haskell function and its implementation is splitted but after type specification one has to provide definition immediately
  5. Type specification of a Haskell function may be omitted and then compiler provides inferred one (but it's recommened to provide one oneself)
  6. Haskell functions are pure and side effect free
  7. The second implementation makes use of pattern matching to decompose input argument into more useful chunks. In the pattern matching expression one can use arbitrary value constructors of the provided value (list has two of them [] empty list constructor and (:) colon that pushes element on front of the list.

Monad type class

Hasekell implementation

class Monad m where
  (>>=) :: m a -> (a -> m b) -> m b
  return :: a -> m a

Java implementation

Monad definition

Remarks

  1. Haskell return method is a method not a keyword like in Java
  2. Haskell Monad type class requires type m which has kind * -> * it which corresponds to generic type in Java but:
  3. Java implementation is far weaker typed than Haskell one (uses Object instead of generic type T)
  4. One cannot specify class level methods in Java interface to force implementation of static method in subclasses
  5. Java bind implementation takes first argument of Hasekell type m a as implicit argument this
  6. Java bind implementation takes second argument of Haskell type a -> m b explicitly as another interface ToMonadFunction

Monad implementation of Maybe

Hasekell implementation

data Maybe x = Nothing | Just x

instance Monad Maybe where
  (>>=) :: Maybe a -> (a -> Maybe b) -> Maybe b
  Just x >>= f = f x
  Nothing >>= _ = Nothing
  
  return :: a -> Maybe a
  return x = Just x

Java implementation

Monadic Optional definition

Remarks

  1. The most similar type to Hasekell Maybe is Java 8 Optional class
  2. Java implements return as returnMonad because return is reserved keyword
  3. Java doesn't have pattern matching like Haskell therefore its implementation is just if else
  4. Java implementation is reckless about copying object just by assignment of references (just didn't want to obfuscate code more)
  5. Java simulates Haskell implementation as an Adapter pattern it is more similar I guess. In Haskell one can define instance of already existing types and therefore the only option in Java is to use Adapter in such cases.

Monad implementation of List

Hasekell implementation

data [] a = [] | (:) a [a]
-- [a] is same as ([] a)

instance Monad [] where
  (>>=) :: [] a -> (a -> [] b) -> [] b
  xs >>= f = [y | x <- xs, y <- f x]
  
  return :: a -> [] a
  return x = [x]

Java implementation

Monadic List definition

Remarks

  1. A similar type to Hasekell list is Java List<T> interface but one has to remember that Haskell lists can be infinite
  2. Haskell notation x <- monadic is generic way to take out something from inside of the Monad therefore if monadic :: [] Int then x :: Int. This notation is available in the do notation but with extension of MonadComprehensions can be avaible in comprehension syntax as well. list comprehension and monad comprehension
  3. Actually the definition of Haskell bind operator is tautology so don't think about it too much...

do notation

do notation in Haskell is just syntactic sugar and can be interpreted in means of simple lambdas and Mondad functions desugar

Java implementation

explicit bind notation

Unit testing

Hasekell implementation

Java implementation

Method definition Tests in JUnit Tests in AssertJ

Remarks