Haskell Question

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

Haskell Question

instanct95
I need help with this haskell function
Write a function productLastPart which, only using library functions, returns the product of the last n numbers in the list, where n is the first argument to the function. 
productLastPart :: Int -> [Int] -> Int

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

Re: Haskell Question

Leonhard Applis
As I think you´re learning Haskell, I´m just giving some hints:

There is a function for dropping elements: http://zvon.org/other/haskell/Outputprelude/drop_f.html
Also there is similiar a "take" which may helps you with "reverse".


And for multiplying everything you might want to use a fold: http://zvon.org/other/haskell/Outputprelude/foldr_f.html

best regards
Leonhard


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

publickey - Leonhard.Applis@protonmail.com - 0x807FDDF3.asc (2K) Download Attachment
signature.asc (490 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Haskell Question

Seph Shewell Brockway
In reply to this post by instanct95
On Wed, Oct 16, 2019 at 09:09:40PM +0000, [hidden email] wrote:
> I need help with this haskell functionWrite a function productLastPart which, only using library functions, returns the product of thelast n numbers in the list, where n is the first argument to the function. productLastPart :: Int -> [Int] -> Int

It would probably help you learn if you made an attempt to solve it
yourself first. If you post what you have so far, I’d be happy to take
a look.

         Regards,

         Seph

--
Seph Shewell Brockway, BSc MSc (Glas.)
Pronouns: she/her
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Haskell Question

Ut Primum
Hi,
this is my solution:

productLastPart :: Int -> [Int] -> Int
productLastPart n xs = product (take n (reverse xs))

It only uses functions product, take and reverse
Regards, 
Ut

Mail priva di virus. www.avg.com

Il giorno ven 18 ott 2019 alle ore 18:16 Seph Shewell Brockway <[hidden email]> ha scritto:
On Wed, Oct 16, 2019 at 09:09:40PM +0000, [hidden email] wrote:
> I need help with this haskell functionWrite a function productLastPart which, only using library functions, returns the product of thelast n numbers in the list, where n is the first argument to the function. productLastPart :: Int -> [Int] -> Int

It would probably help you learn if you made an attempt to solve it
yourself first. If you post what you have so far, I’d be happy to take
a look.

         Regards,

         Seph

--
Seph Shewell Brockway, BSc MSc (Glas.)
Pronouns: she/her
_______________________________________________
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: Haskell Question

Seph Shewell Brockway
On Fri, Oct 18, 2019 at 06:59:05PM +0200, Ut Primum wrote:
> Hi,
> this is my solution:
>
> productLastPart :: Int -> [Int] -> Int
> productLastPart n xs = product (take n (reverse xs))
>
> It only uses functions product, take and reverse
> Regards,
> Ut

That should work. I’d probably use point-free style, though, and avoid
all of those brackets.

   productLastPart n = product . take n . reverse

The version you’ve written runs in linear time, as would a version that
worked like this:

   productLastPart n xs = product (drop (length xs - n) xs)

because reverse, length and product (actually a partial application of
foldr) are all linear in the length of the list—they access each element
once. You can see the GHC implementation of reverse at
https://hackage.haskell.org/package/base-4.12.0.0/docs/src/GHC.List.html#reverse.
This turns out to be the best we can do with a regular Haskell list,
because accessing the last element of a list is a linear-time operation:

   last :: [a] -> a
   last [] = error "Oh no!"
   last [x] = x
   last (x : xs) = last xs

There do, however, exist list types like Sequence and Vector that allow
constant-time access to both ends of the list.

Hope this is helpful.

                                                Seph

--
Seph Shewell Brockway, BSc MSc (Glas.)
Pronouns: she/her
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners