Monthly Archives: September 2012

Haskell : finding substring with char index

import Data.List wordFinderHelper reverseword content = foldl step (0,[],1,””) content where step (x,ys,i,matcher) c | c:matcher == reverseword = ((x+1),i-length reverseword:ys,i+1,””) | (c:matcher) `isSuffixOf` reverseword = (x,ys,i+1,c:matcher) | otherwise = (x,ys,i+1,””) wordFinder word content = (\(x,y,_,_)-> (x,reverse y))$wordFinderHelper (reverse word) … Continue reading

Posted in Uncategorized | Leave a comment

Haskell: Finding no of occurrence of words with string index

wordFilnderHelper word content = foldl step (0,[],0) $ words content where step (x,y,z) w | word == w = ((x+1),z:y,z+1) | otherwise = (x,y,z+1) wordFinder word content = (\(x,y,z)->(x,reverse y))$wordFilnderHelper word content –sample run wordFinder “hello” “hello this is tough … Continue reading

Posted in Uncategorized | Leave a comment

Haskell implementation of MergeSort

Merge sort explanation http://en.wikipedia.org/wiki/Merge_sort mergeSort xs = merge (split xs) [] where split [] = [] split (x:xs) = [x]:split xs merge [] [] = [] merge (x:x1:xs) ys = merge xs ((sort x x1):ys) merge [x] ys = merge … Continue reading

Posted in Uncategorized | Leave a comment

Haskell Lazy Infinite Fibonacci

fib = lazyFib 1 1lazyFib x y = x:lazyFib y (x+y)fibNth n = head$drop (n-1)$fib –sampe run  take 10 fib [1,1,2,3,5,8,13,21,34,55] fibNth 100354224848179261915075

Posted in Uncategorized | Tagged | Leave a comment

Haskell Infinite calculation :Pascal’s_triangle

–For details of Pascal’s_triangle go through : http://en.wikipedia.org/wiki/Pascal’s_triangle — Haskell is good at lazy evaluation ,Using that here is infinite Pascal’s_triangle pascalTriangle = lazyPascaltriangle [1] lazyPascaltriangle xs = xs : lazyPascaltriangle((head xs):pairSum xs) where pairSum [] = [] pairSum [x] … Continue reading

Posted in Uncategorized | Leave a comment

Haskell Fun : reverse a list

myreverse xs = reverseHelper xs [] where reverseHelper (x:xs) ys = reverseHelper xs (x:ys) reverseHelper [] ys = ys –sample myreverse [1,2,3] [3,2,1] myreverse “hello” “olleh” another way with (++) infixr reverse x:xs = reverse xs ++ [x] reverse [x] … Continue reading

Posted in Uncategorized | Leave a comment

Robustness of Haskell :Mirroring a Binary tree

— tree structure PTree a = Pleaf a        | PNode (PTree a) (PTree a) –mirror mirror (Pleaf a) = Pleaf a mirror (PNode n1 n2) = PNode(mirror n1) (mirror n2)

Posted in Uncategorized | Leave a comment