Send Beginners mailing list submissions to
        [email protected]

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
        [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:  recursion - more elegant solution (Francesco Ariis)
   2. Re:  recursion - more elegant solution (Miro)


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

Message: 1
Date: Sat, 29 Aug 2015 02:23:12 +0200
From: Francesco Ariis <[email protected]>
To: [email protected]
Subject: Re: [Haskell-beginners] recursion - more elegant solution
Message-ID: <[email protected]>
Content-Type: text/plain; charset="utf-8"

On Sat, Aug 29, 2015 at 01:57:48AM +0200, Miro Karpis wrote:
> Hi haskellers, I have made one recursive function, where
> 
> 
> input is:
>    nodesIds: [1,2,4,3,5]
>    edgesIds: [(3,5),(4,3),(2,4),(1,2)]
> 
> and on output I need/have:
>    [([1,2,4,3,5],(3,5)),([1,2,4,3],(4,3)),([1,2,4],(2,4)),([1,2],(1,2))]
> 
> I have made one function for this, but it seems to me a bit too big and
> 'ugly'? Please, do you have any hints for a more elegant solution?
> 
> joinEdgeNodes' :: [Int] -> [Int] -> [(Int, Int)] -> [([Int], (Int, Int))]
> joinEdgeNodes' [] _ [] = []
> joinEdgeNodes' [] _  _ = []
> joinEdgeNodes'  _ _ [] = []
> joinEdgeNodes' (n) (wn) [e] = [(n, e)]
> joinEdgeNodes' (n) [] (e:es) = joinEdgeNodes' n edgeNodes es ++
> [(edgeNodes, e)]
>    where edgeNodes = take 2 n
> joinEdgeNodes' (n) (wn) (e:es) = joinEdgeNodes' n edgeNodes es ++
> [(edgeNodes, e)]
>    where edgeNodes = take edgeNodesLength n
>          edgeNodesLength = (length wn) + 1
> 
> I call the function with: let l = joinEdgeNodes' nodeIds [] edgeIds

Have you looked into `inits` (from Data.List), `reverse` and `zip`?

something like:

    ?> :m +Data.List
    ?> let a = [1,2,4,3,5]
    ?> let b = [(3,5),(4,3),(2,4),(1,2)]
    ?> zip (reverse . inits $ a) b
    [([1,2,4,3,5],(3,5)),([1,2,4,3],(4,3)),([1,2,4],(2,4)),([1,2],(1,2))]

seems to lead to the correct output

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: Digital signature
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20150829/aa713740/attachment-0001.sig>

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

Message: 2
Date: Sat, 29 Aug 2015 08:38:56 +0200
From: Miro <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of
        primarilybeginner-level topics related to Haskell
        <[email protected]>
Subject: Re: [Haskell-beginners] recursion - more elegant solution
Message-ID: <[email protected]>
Content-Type: text/plain; charset="utf-8"

Thank you! This is so much better :-)

-----Original Message-----
From: "Francesco Ariis" <[email protected]>
Sent: ?29/?08/?2015 02:24
To: "[email protected]" <[email protected]>
Subject: Re: [Haskell-beginners] recursion - more elegant solution

On Sat, Aug 29, 2015 at 01:57:48AM +0200, Miro Karpis wrote:
> Hi haskellers, I have made one recursive function, where
> 
> 
> input is:
>    nodesIds: [1,2,4,3,5]
>    edgesIds: [(3,5),(4,3),(2,4),(1,2)]
> 
> and on output I need/have:
>    [([1,2,4,3,5],(3,5)),([1,2,4,3],(4,3)),([1,2,4],(2,4)),([1,2],(1,2))]
> 
> I have made one function for this, but it seems to me a bit too big and
> 'ugly'? Please, do you have any hints for a more elegant solution?
> 
> joinEdgeNodes' :: [Int] -> [Int] -> [(Int, Int)] -> [([Int], (Int, Int))]
> joinEdgeNodes' [] _ [] = []
> joinEdgeNodes' [] _  _ = []
> joinEdgeNodes'  _ _ [] = []
> joinEdgeNodes' (n) (wn) [e] = [(n, e)]
> joinEdgeNodes' (n) [] (e:es) = joinEdgeNodes' n edgeNodes es ++
> [(edgeNodes, e)]
>    where edgeNodes = take 2 n
> joinEdgeNodes' (n) (wn) (e:es) = joinEdgeNodes' n edgeNodes es ++
> [(edgeNodes, e)]
>    where edgeNodes = take edgeNodesLength n
>          edgeNodesLength = (length wn) + 1
> 
> I call the function with: let l = joinEdgeNodes' nodeIds [] edgeIds

Have you looked into `inits` (from Data.List), `reverse` and `zip`?

something like:

    ?> :m +Data.List
    ?> let a = [1,2,4,3,5]
    ?> let b = [(3,5),(4,3),(2,4),(1,2)]
    ?> zip (reverse . inits $ a) b
    [([1,2,4,3,5],(3,5)),([1,2,4,3],(4,3)),([1,2,4],(2,4)),([1,2],(1,2))]

seems to lead to the correct output

-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20150829/6e561b06/attachment-0001.html>

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

Subject: Digest Footer

_______________________________________________
Beginners mailing list
[email protected]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


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

End of Beginners Digest, Vol 86, Issue 23
*****************************************

Reply via email to