Skip to content

Instantly share code, notes, and snippets.

@wavebeem
Created March 3, 2011 05:05
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 wavebeem/852358 to your computer and use it in GitHub Desktop.
Save wavebeem/852358 to your computer and use it in GitHub Desktop.
-------------------- Brian Mock -----------------------
module Lab3 where
import List
-------------------- Problem 1 -----------------------
(==>) = flip ($)
integerToDigit (i + 1) = digits !! i
digitToInteger c = i + 1 where
(Just i) = findIndex (== c) digits
numeralToInteger base cs = n2i base 0 cs where
n2i base s (c:cs) = n2i base (base * s + (digitToInteger c)) cs
n2i base s [ ] = s
integerToNumeral base 0 = ""
integerToNumeral base n =
integerToNumeral base d
++ [integerToDigit m] where
(d, m) = n `wackyDivMod` base
wackyDivMod a b
| m == 0 && a >= b = (n - 1, b)
| otherwise = (n, m)
where (n, m) = a `divMod` b
digits = ['1' .. '9'] ++ ['a' .. 'g']
test = do
putStrLn (joinWith sep heads)
printSepLine
map f xs
==> unlines
==> putStrLn where
funcs = [show, bin, dec, hex]
heads = words "dec ibin idec ihex" ==> map pad
pad = rPad 10
sep = " | "
nums = 80
xs = [0 .. nums]
f x = map ($ x) [show, bin, dec, hex]
==> map pad
==> joinWith sep
rPad width xs = get nel " " ++ xs where
get n xs = cycle xs ==> take n
nel = width - len
len = length xs
printSepLine = putStrLn (cycle pattern ==> take width) where
pattern = "-"
width = 60
joinWith _ [ ] = ""
joinWith _ [x ] = x
joinWith sep (x:xs) = x ++ sep ++ joinWith sep xs
dec = integerToNumeral 10
decI = numeralToInteger 10
hex = integerToNumeral 16
hexI = numeralToInteger 16
bin = integerToNumeral 2
binI = numeralToInteger 2
-------------------- Problem 2 -----------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment