[ToC]
Homebrew(macOS)
$ brew tap hamler-lang/hamler
$ brew install hamler
Install from source code(macOS)
-
Install Erlang
$ brew install erlang@23
-
Install Stack
Stack tutoriall https://docs.haskellstack.org/en/stable/install_and_upgrade/
-
Clone from the Git repo
$ git clone https://github.com/hamler-lang/hamler.git
-
Install Hamler
$ cd hamler $ make $ make install $ sudo mkdir -p /usr/local/lib/hamler/bin $ sudo cp repl/replsrv /usr/local/lib/hamler/bin/ $ sudo cp -rv lib /usr/local/lib/hamler/ $ sudo cp -rv ebin /usr/local/lib/hamler/
$ hamler repl
> -- List, range and enums
> [1,2,3]
> [1..10]
> ['a'..'z']
> -- erlang style maps
> import Data.Map as Map
> -- New map
> m = #{"foo" => "bar", "bar" => "foo"}
> -- Match Map
> #{"foo" := a, "bar" := b} = m
> -- get, put
> Map.get "foo" m -- a = "bar"
> Map.get "bar" m -- b = "foo"
> m1 = Map.put "key" "val"
> -- keys, values
> keys = Map.keys m
> values = Map.values m
$ mkdir demo-project
$ cd demo-project
$ hamler init
$ make
$ make run
A module is simply a bunch of related functions, types and type classes. This makes a program a collection of modules. This helps organize your code and makes reusing some of the code easier.
Module declaration
This how we declare a new module and specify which of the functions or types are exported.
module Hello (greet, farewell) where
{- the module name can be a word or words separated by '.';
in this case it is just "Hello" -}
greet :: String -> String
greet n = "Hello " ++ n
farewell :: String -> String
farewell n = "Bye " ++ n
Module import
The syntax for import in Hamler is import <module name>
. This has to be done before defining any functions. One module can import as many as modules you wish, but there could be ambiguity when there are two things with the same name.
import Data.List --Modules are imported using their full names
import Data.Maybe (isJust, isNothing) -- We can choose which functions to import
import Data.Funtion as F
-- We can deal with ambiguity by adding an alias. This means we need to add "F." before every function that is exposed from Data.Function to specify that it is from this module
import Prelude hiding (fst) -- The Prelude is imported by default. By hiding `fst`, we can define our own version.
module Main where
import Prelude
main = print "Hello, World!"