Friday, June 14, 2019

Function as Functor in Haskell programming language

I was really confused with the Function as Functor and tried to understand what (->) r really means.

Already many explanations about this but still I could find out what the "fixing" of input type "r" in "(->) r" really means.

     It is really similar to the approach to (Either a) b, where the first type "a" is fixed.
     And we can see that :k Either is same as :k (->), both of them are "kind" of *-> * -> * .
     So we have to fix them to have the signature like *-> *, e.g., (->) Int or Either Int has.

     But what does the fix of type mean?
     To me, if we say fix the input parameter type of the function, e.g., "(-) r", it means we don't need to bother to handle mapping over "r",
     but only focus on mapping over of the mapping over of type of result, "a" in "(->) r a", which in turn means we have to map it to "(->) r b".
     You can see "r" is the same , (meaning fixed), above, but the result type changes from "a" to "b".

I will be really happy if I can help somebody to have a deeper understanding, though I am not using academic terms in Haskell.

Saturday, March 23, 2019

a haskell implementation of Sieve of Eratosthenes

As a novice of Haskell, tried to implemented a piece of code of Sieve of Eratosthenes.
Quite redundant now, maybe.

prim' :: (Integral a) => [a] -> [a]
prim' []= []
prim' (x:xs) = x : (prim' $ foldl (\acc z -> if (z `mod` x) == 0 then acc else acc ++ [z]) [] xs)

Here is the testing result of it.

> prim' [2..100]
[2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97]