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
| {-# 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
| 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
| -- 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
| 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
| 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
| 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
| 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
| 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
| data TakeState a = TakeState Int a deriving (Show) | |
| take' n = | |
| let f (TakeState i acc) head = if i==n then (TakeState i acc) else (TakeState (i+1) (acc++[head])) | |
| in foldl f (TakeState 0 []) |