Hi, almost done with Chapter 4 of the book
Here’s in the repo, will be completing tomorrow last exercises and write some test with runTest /suite.
Difference betrween apply and map
map :: forall a b. (a -> b) -> f a -> f b
map can be used to turn functions a -> b into functions f a -> f b whose argument and return types use the type constructor
So if I understand correctly,
maptakes a function
(a -> b), a
functor f a(a type constructor, like
Maybe) and applies the function and returns a new
functor f b?
> map (\n -> n + 3) (Just 5) (Just 8) > (\n -> n + 3) <$> Just 5 (Just 8)
apply :: forall a b. f (a -> b) -> f a -> f b
Apply can be used to lift functions of two or more arguments to work on values wrapped with the type constructor
So here I apply a function wrapped in a context (e.g.
Maybefunctor/type constructor) to a value or values wrapped with the same type constructor ?
> apply (Just (\n -> n + 3)) (Just 5) (Just 8) > Just (\n -> n + 3) <*> Just 5 (Just 8)
Does this means
Apply is an
Applicative? Going by the definition of applying a function wrapped in a context to a value wrapped in a context?
How do I write these
> (*) <$> Just 5 <*> Just 3 (Just 15) > lift2 (*) (Just 5) (Just 3) (Just 15)
Can anyone make some example of when this is used in practice?
pure support lifting opoperations for any number of function arguments? Or is it other functor(s) part of
Applicative typeclass? Any examples?
More generally: for a
functor to be part of the
Applicative class, it means it needs to have an operation that lift functions of two or more arguments in it (plus the additional laws as per docs)?
Array a of MonadZero typeclass?
I am not clear what happens here in the book:
> import Control.MonadZero > :type guard forall m. MonadZero m => Boolean -> m Unit
we can assume that PSCi reported the following type:
Boolean -> Array Unit
Does m stand for monad, so Array is a monad?
Why both these are valid, I thought
<> was `String¬ concatenation only
> [1, 2, 3] <>  [1,2,3,1] > snoc [1, 2, 3] 1 [1,2,3,1]
Wouldn’t it be great to have additional concrete examples of functions usage in the documentation?
Some have, some don’t – I think it would be great to enrich the docs. For example this is immediate:
cons cons :: forall a. a -> Array a -> Array a Attaches an element to the front of an array, creating a new array. cons 1 [2, 3, 4] = [1, 2, 3, 4] -- Note, the running time of this function is O(n).
I find some naming quite obscure fro someone with little knowledge of Lisp / new to the language etc., e.g. unless you know what
snoc means a priori, is hard to find a function in Pursuit to simply:
Append an element to the end of an array, creating a new array.
Is there any way this could be improved, perhaps just tags in search?