Send Beginners mailing list submissions to
[email protected]
To subscribe or unsubscribe via the World Wide Web, visit
http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
[email protected]
You can reach the person managing the list at
[email protected]
When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."
Today's Topics:
1. Re: Data type definition for a list of elements of
alternating types? (David McBride)
2. Re: Functor on Tree data constructor (Nishant)
----------------------------------------------------------------------
Message: 1
Date: Wed, 2 Apr 2014 22:22:19 -0400
From: David McBride <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Data type definition for a list of
elements of alternating types?
Message-ID:
<CAN+Tr41ne-ZCUVoe=zUmcUjuj6CVed6kOoH2rwR=U=-pn-6...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"
Would this work for you?
data BiList a b
= Empty
| a :# (BiList b a)
infixr 5 :#
blah :: BiList Char Int
blah = 'a' :# 1 :# 'a' :# Empty
On Wed, Apr 2, 2014 at 9:52 PM, Jacek Dudek <[email protected]> wrote:
> -- As an exercise I wanted to define a datatype that is an alternating
> list of elements of two different types. The best that I could do are
> the type definitions below:
>
> module BiList
> ( BiList (..)
> , AList (EmptyA)
> , BList (EmptyB)
> ) where
>
> data BiList a b
> = Empty
> | BA (AList a b)
> | BB (BList a b)
>
> data AList a b
> = EmptyA
> | AL a (BList a b)
>
> data BList a b
> = EmptyB
> | BL b (AList a b)
>
> (<#) :: a -> (BList a b) -> (AList a b)
> a <# bs = AL a bs
>
> (<@) :: b -> (AList a b) -> (BList a b)
> b <@ as = BL b as
>
> infixr 5 <#
> infixr 5 <@
>
> example :: BiList Int Char
> example = BA $ 1 <# 'a' <@ 2 <# 'b' <@ 3 <# 'c' <@ 4 <# 'd' <@ EmptyA
>
> -- There are two things that I don't like about this implementation.
>
> -- (1) The BA and BB constructors that must be applied on top of
> instances of (AList a b) and (BList a b) to lift them to be of type
> (BiList a b).
>
> -- (2) Having three different constructors for an empty list: Empty,
> EmptyA, EmptyB, where ideally I would just have one.
>
> -- Is it possible to get around either of these annoyances with some
> type theory gymnastics? Maybe something like the function fromIntegral
> (the mechanics of which I don't really understand at this point)?
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20140402/38a8ceb0/attachment-0001.html>
------------------------------
Message: 2
Date: Thu, 3 Apr 2014 14:20:20 +0530
From: Nishant <[email protected]>
To: [email protected], The Haskell-Beginners Mailing List -
Discussion of primarily beginner-level topics related to Haskell
<[email protected]>
Subject: Re: [Haskell-beginners] Functor on Tree data constructor
Message-ID:
<CAEQDmz5ZKMhTJ-giAE_7S7ZoVJKXiASPmOpg8uenG=q17d8...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"
Thanks Vlatko ...
That analogy helped ..... I created my own functor typeclass where i
defined fmap :: MyFucntor f => (a->a) -> f a ->f a and that worked ...
On Wed, Apr 2, 2014 at 7:24 PM, Vlatko Basic <[email protected]> wrote:
> Try to visualize with concrete types
>
>
> data Tree a = NULL | Node (Tree a) a (Tree a) deriving Show
>
> Tree Int = Node (Tree Int) Int (Node Int) -- OK
> Tree String = Node (Tree String) String (Node String) -- OK
>
> f :: Int -> String
> f i = show i
>
> and see what you get:
>
>
> instance Functor Tree where
> fmap f NULL = NULL
> fmap f (Node left x
> right) -- Node
> (Tree Int) Int (Node Int)
> | (left == NULL) && (right == NULL) = Node left (f x) right -- Node
> (Tree Int) String (Node Int) === Not OK
> | (left /= NULL) = Node (fmap f left) x right
> -- Node (Tree String) Int (Node Int) === Not OK
> | (right /= NULL) = Node left x (fmap f
> right) -- Node (Tree Int) Int (Node
> String) === Not OK
>
>
>
> -------- Original Message --------
> Subject: [Haskell-beginners] Functor on Tree data constructor
> From: Nishant <[email protected]> <[email protected]>
> To: [email protected]
> Date: 02.04.2014 12:53
>
>
> instance Functor Tree where
> ............fmap f NULL = NULL
> ............fmap f (Node left x right) | (left == NULL) && (right ==
> NULL) = Node left (f x) right
> ................................................... | (left /= NULL) =
> Node (fmap f left) x right
> ................................................... | (right /= NULL) =
> Node left x (fmap f right)
>
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
>
--
Nishant
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20140403/33d144af/attachment-0001.html>
------------------------------
Subject: Digest Footer
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
------------------------------
End of Beginners Digest, Vol 70, Issue 4
****************************************