Skip to content

Instantly share code, notes, and snippets.

@qoelet
qoelet / caesar_cipher.hs
Created August 3, 2013 07:43
Full code of Caesar's cipher exercise from Hutton's book
import Data.Char
-- frequencies based on large number of text
table :: [Float]
table = [8.2,1.5,2.8,4.3,12.7,2.2,2.0,6.1,7.0,0.2,0.8,4.0,2.4,6.7,7.5,1.9,0.1,6.0,6.3,9.1,2.8,1.0,2.4,0.2,2.0,0.1]
-- earlier functions in the chapter required here
lowers :: String -> Int
lowers xs = length [ x | x <- xs, isLower x]
@qoelet
qoelet / replication.hs
Last active December 20, 2015 14:19
birds of similar feathers
-- standard library version
replicate :: Int -> a -> [a]
replicate n x = take n (repeat x)
-- my version (having to use list comprehension)
replicate' :: Int -> a -> [a]
replicate' n x = [x | xn <- [1..n]]
-- λ <name> <expression>
-- lambda forms for myLast and myInit, tested with example list [1,2,3,4]
(\x -> head (reverse x))[1,2,3,4]
(\x -> take (length x - 1) x) [1,2,3,4]
@qoelet
qoelet / Fib.hs
Created August 1, 2013 04:20
Fibonacci task
-- file: Fib.hs
-- Task: Generate first n Fibonacci numbers, then sum it
-- fib: given n for the index of the Fibonacci series, return the corresponding value, e.g. fib 9 gives 34
fib :: Int -> Int
fib 0 = 0
fib 1 = 1
fib n = fib (n-1) + fib (n-2)
@qoelet
qoelet / myInit.hs
Created July 31, 2013 03:13
two definitions of 'init'
-- file: myInit.hs
-- 2 ways to define init function (removes last element from a non empty list)
myInitOne :: [a] -> [a]
myInitOne [x] = []
myInitOne (x:xs) = [x] ++ myInitOne xs
myInitTwo :: [a] -> [a]
@qoelet
qoelet / myLast.hs
Created July 31, 2013 02:57
Implementing last function
-- file: myLast.hs
myLast :: [a] -> a
myLast x = head (reverse x)
@qoelet
qoelet / Clean.hs
Created July 30, 2013 09:06
null character cleanup
-- file: Clean.hs
-- removes null characters
import System.Environment (getArgs)
removeNull :: [Char] -> [Char]
removeNull [] = []
removeNull (x:xs)
| x == '\000' = [] ++ removeNull xs
| otherwise = [x] ++ removeNull xs
@qoelet
qoelet / Clean.hs
Created July 30, 2013 09:05
null character cleanup
-- file: Clean.hs
-- removes null characters
import System.Environment (getArgs)
removeNull :: [Char] -> [Char]
removeNull [] = []
removeNull (x:xs)
| x == '\000' = [] ++ removeNull xs
| otherwise = [x] ++ removeNull xs
Sort (cost=3357.35..3380.23 rows=9150 width=809) (actual time=147.963..159.258 rows=9429 loops=1)
Sort Key: news_article.date_is_published
Sort Method: quicksort Memory: 9491kB
-> Hash Join (cost=828.56..2755.30 rows=9150 width=809) (actual time=38.766..107.775 rows=9429 loops=1)
# additional details etc etc.
[Thu Jun 14 16:57:24 2012] [error] [client 127.0.0.1] mod_wsgi (pid=10827): Exception occurred processing WSGI script '/home/kenny/someapp/wsgi.py'.
[Thu Jun 14 16:57:24 2012] [error] [client 127.0.0.1] Traceback (most recent call last):
[Thu Jun 14 16:57:24 2012] [error] [client 127.0.0.1] File "/usr/lib/pymodules/python2.6/django/core/handlers/wsgi.py", line 230, in __call__
[Thu Jun 14 16:57:24 2012] [error] [client 127.0.0.1] self.load_middleware()
[Thu Jun 14 16:57:24 2012] [error] [client 127.0.0.1] File "/usr/lib/pymodules/python2.6/django/core/handlers/base.py", line 42, in load_middleware
[Thu Jun 14 16:57:24