1
0

Compare commits

..

15 Commits

Author SHA1 Message Date
2ef8dad418 Update flake, latest haskell build with many libraries 2026-01-03 18:50:15 -05:00
c65e1b3828 Nix fmt formatter 2026-01-03 18:16:11 -05:00
640babea21 Setup precommit 2026-01-03 18:15:57 -05:00
f0b80ccc00 Switch to flake.parts 2026-01-03 18:15:21 -05:00
f564347e2c Thoughts for next steps 2025-08-05 09:20:00 -04:00
49d4f20b18 Make computation parallel 2025-08-05 09:18:33 -04:00
f511d1c0ea Update Haskell and Flake dependencies 2025-08-04 22:05:05 -04:00
23c65a00a8 Apply nix-rfc formatting to flake 2025-03-02 15:05:33 -05:00
ba2e16d000 Update dependencies 2025-03-02 15:04:10 -05:00
724b4abcf0 Advent of Code, 2024 - Day 2 Part 2 2024-12-29 14:13:25 -05:00
caee6ea61b Spelling 2024-12-29 14:12:58 -05:00
ea4d0e8580 Rebase laster some tests 2024-12-21 13:31:04 -05:00
d54b9f9812 Be smarter! 2024-12-21 13:06:55 -05:00
b818aa089e Advent of Code 2024 Day 1 and 2 2024-12-21 13:06:43 -05:00
2eecbe9980 Collatz changes 2024-12-01 23:37:39 -05:00
12 changed files with 1488 additions and 149 deletions

1
.pre-commit-config.yaml Symbolic link
View File

@@ -0,0 +1 @@
/nix/store/gr5dfsr20jqrim8w0lbhicgdnpwwwhs2-pre-commit-config.json

View File

@@ -13,6 +13,7 @@
"HLINT",
"mempty",
"Prec",
"succ",
"unrecognised"
]
}

View File

