This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
zipWith' :: (a -> b -> c) -> [a] -> [b] -> [c] | |
zipWith' _ [] _ = [] | |
zipWith' _ _ [] = [] | |
zipWith' f (a:as) (b:bs) = (f a b):(zipWith' f as bs) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) [] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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." |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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 |