Skip to content

Instantly share code, notes, and snippets.

@paulcc
paulcc / gist:5870420
Last active December 19, 2015 00:39
Bits of the word pack code in Idris
placeholder... until a few more days into July
-- using Haskell's library for rational numbers, which stores a rational as
-- a pair of integer numbers (not limited to 32 or 64 bits); hence able to do
-- exact arithmetic etc up to high numbers
import Data.Ratio
-- the core function: spits out a list of ascending n for the fractions
-- works by reducing (a/b) to 1/ceil(b/a) + ...
fractionize t
| t == 0 = []
| diff >= 0 = next : fractionize diff
import Data.List(nub, sortBy, groupBy)
import Data.Char(isAlpha, toLower)
isograms s
= [ (length w, w)
| w <- nub $ words $ map toLower s
, all isAlpha w
, nub w == w ]
main = do text <- readFile "pg11.txt"
@paulcc
paulcc / gist:4577526
Last active December 11, 2015 09:08
Placeholder
import Data.List (elemIndex)
tests :: (Int -> String -> String) -> [Bool]
tests wrap
= [ wrap 1 "" == ""
, wrap 1 "x" == "x"
, wrap 1 "xx" == "x\nx"
, wrap 1 "xxx" == "x\nx\nx"
, wrap 1 "x x" == "x\nx"
, wrap 2 "x x" == "x\nx"
-- see below for commentary
makeUpTo :: Int -> [String] -> [String]
makeUpTo n = mlu []
where
mlu pre [] = [pre]
mlu [] (w:ws) = mlu w ws
mlu pre (w:ws) | length pre + 1 + length w <= n = mlu (pre ++ ' ' : w) ws
| otherwise = pre : mlu [] (w:ws)
@paulcc
paulcc / Files.hs
Created November 27, 2012 23:02
Async JS via Fay (Haskell) - see the December PragPub Magazine article
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE NoMonomorphismRestriction #-}
-- Example for December 2012 Prag Pub Magazine
-- expects Fay 0.10
--
-- Fay doesn't support type classes yet (hopefully soon) so this code contains
-- various workarounds and so is a bit more complex than it should be!
@paulcc
paulcc / gist:3799331
Created September 28, 2012 11:43
list of four-letter words to go with https://gist.github.com/3733182
aahs
abbe
abbr
abed
abet
able
ably
abut
acct
aced
@paulcc
paulcc / gist:3733182
Created September 16, 2012 16:51
Code for the Word Chain kata, see the October edition of PragPub magazine
-- word list is at https://gist.github.com/3799331
import Data.Set(Set, fromDistinctAscList, member)
import System.IO.Unsafe(unsafePerformIO)
import Data.Tree
import Data.List(inits,tails)
--------------------
-- first step - generating valid next words
@paulcc
paulcc / group_on-full-example.hs
Created September 5, 2012 17:37
Example to accompany my types in haskell article
-- compiles with ghc(i) 7.4.1
import Data.Map (Map, empty, insertWith, toList)
import Data.List(groupBy, group, sortBy, sort)
{- To recap from the main article, we want to define "group_on" via "groupBy", so that its
type is something like (b -> b -> Bool) -> (a -> b) -> [a] -> [(b, [a])]
The method here is to work with a concrete example and gradually massage it
into the form we want - using the REPL