@@ -1,6 +1,6 @@
# [The Universal Calculator](https://git.ewanick.com/bill/universal-calculator)
Using the GHC REPL and Haskell to teach programming concepts through the guise of mathematics.
Using the GHC REPL and Haskell to teach programming concepts through the guise of mathematics.\
A handy assistant to help you understand and solve maths problems.
- GHC = [Glasgow Haskell Compiler](https://www.haskell.org/ghc/)

129
flake.lock generated
View File

@@ -1,24 +1,139 @@
{
"nodes": {
"nixpkgs": {
"flake-compat": {
"flake": false,
"locked": {
"lastModified": 1694304580,
"narHash": "sha256-5tIpNodDpEKT8mM/F5zCzWEAnidOg8eb1/x3SRaaBLs=",
"lastModified": 1767039857,
"narHash": "sha256-vNpUSpF5Nuw8xvDLj2KCwwksIbjua2LZCqhV1LNRDns=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "4c8cf44c5b9481a4f093f1df3b8b7ba997a7c760",
"repo": "flake-compat",
"rev": "5edf11c44bc78a0d334f6334cdaf7d60d732daab",
"type": "github"
},
"original": {
"owner": "NixOS",
"ref": "nixos-23.05",
"repo": "flake-compat",
"type": "github"
}
},
"flake-parts": {
"inputs": {
"nixpkgs-lib": "nixpkgs-lib"
},
"locked": {
"lastModified": 1765835352,
"narHash": "sha256-XswHlK/Qtjasvhd1nOa1e8MgZ8GS//jBoTqWtrS1Giw=",
"owner": "hercules-ci",
"repo": "flake-parts",
"rev": "a34fae9c08a15ad73f295041fec82323541400a9",
"type": "github"
},
"original": {
"owner": "hercules-ci",
"repo": "flake-parts",
"type": "github"
}
},
"git-hooks-nix": {
"inputs": {
"flake-compat": "flake-compat",
"gitignore": "gitignore",
"nixpkgs": [
"nixpkgs"
]
},
"locked": {
"lastModified": 1767281941,
"narHash": "sha256-6MkqajPICgugsuZ92OMoQcgSHnD6sJHwk8AxvMcIgTE=",
"owner": "cachix",
"repo": "git-hooks.nix",
"rev": "f0927703b7b1c8d97511c4116eb9b4ec6645a0fa",
"type": "github"
},
"original": {
"owner": "cachix",
"repo": "git-hooks.nix",
"type": "github"
}
},
"gitignore": {
"inputs": {
"nixpkgs": [
"git-hooks-nix",
"nixpkgs"
]
},
"locked": {
"lastModified": 1709087332,
"narHash": "sha256-HG2cCnktfHsKV0s4XW83gU3F57gaTljL9KNSuG6bnQs=",
"owner": "hercules-ci",
"repo": "gitignore.nix",
"rev": "637db329424fd7e46cf4185293b9cc8c88c95394",
"type": "github"
},
"original": {
"owner": "hercules-ci",
"repo": "gitignore.nix",
"type": "github"
}
},
"nixpkgs": {
"locked": {
"lastModified": 1767364772,
"narHash": "sha256-fFUnEYMla8b7UKjijLnMe+oVFOz6HjijGGNS1l7dYaQ=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "16c7794d0a28b5a37904d55bcca36003b9109aaa",
"type": "github"
},
"original": {
"owner": "NixOS",
"ref": "nixpkgs-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"nixpkgs-lib": {
"locked": {
"lastModified": 1765674936,
"narHash": "sha256-k00uTP4JNfmejrCLJOwdObYC9jHRrr/5M/a/8L2EIdo=",
"owner": "nix-community",
"repo": "nixpkgs.lib",
"rev": "2075416fcb47225d9b68ac469a5c4801a9c4dd85",
"type": "github"
},
"original": {
"owner": "nix-community",
"repo": "nixpkgs.lib",
"type": "github"
}
},
"root": {
"inputs": {
"nixpkgs": "nixpkgs"
"flake-parts": "flake-parts",
"git-hooks-nix": "git-hooks-nix",
"nixpkgs": "nixpkgs",
"treefmt-nix": "treefmt-nix"
}
},
"treefmt-nix": {
"inputs": {
"nixpkgs": [
"nixpkgs"
]
},
"locked": {
"lastModified": 1767468822,
"narHash": "sha256-MpffQxHxmjVKMiQd0Tg2IM/bSjjdQAM+NDcX6yxj7rE=",
"owner": "numtide",
"repo": "treefmt-nix",
"rev": "d56486eb9493ad9c4777c65932618e9c2d0468fc",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "treefmt-nix",
"type": "github"
}
}
},

267
flake.nix
View File

@@ -1,122 +1,181 @@
{
description = ''
A basic Haskell flake for solving problems and sketching out concepts.
https://tristancacqueray.github.io/blog/beautiful-haskell
'';
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-23.05";
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
flake-parts.url = "github:hercules-ci/flake-parts";
git-hooks-nix.url = "github:cachix/git-hooks.nix";
git-hooks-nix.inputs.nixpkgs.follows = "nixpkgs";
treefmt-nix.url = "github:numtide/treefmt-nix";
treefmt-nix.inputs.nixpkgs.follows = "nixpkgs";
};
outputs = { self, nixpkgs }: {
devShell.x86_64-linux =
let
pkgs = nixpkgs.legacyPackages.x86_64-linux;
ghc' = pkgs.haskell.packages.ghc928.ghcWithHoogle (self: with self;
[
relude
split
aeson
random
neat-interpolation
# maths
primes
arithmoi
# graphing libraries!
Chart
Chart-cairo
]
);
clean = pkgs.writeShellScriptBin "clean" ''
# Delete executables
find . -type f -executable -not -path '*/.git/*' -delete
# Delete all Haskell IR files
find . -type f -name '*.hi' -delete
find . -type f -name '*.o' -delete
# Delete any test graphs created
find . -type f -name '*.png' -delete
'';
in
pkgs.mkShell {
buildInputs = with pkgs.haskellPackages; [
ghc'
haskell-language-server
ghcid
hlint
] ++ (with pkgs; [
# Scripts
clean
go
gofumpt
nodejs
python310
black
]);
shellHook = ''
echo ".------..------..------..------..------..------..------..------..------..------."
echo "|U.--. ||N.--. ||I.--. ||V.--. ||E.--. ||R.--. ||S.--. ||A.--. ||L.--. ||=.--. |"
echo "| (\/) || :(): || (\/) || :(): || (\/) || :(): || :/\: || (\/) || :/\: || (\/) |"
echo "| :\/: || ()() || :\/: || ()() || :\/: || ()() || :\/: || :\/: || (__) || :\/: |"
echo "| '--'U|| '--'N|| '--'I|| '--'V|| '--'E|| '--'R|| '--'S|| '--'A|| '--'L|| '--'=|"
echo "[------'[------'[------'[------'[------'[------'[------'[------'[------'[------'"
echo ".------..------..------..------..------..------..------..------..------..------."
echo "|C.--. ||A.--. ||L.--. ||C.--. ||U.--. ||L.--. ||A.--. ||T.--. ||O.--. ||R.--. |"
echo "| :/\: || (\/) || :/\: || :/\: || (\/) || :/\: || (\/) || :/\: || :/\: || :(): |"
echo "| :\/: || :\/: || (__) || :\/: || :\/: || (__) || :\/: || (__) || :\/: || ()() |"
echo "| '--'C|| '--'A|| '--'L|| '--'C|| '--'U|| '--'L|| '--'A|| '--'T|| '--'O|| '--'R|"
echo "[------'[------'[------'[------'[------'[------'[------'[------'[------'[------'"
echo " _ _ _ _ "
echo "| | | | (_) | | ______ "
echo "| | | | _ __ _ __ __ ___ _ __ ___ __ _ | ||______|"
echo "| | | || '_ \ | |\ \ / / / _ \| '__|/ __| / _[ || | ______ "
echo "| |_| || | | || | \ V / | __/| | \__ \| (_| || ||______|"
echo " \___/ |_| |_||_| \_/ \___||_| |___/ \__,_||_| "
echo " "
echo " "
echo " _____ _ _ _ "
echo "/ __ \ | | | | | | "
echo "| / \/ __ _ | | ___ _ _ | | __ _ | |_ ___ _ __ "
echo "| | / _[ || | / __|| | | || | / _[ || __| / _ \ | '__| "
echo "| \__/\| (_| || || (__ | |_| || || (_| || |_ | (_) || | "
echo " \____/ \__,_||_| \___| \__,_||_| \__,_| \__| \___/ |_| "
'';
outputs = inputs @ {flake-parts, ...}:
flake-parts.lib.mkFlake {inherit inputs;} {
imports = [
# To import a flake module
# 1. Add foo to inputs
# 2. Add foo as a parameter to the outputs function
# 3. Add here: foo.flakeModule
inputs.git-hooks-nix.flakeModule
inputs.treefmt-nix.flakeModule
];
systems = [
"x86_64-linux"
# "aarch64-linux"
# "aarch64-darwin"
# "x86_64-darwin"
];
flake = {
# The usual flake attributes can be defined here, including system-
# agnostic ones like nixosModule and system-enumerating ones, although
# those are more easily expressed in perSystem.
};
};
perSystem = {
config,
self',
inputs',
pkgs,
system,
...
}: {
# Per-system attributes can be defined here. The self' and inputs'
# module parameters provide easy access to attributes of the same
# system.
# Equivalent to inputs'.nixpkgs.legacyPackages.hello;
# packages.default = pkgs.hello;
devShells.default = let
# https://downloads.haskell.org/~ghc/9.10.3/docs/users_guide/index.html
ghc' = pkgs.haskell.packages.ghc9103.ghcWithHoogle (
self:
with self; [
relude
split
aeson
random
neat-interpolation
# maths
primes
arithmoi
parallel
# graphing libraries!
Chart
Chart-cairo
]
);
clean = pkgs.writeShellScriptBin "clean" ''
# Delete executables
find . -type f -executable -not -path '*/.git/*' -delete
# Delete all Haskell IR files
find . -type f -name '*.hi' -delete
find . -type f -name '*.o' -delete
# Delete any test graphs created
find . -type f -name '*.png' -delete
'';
in
pkgs.mkShell {
packages = with pkgs; [
cowsay
alejandra
ghc'
clean
haskell-language-server
ghcid
hlint
kotlin
];
shellHook = ''
${config.pre-commit.installationScript}
echo ".------..------..------..------..------..------..------..------..------..------."
echo "|U.--. ||N.--. ||I.--. ||V.--. ||E.--. ||R.--. ||S.--. ||A.--. ||L.--. ||=.--. |"
echo "| (\/) || :(): || (\/) || :(): || (\/) || :(): || :/\: || (\/) || :/\: || (\/) |"
echo "| :\/: || ()() || :\/: || ()() || :\/: || ()() || :\/: || :\/: || (__) || :\/: |"
echo "| '--'U|| '--'N|| '--'I|| '--'V|| '--'E|| '--'R|| '--'S|| '--'A|| '--'L|| '--'=|"
echo "[------'[------'[------'[------'[------'[------'[------'[------'[------'[------'"
echo ".------..------..------..------..------..------..------..------..------..------."
echo "|C.--. ||A.--. ||L.--. ||C.--. ||U.--. ||L.--. ||A.--. ||T.--. ||O.--. ||R.--. |"
echo "| :/\: || (\/) || :/\: || :/\: || (\/) || :/\: || (\/) || :/\: || :/\: || :(): |"
echo "| :\/: || :\/: || (__) || :\/: || :\/: || (__) || :\/: || (__) || :\/: || ()() |"
echo "| '--'C|| '--'A|| '--'L|| '--'C|| '--'U|| '--'L|| '--'A|| '--'T|| '--'O|| '--'R|"
echo "[------'[------'[------'[------'[------'[------'[------'[------'[------'[------'"
echo " _ _ _ _ "
echo "| | | | (_) | | ______ "
echo "| | | | _ __ _ __ __ ___ _ __ ___ __ _ | ||______|"
echo "| | | || '_ \ | |\ \ / / / _ \| '__|/ __| / _[ || | ______ "
echo "| |_| || | | || | \ V / | __/| | \__ \| (_| || ||______|"
echo " \___/ |_| |_||_| \_/ \___||_| |___/ \__,_||_| "
echo " "
echo " "
echo " _____ _ _ _ "
echo "/ __ \ | | | | | | "
echo "| / \/ __ _ | | ___ _ _ | | __ _ | |_ ___ _ __ "
echo "| | / _[ || | / __|| | | || | / _[ || __| / _ \ | '__| "
echo "| \__/\| (_| || || (__ | |_| || || (_| || |_ | (_) || | "
echo " \____/ \__,_||_| \___| \__,_||_| \__,_| \__| \___/ |_| "
'';
};
pre-commit = {
settings.hooks = {
alejandra.enable = true;
mdformat.enable = true;
prettier.enable = true;
prettier.excludes = [".*\.md" "flake.lock"];
stylish-haskell.enable = true;
};
};
treefmt = {
programs = {
alejandra.enable = true;
mdformat.enable = true;
prettier.enable = true;
prettier.excludes = ["*.md"];
stylish-haskell.enable = true;
};
};
};
};
}

View File

@@ -3,41 +3,61 @@ The Simplest Math Problem No One Can Solve - Collatz Conjecture
https://youtu.be/094y1Z2wpJg
-}
import Control.Parallel.Strategies (parMap, rdeepseq, rpar)
import Data.Set (fromList)
import Control.Parallel.Strategies
import Data.Containers.ListUtils
import Debug.Trace (trace)
main :: IO ()
main = do
let results =
parMap rdeepseq f [10^100_000..10^100_000+100] :: [Integer]
print (fromList results)
-- main = print $ fromList $ dxs
-- main = print $ fromList $ take 300 $ map f [2^100_000..]
-- fromList [100001,717859]
let start = 2^1_000_000
let end = 100
-- let message = "Starting at " <> show start <> ", and computing " <> show end <> " values."
let message = "Starting at 2^1_000_000, and computing " <> show end <> " values."
let results = parMap rpar f [start..start+end] :: [Integer]
putStrLn "======================================"
putStrLn message
print $ nubOrd results
putStrLn "======================================"
-- main = print $ fromList $ take 3 $ map f [2^310997..]
-- main = print $ f $ 2^310997 + 2
{-
Idea for next steps
Don't compute numbers if even, we know they'll go to 1
(https://smunix.github.io/dev.stephendiehl.com/hask/tutorial.pdf - Scientific numbers)
Sample a few numbers at a given order of magnitude, then increase the exponent and sample more numbers
eg. [-10..n..+10], where n = 2^1_000, then n = 2^1_001, then n = 2^2_000, etc.
-}
{-
Starting at 2^1_000, and computing 100 values.
- [1001,7249,7430]
Starting at 2^10_000, and computing 100 values.
- [10001,72379]
Starting at 2^100_000, and computing 100 values.
- [100001,717859]
Starting at 2^1_000_000, and computing 100 values.
- [1000001,7212801]
-}
lst :: [Integer]
lst = take 300 [2^100_000..]
dxs :: [Integer]
dxs = parMap rpar f lst
f :: Integer -> Integer
f n = s 1 n
where
s :: Integer -> Integer -> Integer
s i n
| n == 1 = i
| n == 0 = i
| n == (-1) = i
| n == (-5) = i
| n == (-17) = i
| even n = s (succ i) (n `div` 2)
| odd n = s (succ i) (3*n + 1)
s c n
| n == 1 = c
| n == 0 = c
| n == (-1) = c
| n == (-5) = c
| n == (-17) = c
| even n = s c' (n `div` 2)
| odd n = s c' (3*n + 1)
where c' = succ c
f' :: Integer -> Integer
f' n = f'' 1 n
@@ -70,3 +90,5 @@ cc n = cc' [] n
| even n = cc' acc' (n `div` 2)
| odd n = cc' acc' (3*n + 1)
where acc' = acc <> [n]
x = map (length . cc) [2^1_000+10..]

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,81 @@
= Collatz Chains in Haskell
== Title header
This is a literate haskell blog post. You can load and run this code!
The Simplest Math Problem No One Can Solve - Collatz Conjecture
<https://youtu.be/094y1Z2wpJg>
Why do we want to do this?
> collatzStep :: Integer -> Integer
> collatzStep n
> | even n = n `div` 2
> | odd n = 3 * n + 1
With this, we can iterate over it.
> collatzStep' :: Integer -> Integer
> collatzStep' n
> | n == 1 = error "done"
> | even n = n `div` 2
> | odd n = 3 * n + 1
> result :: [Integer]
> result = iterate collatzStep' 5
collatz collect
generate the collatz sequence and return it
> cc :: Integer -> [Integer]
> cc n = cc' [] n
> where
> cc' :: [Integer] -> Integer -> [Integer]
> cc' acc n
> | n == 1 = acc <> [1]
> | n == 0 = acc <> [0]
> | n == (-1) = acc <> [-1]
> | n == (-5) = acc <> [-5]
> | n == (-17) = acc <> [-17]
> | even n = cc' acc' (n `div` 2)
> | odd n = cc' acc' (3*n + 1)
> where acc' = acc <> [n]
> a :: [Integer]
> a = reverse $ cc $ 2^1_000+1
> b :: [Integer]
> b = reverse $ cc $ 2^1_000+2
> comparingChains :: [Bool]
> comparingChains = zipWith (==) a b
Now if we collect the chain lengths of large numbers we see something slightly horrifying:
> x = map (length . cc) [2^1_000+10..]
This prints us the list of lengths of the chain as:
[7249,7249,7249,7249,7249,7249,7249,7249,7249,7249,7249,7249,7249,7249,7249,7249,7249,7249,7249,7249,7249,7430,7249,7249,7249,7249,7249,7249,7249,7249,7249,7430,7249,7249,7249,7249,7249,7249,7249,7249,7249,7249,7249,7249,7430,7430,7249,7249,7249,7249,7249,7249,7249,7249,7249,7249,7249,7249,7249,7249,7249,7249,7249,7430,7249,7249,7249,7249,7249,7249,7249,7249,7249,7249,7249,7249,7249,7249,7249,7249,7249,7249,7249,7249,7249,7249,7249,7430,7249,7249,7249,7249,7249,7249,7249,7249,7249,7249,7249,7249,7249,7249,7249,7249,7249,7249,7249,7249,7249,7249,7249,7249,7249,7249,7249,7249,7249,7249,7249,7430,7249,7249,7249,7249,7249,7249,7249,7249,7249,7249,7249,7249,7249,7249,7249,7249,7249,7249,7249,7249,7249,7249,7249,7249,7249,7249,7249,7249...]
λ> cc 13
[13,40,20,10,5,16,8,4,2,1]
λ> cc 12
[12,6,3,10,5,16,8,4,2,1]
Now this makes sense with small numbers.
But I find it weird with large numbers.
> f :: Integer -> Integer
> f n = s 1 n
> where
> s :: Integer -> Integer -> Integer
> s i n
> | n == 1 = i
> | n == 0 = i
> | n == (-1) = i
> | n == (-5) = i
> | n == (-17) = i
> | even n = s (succ i) (n `div` 2)
> | odd n = s (succ i) (3 * n + 1)

View File

@@ -1,13 +1,14 @@
-- https://adventofcode.com/2024/day/1
import Data.List
import GHC.IO
import Data.List (elemIndices, sort)
import GHC.IO (unsafePerformIO)
input :: FilePath
input = "src/advent_of_code/2024/1.input"
entries :: ([Int], [Int])
entries = unzip $ map parse' $ unsafePerformIO $ lines <$> readFile input
-- entries :: ([Int], [Int])
-- entries = unzip $ map parse' $ unsafePerformIO $ lines <$> readFile input
entries = unsafePerformIO $ lines <$> readFile input
main :: IO ()
main = do
@@ -20,12 +21,10 @@ main = do
parse' :: String -> (Int, Int)
parse' str = (read l, read r)
where
l = take' str
r = reverse $ take' $ reverse str
take' = takeWhile (/= ' ')
[l, r] = words str
solveP1 :: ([Int], [Int]) -> Int
solveP1 (as, bs) = sum $ zipWith (\i j -> abs (i - j)) as' bs'
solveP1 (as, bs) = sum $ map abs $ zipWith subtract as' bs'
where
as' = sort as
bs' = sort bs

View File

@@ -0,0 +1,51 @@
-- https://adventofcode.com/2024/day/2
{-# LANGUAGE OverloadedStrings #-}
import Data.List (all, drop, elem, filter, length, map, or, sort,
sortOn, tail, take, zipWith)
import Data.Ord (Down (Down), Ord ((<=), (>=)))
import Data.Text (Text, lines, pack, splitOn, unpack)
import Data.Text.IO (readFile)
import GHC.IO (unsafePerformIO)
import Prelude hiding (lines, readFile)
entries :: [[Int]]
entries = parse . unsafePerformIO $ lines <$> readFile "src/advent_of_code/2024/2.input"
entries' :: [[Int]]
entries' = parse . unsafePerformIO $ lines <$> readFile "src/advent_of_code/2024/2f.input"
parse :: [Text] -> [[Int]]
parse = map (map (read . unpack) . splitOn " ")
main :: IO ()
main = do
entries <- parse . lines <$> readFile "src/advent_of_code/2024/2.input"
print "Advent of Code 2024 - Day 2"
print $ "Part 1: " <> show (solveP1 entries)
print $ "Part 2: " <> show (solveP2 entries)
solveP1 :: [[Int]] -> Int
solveP1 = length . filter (== True) . map safeReportP1
solveP2 :: [[Int]] -> Int
solveP2 = length . filter (== True) . map safeReportP2
safeReportP1 :: [Int] -> Bool
safeReportP1 report = isSorted report && adjacentLevelCheck report
isSorted :: [Int] -> Bool
isSorted report = report `elem` [sort report, sortOn Down report]
adjacentLevelCheck :: [Int] -> Bool
adjacentLevelCheck report =
all (((== True) . (\n-> n>=1 && n<=3)) . abs)
(zipWith subtract report (tail report))
safeReportP2 :: [Int] -> Bool
safeReportP2 report = any safeReportP1 (removeOneBadReport report)
removeOneBadReport :: [Int] -> [[Int]]
removeOneBadReport lst = map (\i -> take i lst <> drop (succ i) lst) [0..length lst-1]

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,6 @@
7 6 4 2 1
1 2 7 8 9
9 7 6 2 1
1 3 2 4 5
8 6 4 4 1
1 3 6 7 9