basic use of Applicative

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

basic use of Applicative

Imants Cekusins
module BasicApplicative where


-- 2 arg
upd2:: Int -> Int -> Int
upd2 x1 x2 = x1 * x2

-- 3 arg
upd3:: Int -> Int -> Int -> Int
upd3 x1 x2 x3 = x1 * x2 + x3


-- pure
pureMaybe::Int -> Maybe Int
pureMaybe = pure

pureList::Int -> [Int]
pureList = pure


-- maybe
maybe2::Maybe Int -> Maybe Int -> Maybe Int
maybe2 mi1 mi2 = upd2 <$> mi1 <*> mi2


-- list
list2::[Int] -> [Int] -> [Int]
list2 l1 l2 = upd2 <$> l1 <*> l2

-- same result as list2
list2p::[Int] -> [Int] -> [Int]
list2p l1 l2 = pure upd2 <*> l1 <*> l2


list3::[Int] -> [Int] -> [Int] -> [Int]
list3 l1 l2 l3 = upd3 <$> l1 <*> l2 <*> l3
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
KC
Reply | Threaded
Open this post in threaded view
|

Re: basic use of Applicative

KC

Words?
Sentences?

--
--

Sent from an expensive device which will be obsolete in a few months! :D

Casey
   

On Jun 25, 2015 3:48 AM, "Imants Cekusins" <[hidden email]> wrote:
module BasicApplicative where


-- 2 arg
upd2:: Int -> Int -> Int
upd2 x1 x2 = x1 * x2

-- 3 arg
upd3:: Int -> Int -> Int -> Int
upd3 x1 x2 x3 = x1 * x2 + x3


-- pure
pureMaybe::Int -> Maybe Int
pureMaybe = pure

pureList::Int -> [Int]
pureList = pure


-- maybe
maybe2::Maybe Int -> Maybe Int -> Maybe Int
maybe2 mi1 mi2 = upd2 <$> mi1 <*> mi2


-- list
list2::[Int] -> [Int] -> [Int]
list2 l1 l2 = upd2 <$> l1 <*> l2

-- same result as list2
list2p::[Int] -> [Int] -> [Int]
list2p l1 l2 = pure upd2 <*> l1 <*> l2


list3::[Int] -> [Int] -> [Int] -> [Int]
list3 l1 l2 l3 = upd3 <$> l1 <*> l2 <*> l3
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: basic use of Applicative

Imants Cekusins
> Words?
> Sentences?

You see, these are only short snippets on purpose. There are many
books and tutorials which cover Functors & Applicatives at length.

This is my first attempt at Functors & Applicatives.  Hopefully it
gives an idea re: how these may be used. Hopefully these snippets are
valid use examples and do not misguide anyone.

I do not yet understand the concepts enough to try to explain them. My
explanations would most likely confuse or amuse people depending on
their experience.

These are but self sufficient modules which compile & work.
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: basic use of Applicative

Francesco Ariis
On Thu, Jun 25, 2015 at 08:30:54PM +0200, Imants Cekusins wrote:
> > Words?
> > Sentences?
>
> You see, these are only short snippets on purpose. There are many
> books and tutorials which cover Functors & Applicatives at length.
>
> This is my first attempt at Functors & Applicatives.  Hopefully it
> gives an idea re: how these may be used. Hopefully these snippets are
> valid use examples and do not misguide anyone.

Breadcrumb tutorials [1] have a similar approach; maybe you could
collect yours and list them in a single web page, it would be
easier to navigate.

[1] https://acm.wustl.edu/functional/hs-breads.php
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
KC
Reply | Threaded
Open this post in threaded view
|

Re: basic use of Applicative

KC
In reply to this post by Imants Cekusins

I'm a part time tutor
even though I don't look Elizabethan :D

Try explaining them to increase your understanding

--
--

Sent from an expensive device which will be obsolete in a few months! :D

Casey
   

On Jun 25, 2015 11:31 AM, "Imants Cekusins" <[hidden email]> wrote:
> Words?
> Sentences?

You see, these are only short snippets on purpose. There are many
books and tutorials which cover Functors & Applicatives at length.

This is my first attempt at Functors & Applicatives.  Hopefully it
gives an idea re: how these may be used. Hopefully these snippets are
valid use examples and do not misguide anyone.

I do not yet understand the concepts enough to try to explain them. My
explanations would most likely confuse or amuse people depending on
their experience.

These are but self sufficient modules which compile & work.
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: basic use of Applicative

Imants Cekusins
> Try explaining them to increase your understanding

Functor:
take a polymorphic type e.g. Maybe a
define a functor instance for it (fmap)

now we can write an  (a -> a) function and apply it to the polymorphic type


Applicative:
allows to apply (a -> ... -> a) to the polymorphic type for which
Functor & Applicative instance is defined

?
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: basic use of Applicative

Imants Cekusins
In reply to this post by Francesco Ariis
> Breadcrumb tutorials [1] have a similar approach; maybe you could
collect yours and list them in a single web page, it would be
easier to navigate.

> [1] https://acm.wustl.edu/functional/hs-breads.php

Interesting. I did not see it before. Thank you for the link.

If anyone wants to use these snippets - modified or not - they are
free and welcome to do so. Don't blame me though.
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: basic use of Applicative

Imants Cekusins
-- (a::Int -> b::Bool)

module ApplicativeAb where


-- 2 arg
add3:: Int -> Bool -> Int
add3 x1 addYes
   | addYes = x1 + 3
   | otherwise = x1


main::IO()
main =   do
   print $  maybe2 (Just 3) (Just True)
   print $  maybe2 (Just 3) (Just False)
   print $  list2 [3] [True]


-- maybe
maybe2::Maybe Int -> Maybe Bool -> Maybe Int
maybe2 mi mb = add3 <$> mi <*> mb


-- list
list2::[Int] -> [Bool] -> [Int]
list2 i b = add3 <$> i <*> b
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners