Skip to content

Instantly share code, notes, and snippets.

@witt3rd
Last active September 13, 2020 16:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save witt3rd/816072314430e719ffad37008c834f4f to your computer and use it in GitHub Desktop.
Save witt3rd/816072314430e719ffad37008c834f4f to your computer and use it in GitHub Desktop.
Haskell setup

Haskell

Setup

curl -sSL https://get.haskellstack.org/ | sh

New Project

First, setup your ~/.stack/config.yaml with your basic info to be used for all new projects, e.g.:

templates:
  params:
    author-name: Donald Thompson
    author-email: donald@witt3rd.com
    copyright: Copyright (c) 2020
    github-username: witt3rd
    category: Experimental

(see Categories)

stack new my-project [some-template]

GHCi

Setup

stack install pretty-simple

~/.ghc/ghci.conf

-- Pretty-printing
:set -package-id pretty-simple
:set -interactive-print Text.Pretty.Simple.pPrint

-- Turn on some language extensions you use a lot
:seti -XFlexibleContexts -XOverloadedStrings -XTypeApplications

-- Break on errors
:seti -fbreak-on-error

-- Automatically show the code around breakpoints
:set stop :list

-- Use a cyan lambda as the prompt
:set prompt "\ESC[1;36m\STXλ \ESC[m\STX"

-- Better errors
:set -ferror-spans -freverse-errors -fprint-expanded-synonyms

-- Path-local ghci history
:set -flocal-ghci-history

-- Better typed holes
:set -funclutter-valid-hole-fits -fabstract-refinement-hole-fits -frefinement-level-hole-fits=2

Load a module

:m Data.Array

Haskell Language, Syntax, Idioms, and Packages

Arrays

ax = array (0,3) [(0,'a'),(1,'b'),(2,'c'),(3,'d')]
assocs ax
-- [(0,'a'),(1,'b'),(2,'c'),(3,'d')]
bounds ax
-- (0,3)

Functors

Composition

fmap (\x->x+1) (Just 5)
-- 6
(\x->x+1) <$> (Just 5)
-- 6
(+1) <$> (Just 5)
-- 6
(+1) <$> [1..5]
-- [2,3,4,5,6]
((+1) <$>) <$> Just [1..5]
-- Just [2,3,4,5,6]
-- :m Data.Functor.Compose
(+1) <$> Compose (Just [1..5])
-- Compose(Just [2,3,4,5,6])
getCompose ((+1) <$> Compose (Just [1..5])) 
-- Just [2,3,4,5,6]
getCompose ((*2) . (+1) <$> Compose (Just [1..5])) 
-- Just [4,6,8,10,12]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment