Eta Cheatsheet

A quick reference for Eta

Input and Output

      module Main where
      main :: IO ()
      main = putStrLn "Hello World!"

Hello World


                module Main where
                main :: IO ()
                main = putStrLn "Hello World!"

$ etlas build

Or try it on Tour of Eta.


Binding declaration

            myName :: String
            myName = "Eta"

            main :: IO ()
            main = putStrLn $ myName


List declaration

users :: [String]
users = [ "Tom", "Dick", "Harry" ]

main :: IO ()
main = print $ users

List operations

main :: IO ()
main = do
 print $ head users
 print $ tail users


Basic structure

function :: type -> type
function x = expr

function1 :: type -> [type] -> type
function1 x xs = expr

main = do code

Control Flow

if-then-else expression

main = do
   if 36 `rem` 2 == 0
      then putStrLn "Number is Even"
   else putStrLn "Number is Odd"

Pattern Matching

This sets username to "Harry".

data User = User { name :: String, age :: Int }
  deriving Show

user :: User
user = User "Harry" 42

main :: IO ()
main = do
  let User { name = userName } = user
  print userName


Primitive Types

nil null
true / false Boolean
72 Integer
3.14(32 bits) Float
3.14(64 bits) Double
'hello' Character
"hello" String
['a','b','c']same as "abc" List
{a, b} Tuple


Declaring Module

New module named Main

              module Main where
              main :: IO ()
              main = putStrLn "Eta's first module!"


Importing Data.Time Module

import Data.Time

main = do
    now <- getCurrentTime
    print now

Qualified Imports

Qualified is similar to aliases

import qualified Text.Printf as T

main :: IO ()
main = T.printf "Hello %s! Your score is 100." "Harry"

Foreign Function Interface

Java Wrapper Type

Referencing a Java class inside of Eta

data JInteger = JInteger @java.lang.Integer
  deriving Class

Java Foreign Import Declarations

Import a Java method as an Eta monadic action

foreign import java unsafe coalesce :: Int -> Java JavaDoubleRDD JavaDoubleRDD

Marshalling Between Java and Eta Types

Java Type Eta Type
boolean Bool
byte Byte
short Short
char JChar
int Int
long Int64
float Float
double Double

Primitive Arrays in Eta

Java Type Array Type Element Type
boolean[] JBooleanArray Bool
byte[] JByteArray Byte
short[] JShortArray Short
char[] JCharArray JChar
int[] JIntArray Int
long[] JLongArray Int64
float JFloatArray Float
double JDoubleArray Double

Instance Methods

Import boolean canExecute() from the class.

foreign import java unsafe canExecute :: Java File Bool

Static Methods

Import File createTempFile(String, String) from the class.

foreign import java unsafe "@static"
  createTempFile  :: String -> String -> Java a File


Import File(String) from the class.

foreign import java unsafe "@new" newFile  :: String -> Java a File

Instance Fields

Import private String path from the class.

foreign import java unsafe "@field path" getFilePath  :: Java File String

Static Fields

Import String pathSeparator from the class.

foreign import java unsafe "@static @field"
  getPathSeparator  :: Java a String



Use all the cores of your CPU

import Control.Concurrent

main = do
  forkIO $ do
    putStrLn "Yay! i'm in thread!"
  putStrLn "I'm important i'm in Main thread!"


Channels help communicating with threads

import Control.Concurrent.Chan (newChan, writeChan, readChan)

main = do
    messages <- newChan
    writeChan messages "unbounded"
    writeChan messages "channels"

    msg <- readChan messages
    putStrLn msg
    putStrLn =<< readChan messages


MVar is a single-element Channel

import Control.Concurrent.MVar (newEmptyMVar, takeMVar, putMVar)

p :: IO Int
p = do
  num <- newEmptyMVar
  forkIO $ putMVar num 7
  forkIO $ putMVar num 9
  takeMVar num

main = do
  v <- p
  putStr $ show v

putMVarblocks if MVar has a value.

takeMVarblocks if MVar is empty.