Skip to content

Instantly share code, notes, and snippets.

View buggymcbugfix's full-sized avatar

Vilem Liepelt buggymcbugfix

View GitHub Profile
{-# LANGUAGE DeriveFoldable, DeriveFunctor #-}
{-# LANGUAGE InstanceSigs #-}
{-# LANGUAGE LambdaCase #-}
import Data.Foldable (toList)
import Data.List (nub)
import Data.Map (Map)
import qualified Data.Map as Map
import Data.Maybe (listToMaybe)
import Data.Monoid
@buggymcbugfix
buggymcbugfix / average.hs
Created November 26, 2017 22:11
Command line tool to find the average (arithmetic mean) of the input
#! /usr/bin/env stack
-- stack --resolver lts-9.14 --install-ghc script --package base
import Data.Maybe (mapMaybe)
import System.Environment (getArgs)
import Text.Read (readMaybe)
{- USAGE
Pipe input:
import Data.Monoid
data List a = List | List a :• a
xs • x = xs :• x
infixl •
instance Show a => Show (List a) where
show List = "List"
show (xs :• x) = show xs ++ " •" ++ show x

Unix tree in Haskell

My take on pretty printing a rose tree like the unix tree command does. Inspired by this blog post of a version in OCaml.

It is pure and idiomatic without using any library functions. Thanks to laziness, it doesn't wait to return until the whole string is built. As such it will print a finite prefix of large or infinite trees (see example below).

@buggymcbugfix
buggymcbugfix / catch-all.md
Last active October 11, 2017 15:02
Catch-all considered harmful?

Catch-all considered harmful?

I have been thinking about a potential source of bugs from catch-all pattern matches and would like to know your thoughts.

Motivation

Totality is usually a desirable property of a function and the catch-all can conveniently buy us totality. But at what price?

I have been indoctrinated that rigour goes above convenience (think along the lines of: "Once we indulge in the impurities of I/O, there is no redemption.")

I would like to evaluate the trade-offs between convenience for the programmer and a potential source of bugs.

-- Effectful example in Gram
-- Similar to Haskell
-- main :: IO a
-- main = readLn
echo : (Int -> <Int> {}) -> Int -> <Int> {W}
echo = \f -> \x -> let <a : Int> = write x in f x
dub : Int -> <Int> {}
import Data.Char(toUpper)
main = do
inpStr <- readFile "test.txt"
writeFile "output.txt" (map toUpper inpStr)
@buggymcbugfix
buggymcbugfix / total.hs
Last active October 18, 2017 08:50 — forked from tfausak/total.hs
Sums numbers on the command line.
#! /usr/bin/env stack
-- stack --resolver lts-8.15 --install-ghc script --package base
{-# LANGUAGE ScopedTypeVariables #-}
-- Usage:
-- $ echo -e "1.2 3.4\n5.6 not-a-number" | ./total.hs
-- 10.2
import Data.Maybe (mapMaybe)
import Text.Read (readMaybe)