From instanct95 at aol.com Wed Oct 16 21:09:40 2019 From: instanct95 at aol.com (instanct95 at aol.com) Date: Wed, 16 Oct 2019 21:09:40 +0000 (UTC) Subject: [Haskell-beginners] Haskell Question References: <303350846.1729486.1571260180492.ref@mail.yahoo.com> Message-ID: <303350846.1729486.1571260180492@mail.yahoo.com> 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 -------------- next part -------------- An HTML attachment was scrubbed... URL: From Leonhard.Applis at protonmail.com Thu Oct 17 13:22:25 2019 From: Leonhard.Applis at protonmail.com (Leonhard Applis) Date: Thu, 17 Oct 2019 13:22:25 +0000 Subject: [Haskell-beginners] Haskell Question Message-ID: 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 -------------- next part -------------- A non-text attachment was scrubbed... Name: publickey - Leonhard.Applis at protonmail.com - 0x807FDDF3.asc Type: application/pgp-keys Size: 1843 bytes Desc: not available URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 477 bytes Desc: OpenPGP digital signature URL: From BalterNotz at foxmail.com Fri Oct 18 15:53:42 2019 From: BalterNotz at foxmail.com (BalterNotz at foxmail.com) Date: Fri, 18 Oct 2019 23:53:42 +0800 Subject: [Haskell-beginners] How to use GHC.Event module, main thread not exit directly Message-ID: <201910182353417471209@foxmail.com>+8C573A28B77CA83E Hello everyone: I write a server use GHC.Event module, Refer to this article https://wiki.haskell.org/Simple_Servers {-# LANGUAGE OverloadedStrings #-} -- A simple example of an epoll based http server in Haskell. -- -- Uses two libraries: -- * network-bytestring, bytestring-based socket IO. -- - cabal install network-bytestring: -- -- * haskell-event, epoll-based scalable IO events -- - git clone git://github.com/tibbe/event.git -- - autoreconf ; then cabal install import Network hiding (accept) import Network.Socket (fdSocket, accept) import Network.Socket.ByteString import Data.ByteString.Char8 import System.Event import System.Posix import System.Posix.IO main = withSocketsDo $ do sock <- listenOn $ PortNumber 5002 let fd = fromIntegral (fdSocket sock) mgr <- new registerFd mgr (client sock) fd evtRead loop mgr client sock _ _ = do (c,_) <- accept sock sendAll c msg sClose c msg = "HTTP/1.0 200 OK\r\nContent-Length: 5\r\n\r\nPong!\r\n" but I not find the definition of 'loop' function: loop mgr the after is my program: btnz at vmubuntuserver:~/work/code/echo-server-event-model$ cat Main.hs module Main where import Network.Socket import Network.Socket.ByteString (recv, sendAll) import GHC.Event as Event import qualified Control.Exception as E (bracket) import Data.ByteString.Internal (packChars) hints = defaultHints {addrFlags = [AI_PASSIVE], addrSocketType = Stream} msg = "HTTP/1.0 200 OK\r\nContent-Length: 5\r\n\r\nPong!\r\n" main :: IO () main = do putStrLn "Hello, Haskell!" addr <- head <$> getAddrInfo (Just hints) Nothing (Just "3000") E.bracket (sock addr) close eventServer return () where sock addr = do s <- socket (addrFamily addr) (addrSocketType addr) (addrProtocol addr) setSocketOption s ReuseAddr 1 withFdSocket s setCloseOnExecIfNeeded bind s $ addrAddress addr listen s 1024 return s eventServer s = do fd <- withFdSocket s (\ci -> return (fromIntegral ci)) mgr <- Event.new Event.registerFd mgr (service s) fd Event.evtRead Event.OneShot -- loop mgr service :: Socket -> FdKey -> Event -> IO () service s _ _ = do (c, _) <- accept s sendAll c $ packChars msg close c btnz at vmubuntuserver:~/work/code/echo-server-event-model$ cabal run Up to date Hello, Haskell! when I run cabal run the program exit directly. BalterNotz at foxmail.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From seph at codex.scot Fri Oct 18 16:16:41 2019 From: seph at codex.scot (Seph Shewell Brockway) Date: Fri, 18 Oct 2019 17:16:41 +0100 Subject: [Haskell-beginners] Haskell Question In-Reply-To: <303350846.1729486.1571260180492@mail.yahoo.com> References: <303350846.1729486.1571260180492.ref@mail.yahoo.com> <303350846.1729486.1571260180492@mail.yahoo.com> Message-ID: <20191018161641.pl5zbgzd2z45bxzm@leviathan> On Wed, Oct 16, 2019 at 09:09:40PM +0000, instanct95 at aol.com 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 From utprimum at gmail.com Fri Oct 18 16:59:05 2019 From: utprimum at gmail.com (Ut Primum) Date: Fri, 18 Oct 2019 18:59:05 +0200 Subject: [Haskell-beginners] Haskell Question In-Reply-To: <20191018161641.pl5zbgzd2z45bxzm@leviathan> References: <303350846.1729486.1571260180492.ref@mail.yahoo.com> <303350846.1729486.1571260180492@mail.yahoo.com> <20191018161641.pl5zbgzd2z45bxzm@leviathan> Message-ID: 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 <#DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2> Il giorno ven 18 ott 2019 alle ore 18:16 Seph Shewell Brockway ha scritto: > On Wed, Oct 16, 2019 at 09:09:40PM +0000, instanct95 at aol.com 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 > Beginners at haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: From seph at codex.scot Fri Oct 18 19:44:22 2019 From: seph at codex.scot (Seph Shewell Brockway) Date: Fri, 18 Oct 2019 20:44:22 +0100 Subject: [Haskell-beginners] Haskell Question In-Reply-To: References: <303350846.1729486.1571260180492.ref@mail.yahoo.com> <303350846.1729486.1571260180492@mail.yahoo.com> <20191018161641.pl5zbgzd2z45bxzm@leviathan> Message-ID: <20191018194422.z2kuwm7tcvyxor6i@leviathan> 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 From admin at dominionbeef.us Sun Oct 27 18:58:44 2019 From: admin at dominionbeef.us (Site Administrator) Date: Sun, 27 Oct 2019 12:58:44 -0600 Subject: [Haskell-beginners] Putrady 2018 Ch. 3 - STM Message-ID: Hello, I am new not only to Haskell, but to programming in general. I hope that I am posting in the correct forum. I am working through "Practical Web Development With Haskell" by Ecky Putrady (2018), New York: Apress, and I am having some difficulty understanding the STM portion of Chapter three. More specifically, I cannot get the following code snippet to return the expected result in the REPL: -- snippet from Adapter.InMemory.Auth module as composed on pages 53-64 in the book import ClassyPrelude import qualified Domain.Auth as D -- as composed on pages 35-52 in the book import Conrol.Monad.Except import Text.StringRandom import Data.Has -- ... data State = State   { stateAuths :: [(D.UserId, D.Auth)] -- ... initialState :: State initialState = State   { stateAuths = [] -- ... type InMemory r m = (Has (TVar State) r, MonadReader r m, MonadIO m) addAuth :: InMemory r m                => D.Auth -> m (Either D.RegistrationError D.VerificationCode) addAuth auth = do   tvar <- asks getter -- ... rest of function These are the instructions for "Verification in REPL" provided on page 64 of the book: > :l Adapter.InMemory.Auth > let email = D.mkEmail "ecky at test.com" > let passw = D.mkPassword "1234ABCDefgh" > let auth = either undefined id $ D.Auth <$> email <*> passw > s <- newTVarIO initialState > addAuth s auth Calling the addAuth function is supposed to print the following: Right "aBNhtG653Bga9kas" -- (or whatever random vCode stringRandomIO generates) However, GHCI instead tells me: :6.9: error:        * Couldn't match expected type 'D.Auth'                                with actual type 'TVar State'        * In the first argument of 'addAuth', namely 's'           In the expression: addAuth s auth -- ... The source code for the book at https://github.com/Apress/practical-webdev-haskell was created with stack resolver lts-9.11, while I am using resolver lts-14.11. I am doing this intentionally, because debugging the compilation errors helps guide me to become more familiar with the documentation in the packages being implemented, as well as helping me to understand general Haskell concepts little by little. I have tried looking through the changelogs for the stm and classy-prelude packages on https://hackage.haskell.org, for any clues which would be intelligible to me (an extremely restrictive constraint, admittedly), that would help me understand why the addAuth function thinks the TVar State we pass to it should be a D.Auth, and why this presumably would not have been the case had I built the project with the lts-9.11 resolver, but I have not been able to understand it thus far. -------------- next part -------------- An HTML attachment was scrubbed... URL: From sylvain at haskus.fr Mon Oct 28 08:46:10 2019 From: sylvain at haskus.fr (Sylvain Henry) Date: Mon, 28 Oct 2019 09:46:10 +0100 Subject: [Haskell-beginners] Putrady 2018 Ch. 3 - STM In-Reply-To: References: Message-ID: Hi, The issue has nothing to do with STM. > that would help me understand why the addAuth function thinks the TVar State we pass to it should be a D.Auth Let's look at the type of `addAuth`: addAuth :: InMemory r m => D.Auth -> m (Either D.RegistrationError D.VerificationCode) It takes a single parameter with type `D.Auth`. So you don't have to pass `s` when you call it. The `TVar State` value is passed implicitly thanks to the constraint `InMemory r m` (i.e. a value with type `r` can be obtained from `m` Monad thanks to `MonadReader r m` and this value contains a `TVar State` thanks to the `Has (TVar State) r` constraint`). You can't call `addAuth` directly in GHCI (where `m` would be `IO`), you have to follow the example here: https://github.com/Apress/practical-webdev-haskell/blob/master/03/src/Lib.hs#L30 Or alternatively you can modify `addAuth` for you GHCI test: addAuth :: TVar State -> D.Auth -> m (Etiher ...) addAuth tvar auth = do   -- ... rest of function Hope this helps, Sylvain On 27/10/2019 19:58, Site Administrator wrote: > Hello, > > I am new not only to Haskell, but to programming in general. I hope > that I am posting in the correct forum. > > I am working through "Practical Web Development With Haskell" by Ecky > Putrady (2018), New York: Apress, and I am having some difficulty > understanding the STM portion of Chapter three. More specifically, I > cannot get the following code snippet to return the expected result in > the REPL: > > -- snippet from Adapter.InMemory.Auth module as composed on pages > 53-64 in the book > import ClassyPrelude > import qualified Domain.Auth as D -- as composed on pages 35-52 in > the book > import Conrol.Monad.Except > import Text.StringRandom > import Data.Has > -- ... > data State = State >   { stateAuths :: [(D.UserId, D.Auth)] > -- ... > initialState :: State > initialState = State >   { stateAuths = [] > -- ... > type InMemory r m = (Has (TVar State) r, MonadReader r m, MonadIO m) > > addAuth :: InMemory r m >                => D.Auth -> m (Either D.RegistrationError > D.VerificationCode) > addAuth auth = do >   tvar <- asks getter > -- ... rest of function > > These are the instructions for "Verification in REPL" provided on page > 64 of the book: > > > :l Adapter.InMemory.Auth > > let email = D.mkEmail "ecky at test.com" > > let passw = D.mkPassword "1234ABCDefgh" > > let auth = either undefined id $ D.Auth <$> email <*> passw > > s <- newTVarIO initialState > > addAuth s auth > > Calling the addAuth function is supposed to print the following: > > Right "aBNhtG653Bga9kas" -- (or whatever random vCode > stringRandomIO generates) > > However, GHCI instead tells me: > > :6.9: error: >        * Couldn't match expected type 'D.Auth' >                                with actual type 'TVar State' >        * In the first argument of 'addAuth', namely 's' >           In the expression: addAuth s auth > -- ... > > The source code for the book at > https://github.com/Apress/practical-webdev-haskell was created with > stack resolver lts-9.11, while I am using resolver lts-14.11. I am > doing this intentionally, because debugging the compilation errors > helps guide me to become more familiar with the documentation in the > packages being implemented, as well as helping me to understand > general Haskell concepts little by little. I have tried looking > through the changelogs for the stm and classy-prelude packages on > https://hackage.haskell.org, for any clues which would be intelligible > to me (an extremely restrictive constraint, admittedly), that would > help me understand why the addAuth function thinks the TVar State we > pass to it should be a D.Auth, and why this presumably would not have > been the case had I built the project with the lts-9.11 resolver, but > I have not been able to understand it thus far. > > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners -------------- next part -------------- An HTML attachment was scrubbed... URL: