Send Beginners mailing list submissions to
        beginners@haskell.org

To subscribe or unsubscribe via the World Wide Web, visit
        http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        beginners-requ...@haskell.org

You can reach the person managing the list at
        beginners-ow...@haskell.org

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1.  RDBMS like references? (martin)
   2. Re:  RDBMS like references? (Theodore Lief Gannon)
   3. Re:  RDBMS like references? (Theodore Lief Gannon)
   4.  Generating Infinite List of Fibonacci Numbers (Desonte Jolivet)


----------------------------------------------------------------------

Message: 1
Date: Tue, 28 Jun 2016 20:39:46 +0200
From: martin <martin.drautzb...@web.de>
To: "beginners@haskell.org" <beginners@haskell.org>
Subject: [Haskell-beginners] RDBMS like references?
Message-ID: <5772c472.5060...@web.de>
Content-Type: text/plain; charset=utf-8

Hello all,

I recently tried to model "Items", where an Item can either be inside another 
Item or at a certain location. I started
like this:

data Location i l = At l
                  | In (Item i l)
                    deriving (Eq, Show)

data Item i l     = Itm i (Location i l)
                    deriving (Eq, Show)

type ItemDb i l = [Item i l]

ex_itemdb = let i1 = Itm 1 (At "a")
                i2 = Itm 2 (In i1)
            in [i1, i2]

Now I wanted to move Item 1 to a different location using this code:

moved = fmap f ex_itemdb
        where
            f (Itm 1 (At "a")) = (Itm 1 (At "b"))
            f x = x

Not this does not do what I want. I get

-- *Main> ex_itemdb
-- [Itm 1 (At "a"),Itm 2 (In (Itm 1 (At "a")))]

-- *Main> moved
-- [Itm 1 (At "b"),Itm 2 (In (Itm 1 (At "a")))]

While Item 1 has moved to "b" Item 2 still claims to be in an "Itm 1 (At "a")". 
I understand what's going on here and
coming from a DB background I can see that the redundant data is the root of 
the problem.

I can model this in a relational way such that the redundancy is avoided. But 
maybe there is a more haskellish/idiomatic
way to acomplish this.

Is there?



------------------------------

Message: 2
Date: Tue, 28 Jun 2016 14:26:49 -0700
From: Theodore Lief Gannon <tan...@gmail.com>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <beginners@haskell.org>
Subject: Re: [Haskell-beginners] RDBMS like references?
Message-ID:
        <cajopsudkqgucfap1dfa6eaal75ynma+l+ad_am5njsp4y1q...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

Sometimes relational is the way to go, but it's always worth looking for
other options. :) The idiomatic preference, I think, would be to separate
the nesting structure from the object data. The item containment graph
sounds like a RoseTree, and since location is always inherited, that can
just be a Map.

import qualified Data.Map as M
data Rose a = Rose a [Rose a] deriving (Eq, Show)
data Item a = Item a deriving (Eq, Show)
type ItemGraph a = Rose (Item a)
type ItemDB k a = M.Map k (ItemGraph a)

>>> let ex_itemdb = M.fromList [("a", Rose (Item 1) [Rose (Item 2) []])]
>>> M.lookup "a" ex_itemdb
Just (Rose (Item 1) [Rose (Item 2) []])

>>> let roseContains i (Rose x xs) = i == x || any (roseContains i) xs
>>> let isItemAt i l idb = maybe False id $ roseContains i <$> M.lookup l
idb
>>> isItemAt (Item 2) "a" ex_itemdb
True

>>> let swapLoc l1 l2 idb = let {i1 = M.lookup l1 idb; i2 = M.lookup l2
idb} in maybe (M.delete l1) (M.insert l1) i2 . maybe (M.delete l2)
(M.insert l2) i1 $ idb
>>> let moved = swapLoc "a" "b" ex_itemdb
>>> isItemAt (Item 2) "a" moved
False
>>> moved
fromList [("b",Rose (Item 1) [Rose (Item 2) []])]

There are quite a few unfinished pieces here, but hopefully it gets you
thinking in new directions!

On Tue, Jun 28, 2016 at 11:39 AM, martin <martin.drautzb...@web.de> wrote:

> Hello all,
>
> I recently tried to model "Items", where an Item can either be inside
> another Item or at a certain location. I started
> like this:
>
> data Location i l = At l
>                   | In (Item i l)
>                     deriving (Eq, Show)
>
> data Item i l     = Itm i (Location i l)
>                     deriving (Eq, Show)
>
> type ItemDb i l = [Item i l]
>
> ex_itemdb = let i1 = Itm 1 (At "a")
>                 i2 = Itm 2 (In i1)
>             in [i1, i2]
>
> Now I wanted to move Item 1 to a different location using this code:
>
> moved = fmap f ex_itemdb
>         where
>             f (Itm 1 (At "a")) = (Itm 1 (At "b"))
>             f x = x
>
> Not this does not do what I want. I get
>
> -- *Main> ex_itemdb
> -- [Itm 1 (At "a"),Itm 2 (In (Itm 1 (At "a")))]
>
> -- *Main> moved
> -- [Itm 1 (At "b"),Itm 2 (In (Itm 1 (At "a")))]
>
> While Item 1 has moved to "b" Item 2 still claims to be in an "Itm 1 (At
> "a")". I understand what's going on here and
> coming from a DB background I can see that the redundant data is the root
> of the problem.
>
> I can model this in a relational way such that the redundancy is avoided.
> But maybe there is a more haskellish/idiomatic
> way to acomplish this.
>
> Is there?
>
> _______________________________________________
> Beginners mailing list
> Beginners@haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20160628/8d7c1efa/attachment-0001.html>

