haskell on (map solve problems)
http://yantonov.com/categories/haskell/
Recent content in haskell on (map solve problems)Hugo -- gohugo.ioen-usSat, 21 Feb 2015 17:44:29 +0500Path to learn haskell
http://yantonov.com/blog/2015/02/21/path-to-learn-haskell/
Sat, 21 Feb 2015 17:44:29 +0500http://yantonov.com/blog/2015/02/21/path-to-learn-haskell/Some haskell resources recommended by @bytemyapp.
CIS 194: Introduction to Haskell (Spring 2013) NICTA course Stanford CS240h Spring 2014 Path to learning haskell Find the length of the loop
http://yantonov.com/blog/2015/02/21/find-the-length-of-the-loop/
Sat, 21 Feb 2015 16:06:23 +0500http://yantonov.com/blog/2015/02/21/find-the-length-of-the-loop/One of the popular interivew tasks…
You are given a head of a linked list. Its known list contains cycle. The goal is to determine the length of the loop.
Here is straghtforward solution in haskell
{- data Node a instance Eq a => Eq (Node a) next :: Node a -> Node a -} data Phase = TryFindLoop | FindLength deriving (Eq) loopSize :: Eq a => Node a -> Int loopSize a = slowAndFast a a 0 TryFindLoop where slowAndFast :: (Eq node) => Node node -> Node node -> Int -> Phase -> Int slowAndFast slow fast len phase = let nextSlow = next slow nextPreFast = next fast nextFast = next nextPreFast in if (slow == nextPreFast || slow == nextFast) then if (phase == FindLength) then if (nextPreFast == nextFast) then 1 else 2*(len+1) - len - (if slow == nextFast then 0 else 1) else slowAndFast nextSlow nextSlow 0 FindLength else slowAndFast nextSlow nextFast (len + 1) phase Complexity: time o(n), memory o(1)Church numerals
http://yantonov.com/blog/2014/11/13/church-numerals/
Thu, 13 Nov 2014 08:17:25 +0500http://yantonov.com/blog/2014/11/13/church-numerals/Church numerals is a great example of introducing abstraction in terms of composition of functions. This concept demonstrates that “data” (naturals numbers in particular) and operations on data can be defined in the same way - using higher-order functions.
Summary:
type Church a = (a -> a) -> a -> a zero, cone, ctwo :: Church a zero s z = z one s z = s z two s = s .