Hasekell for Java programmers tutorial by code samples
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
Recursive definition of showList together with definition of head and tail methods
- Two implementations with the same behaviour in Haskell are provided. The very first one is more similar to the one provided in Java
- Apostrophe (') is a valid character in Haskell's literals
- All the function names should start with non capital letter
- Type specification of a Haskell function and its implementation is splitted but after type specification one has to provide definition immediately
- Type specification of a Haskell function may be omitted and then compiler provides inferred one (but it's recommened to provide one oneself)
- Haskell functions are pure and side effect free
- 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.
class Monad m where
(>>=) :: m a -> (a -> m b) -> m b
return :: a -> m a
- Haskell return method is a method not a keyword like in Java
- Haskell Monad type class requires type m which has kind
* -> *
it which corresponds to generic type in Java but: - Java implementation is far weaker typed than Haskell one (uses Object instead of generic type T)
- One cannot specify class level methods in Java interface to force implementation of static method in subclasses
- Java bind implementation takes first argument of Hasekell type
m a
as implicit argumentthis
- Java bind implementation takes second argument of Haskell type
a -> m b
explicitly as another interface ToMonadFunction
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
- The most similar type to Hasekell Maybe is Java 8 Optional class
- Java implements return as returnMonad because return is reserved keyword
- Java doesn't have pattern matching like Haskell therefore its implementation is just if else
- Java implementation is reckless about copying object just by assignment of references (just didn't want to obfuscate code more)
- 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.
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]
- A similar type to Hasekell list is Java
List<T>
interface but one has to remember that Haskell lists can be infinite - Haskell notation
x <- monadic
is generic way to take out something from inside of the Monad therefore ifmonadic :: [] Int
thenx :: Int
. This notation is available in thedo
notation but with extension ofMonadComprehensions
can be avaible in comprehension syntax as well. list comprehension and monad comprehension - Actually the definition of Haskell bind operator is tautology so don't think about it too much...
do notation in Haskell is just syntactic sugar and can be interpreted in means of simple lambdas and Mondad functions desugar