------------------------------

Message: 3
Date: Tue, 28 Jun 2016 15:10:22 -0700
From: Theodore Lief Gannon <tan...@gmail.com>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <beginners@haskell.org>
Subject: Re: [Haskell-beginners] RDBMS like references?
Message-ID:
        <CAJoPsuCOOJKBUYpcGZSC7h8qrp=ahcywmtkssgehtyftqeg...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

Another possibility would be to keep your definition for Location, but
switch to:

data Item a = Item a
type ItemDB = Map Item Location

...and use a recursive lookup to find the location of an item contained
within another. This is quite a bit simpler, but whether it's preferable
probably depends on whether your more common lookup is "where is item x?"
vs. "what's at location y?"
On Jun 28, 2016 2:26 PM, "Theodore Lief Gannon" <tan...@gmail.com> wrote:

> Sometimes relational is the way to go, but it's always worth looking for
> other options. :) The idiomatic preference, I think, would be to separate
> the nesting structure from the object data. The item containment graph
> sounds like a RoseTree, and since location is always inherited, that can
> just be a Map.
>
> import qualified Data.Map as M
> data Rose a = Rose a [Rose a] deriving (Eq, Show)
> data Item a = Item a deriving (Eq, Show)
> type ItemGraph a = Rose (Item a)
> type ItemDB k a = M.Map k (ItemGraph a)
>
> >>> let ex_itemdb = M.fromList [("a", Rose (Item 1) [Rose (Item 2) []])]
> >>> M.lookup "a" ex_itemdb
> Just (Rose (Item 1) [Rose (Item 2) []])
>
> >>> let roseContains i (Rose x xs) = i == x || any (roseContains i) xs
> >>> let isItemAt i l idb = maybe False id $ roseContains i <$> M.lookup l
> idb
> >>> isItemAt (Item 2) "a" ex_itemdb
> True
>
> >>> let swapLoc l1 l2 idb = let {i1 = M.lookup l1 idb; i2 = M.lookup l2
> idb} in maybe (M.delete l1) (M.insert l1) i2 . maybe (M.delete l2)
> (M.insert l2) i1 $ idb
> >>> let moved = swapLoc "a" "b" ex_itemdb
> >>> isItemAt (Item 2) "a" moved
> False
> >>> moved
> fromList [("b",Rose (Item 1) [Rose (Item 2) []])]
>
> There are quite a few unfinished pieces here, but hopefully it gets you
> thinking in new directions!
>
> On Tue, Jun 28, 2016 at 11:39 AM, martin <martin.drautzb...@web.de> wrote:
>
>> Hello all,
>>
>> I recently tried to model "Items", where an Item can either be inside
>> another Item or at a certain location. I started
>> like this:
>>
>> data Location i l = At l
>>                   | In (Item i l)
>>                     deriving (Eq, Show)
>>
>> data Item i l     = Itm i (Location i l)
>>                     deriving (Eq, Show)
>>
>> type ItemDb i l = [Item i l]
>>
>> ex_itemdb = let i1 = Itm 1 (At "a")
>>                 i2 = Itm 2 (In i1)
>>             in [i1, i2]
>>
>> Now I wanted to move Item 1 to a different location using this code:
>>
>> moved = fmap f ex_itemdb
>>         where
>>             f (Itm 1 (At "a")) = (Itm 1 (At "b"))
>>             f x = x
>>
>> Not this does not do what I want. I get
>>
>> -- *Main> ex_itemdb
>> -- [Itm 1 (At "a"),Itm 2 (In (Itm 1 (At "a")))]
>>
>> -- *Main> moved
>> -- [Itm 1 (At "b"),Itm 2 (In (Itm 1 (At "a")))]
>>
>> While Item 1 has moved to "b" Item 2 still claims to be in an "Itm 1 (At
>> "a")". I understand what's going on here and
>> coming from a DB background I can see that the redundant data is the root
>> of the problem.
>>
>> I can model this in a relational way such that the redundancy is avoided.
>> But maybe there is a more haskellish/idiomatic
>> way to acomplish this.
>>
>> Is there?
>>
>> _______________________________________________
>> Beginners mailing list
>> Beginners@haskell.org
>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20160628/b353cc5e/attachment-0001.html>

------------------------------

Message: 4
Date: Wed, 29 Jun 2016 03:39:23 +0000 (UTC)
From: Desonte Jolivet <jolivetdeso...@yahoo.com>
To: "beginners@haskell.org" <beginners@haskell.org>
Subject: [Haskell-beginners] Generating Infinite List of Fibonacci
        Numbers
Message-ID:
        <2139750997.643950.1467171563397.javamail.ya...@mail.yahoo.com>
Content-Type: text/plain; charset="utf-8"


Hi,

Problem: I would like to generate an infinite list of Fibonacci numbers. 
Although the below code does not work.

fibs :: [Integer]fibs = 0 : 1 : [ n | x <-[2..], let n = ((fibs !! x-1) + (fibs 
!! x-2))]

Thought: I'm assuming that I'm ignorant on how ranges/generators work with a 
list comprehension, which is why this code is not working.

ghci output:
*Main> fibs !! 01*Main> fibs !! 12*Main> fibs !! 2 
When I try and access the third element in the list ghci simply stalls.
Can someone please give me a little more insight on why my code is not working.
Thanks in advance.
drjoliv
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20160629/6dcb89ac/attachment.html>

------------------------------

Subject: Digest Footer

_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


------------------------------

End of Beginners Digest, Vol 96, Issue 18
*****************************************

Reply via email to