Skip to content

Instantly share code, notes, and snippets.

@dtchepak
dtchepak / zip.hs
Created February 29, 2012 12:27
Trying to implement zip using foldr
zip' :: [a] -> [b] -> [(a,b)]
zip' x y = foldr (\h a ->
foldr (\h2 _ -> ((h,h2):a)) [] y
) [] x
{-
*Main> zip' [] []
[]
*Main> zip' [1] [2]
[(1,2)]
@dtchepak
dtchepak / zipwith.hs
Created March 1, 2012 01:47
recursive zipwith
zipWith' :: (a -> b -> c) -> [a] -> [b] -> [c]
zipWith' _ [] _ = []
zipWith' _ _ [] = []
zipWith' f (a:as) (b:bs) = (f a b):(zipWith' f as bs)
@dtchepak
dtchepak / fold2r.hs
Created March 5, 2012 09:07
folding over two lists
fold2r :: (a -> b -> c -> c) -> c -> [a] -> [b] -> c
fold2r f seed (a:as) (b:bs) = f a b (fold2r f seed as bs)
fold2r _ seed _ _ = seed
zipWithFold2r f = fold2r (\a b c -> (f a b):c) []
@dtchepak
dtchepak / Functors.cs
Created April 30, 2012 13:12
Attempt at C# Functors
public static class Functors
{
public static IEnumerable<B> fmap<A,B>(this IEnumerable<A> functor, Func<A, B> f)
{
return functor.Select(f);
}
public static B? fmap<A, B>(this A? functor, Func<A, B> f) where A : struct where B : struct
{
return functor == null ? null : (B?) f(functor.Value);
@dtchepak
dtchepak / splitWhere.hs
Created May 11, 2012 08:58
Attempt at splitting a single list into segments whenever a predicate is matched
splitWhere :: [a] -> (a -> Bool) -> [[a]]
splitWhere xs p = combine [] rest lists
where
(lists, rest) = foldr (\x (l,r) -> if p x then (combine [x] r l,[]) else (l,x:r)) ([],[]) xs
combine [] [] c = c
combine [] b c = b:c
combine a [] c = a:c
combine a b c = a:b:c
-- Implementations below based on comments from #haskell IRC
@dtchepak
dtchepak / cltd-typeclasses.hs
Created May 17, 2012 04:29
My attempt at solutions to data type and typeclasses sample probs
-- Problem 1
data NestedList a = Elem a | List [NestedList a] deriving (Show,Eq)
flatten :: NestedList a -> [a]
flatten (Elem a) = [a]
flatten (List []) = []
flatten (List (h:t)) = flatten h ++ flatten (List t)
-- Problem 2
@dtchepak
dtchepak / prob11_runlength.hs
Created May 22, 2012 03:03
Attempt at prob 11 from 99 Haskell probs
import Data.List (group)
data RunLength a = Single a | Multiple Int a deriving (Show, Eq)
encodeModified :: Eq a => [a] -> [RunLength a]
encodeModified = map runLength . group
where runLength [x] = Single x
runLength xs@(x:_) = Multiple (length xs) x
@dtchepak
dtchepak / test.hs
Created June 4, 2012 00:27
Illegal instance declaration, flexible instances?
{-# LANGUAGE MultiParamTypeClasses #-}
class Collection c a where
insert :: a -> c a -> c a
instance Collection [] a where
insert a b = (a:b)
{-
Illegal instance declaration for `Collection [] a'
(All instance types must be of the form (T a1 ... an)
@dtchepak
dtchepak / ioexample.hs
Created June 15, 2012 07:00
IO example from Haskell.org IO tutorial (http://www.haskell.org/tutorial/io.html)
-- IO example from http://www.haskell.org/tutorial/io.html
-- copies one file to another:
main = do fromHandle <- getAndOpenFile "Copy from: " ReadMode
toHandle <- getAndOpenFile "Copy to: " WriteMode
contents <- hGetContents fromHandle
hPutStr toHandle contents
hClose toHandle
putStr "Done."
@dtchepak
dtchepak / gist:3014681
Created June 28, 2012 23:22
[SOLVED] NSub error on mono when built with vs2012 tools
-- Sorted, see https://gist.github.com/3014681#gistcomment-360816
~/dev/git/mononsub % gmcs -sdk:4 Program.cs -reference:NSubstitute.dll
Missing method .ctor in assembly /Users/dave/dev/git/mononsub/NSubstitute.dll, type System.Runtime.Versioning.TargetFrameworkAttribute
Can't find custom attr constructor image: /Users/dave/dev/git/mononsub/NSubstitute.dll mtoken: 0x0a00085a
~/dev/git/mononsub % dmcs Program.cs -reference:newsub40/NSubstitute.dll
Missing method .ctor in assembly /Users/dtchepak/dev/git/mononsub/newsub40/NSubstitute.dll, type System.Runtime.CompilerServices.ExtensionAttribute
Can't find custom attr constructor image: /Users/dtchepak/dev/git/mononsub/newsub40/NSubstitute.dll mtoken: 0x0a000850