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. Passing a file containing a list as an input (Awsaf Rahman) 2. Re: Passing a file containing a list as an input (Tobias Brandt) 3. Execution time of Mergesort (Awsaf Rahman) 4. Re: Execution time of Mergesort (KC) 5. Re: Execution time of Mergesort (Awsaf Rahman) ---------------------------------------------------------------------- Message: 1 Date: Thu, 5 Jul 2018 22:21:15 +0200 From: Awsaf Rahman <awsafrahman1...@gmail.com> To: beginners@haskell.org Subject: [Haskell-beginners] Passing a file containing a list as an input Message-ID: <caoh+qtdwoi6sypgp1+6gticin1rfqozmzjr33anrtomctgk...@mail.gmail.com> Content-Type: text/plain; charset="utf-8" Hello everyone! I am really struggling with Haskell I/O. I have written a mergesort program and as an input to that program I want to use a file containing a list of 1000 integers. Let's say the contents of the file look like this [120, 400, 500 , 20, 100 ..] How can I achieve that? I am trying to write a main function that takes the file as an input and passes the list to my mergesort function.. Regards Awsaf Rahman -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.haskell.org/pipermail/beginners/attachments/20180705/5e1dcad9/attachment-0001.html> ------------------------------ Message: 2 Date: Thu, 05 Jul 2018 22:36:39 +0200 From: Tobias Brandt <to...@uni-bremen.de> To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell <beginners@haskell.org> Subject: Re: [Haskell-beginners] Passing a file containing a list as an input Message-ID: <20180705223639.horde.xya17xzozlb2efl2sr-t...@webmail.uni-bremen.de> Content-Type: text/plain; charset="utf-8"; Format="flowed"; DelSp="Yes" Hello Awsaf, you should probably look further into the topic of "IO in Haskell" and "Monads". Then you can use impure functions like readFile (http://hackage.haskell.org/package/base-4.11.1.0/docs/Prelude.html#v:readFile ) or getArgs (http://hackage.haskell.org/package/base-4.11.1.0/docs/System-Environment.html#v:getArgs). A naive solution might look like this: module Main where import System.Environment (getArgs) main = do (fileName:_) <- getArgs unsortedList <- read <$> readFile fileName let sorted = yourMergeSortFunction unsortedList print sorted -- maybe you like to print your sorted list Best regards, Tobias ----- Nachricht von Awsaf Rahman <awsafrahman1...@gmail.com> --------- Datum: Thu, 5 Jul 2018 22:21:15 +0200 Von: Awsaf Rahman <awsafrahman1...@gmail.com> Antwort an: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell <beginners@haskell.org> Betreff: [Haskell-beginners] Passing a file containing a list as an input An: beginners@haskell.org > Hello everyone! > I am really struggling with Haskell I/O. > > I have written a mergesort program and as an input to that > program I want to use a file containing a list of 1000 integers. > Let's say the contents of the file look like this > > [120, 400, 500 , 20, 100 ..] > > How can I achieve that? I am trying to write a main function that > takes the file as an input and passes the list to my mergesort > function.. > > Regards > Awsaf Rahman ----- Ende der Nachricht von Awsaf Rahman <awsafrahman1...@gmail.com> ----- -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.haskell.org/pipermail/beginners/attachments/20180705/bd9ee550/attachment-0001.html> ------------------------------ Message: 3 Date: Fri, 6 Jul 2018 04:01:27 +0200 From: Awsaf Rahman <awsafrahman1...@gmail.com> To: beginners@haskell.org Subject: [Haskell-beginners] Execution time of Mergesort Message-ID: <caoh+qte-_1vidhqutjtf83ip8opgdwxlmnm3r+parb8mmf4...@mail.gmail.com> Content-Type: text/plain; charset="utf-8" Hello everyone! I am trying to find out the execution time of mergesort for a list size of 1 million integers. I have done the same in Erlang as well and the execution time in Erlang was around 0.93 seconds. I have implemented the program in almost the same way in Haskell as well but for some reason the Haskell implementation is taking around 12 seconds which doesn't seem right. Here is the implementation in Haskell: *{-# LANGUAGE OverloadedStrings #-}* *{-# LANGUAGE BangPatterns #-}* *import Control.Exception* *import Formatting* *import Formatting.Clock* *import System.Clock* *import Control.DeepSeq* *mergesort [] = []* *mergesort [x] = [x]* *mergesort xs = let (lhalf, rhalf) = splitAt (length xs `div` 2) xs* * in merge' (mergesort lhalf) (mergesort rhalf)* *merge' lhalf rhalf = merge lhalf rhalf []* *merge [] [] acc = reverse acc* *merge [] y acc = reverse acc ++ y* *merge x [] acc = reverse acc ++ x* *merge (l:ls) (r:rs) acc* * | l < r = merge ls (r:rs) (l:acc)* * | otherwise = merge rs (l:ls) (r:acc)* *toList :: String -> [Integer]* *toList input = read ("[" ++ input ++ "]")* *main = do* * file <- getLine* * contents <- readFile file* * let !unsortedlist = (toList contents)* * start <- getTime Monotonic* * evaluate(force (mergesort unsortedlist))* * end <- getTime Monotonic* * fprint (timeSpecs % "\n") start end* What am I doing wrong? -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.haskell.org/pipermail/beginners/attachments/20180706/30c71f34/attachment-0001.html> ------------------------------ Message: 4 Date: Thu, 5 Jul 2018 19:06:10 -0700 From: KC <kc1...@gmail.com> To: Haskell Beginners <beginners@haskell.org> Subject: Re: [Haskell-beginners] Execution time of Mergesort Message-ID: <CAMLKXyk49OkO47JF1Yys0TL=wz-1cizbsqw7--hg89qjooa...@mail.gmail.com> Content-Type: text/plain; charset="utf-8" I haven't looked at the code much yet But might want to consider strictness 😎 -- Sent from an expensive device which will be obsolete in a few months Casey On Thu, Jul 5, 2018, 7:01 PM Awsaf Rahman, <awsafrahman1...@gmail.com> wrote: > Hello everyone! > > I am trying to find out the execution time of mergesort for a list size of > 1 million integers. I have done the same in Erlang as well and the > execution time in Erlang was around 0.93 seconds. I have implemented the > program in almost the same way in Haskell as well but for some reason the > Haskell implementation is taking around 12 seconds which doesn't seem > right. > > Here is the implementation in Haskell: > > *{-# LANGUAGE OverloadedStrings #-}* > *{-# LANGUAGE BangPatterns #-}* > *import Control.Exception* > *import Formatting* > *import Formatting.Clock* > *import System.Clock* > *import Control.DeepSeq* > > *mergesort [] = []* > *mergesort [x] = [x]* > *mergesort xs = let (lhalf, rhalf) = splitAt (length xs `div` 2) xs* > * in merge' (mergesort lhalf) (mergesort rhalf)* > > *merge' lhalf rhalf = merge lhalf rhalf []* > > *merge [] [] acc = reverse acc* > *merge [] y acc = reverse acc ++ y* > *merge x [] acc = reverse acc ++ x* > > *merge (l:ls) (r:rs) acc* > * | l < r = merge ls (r:rs) (l:acc)* > * | otherwise = merge rs (l:ls) (r:acc)* > > *toList :: String -> [Integer]* > *toList input = read ("[" ++ input ++ "]")* > > *main = do* > * file <- getLine* > * contents <- readFile file* > * let !unsortedlist = (toList contents)* > * start <- getTime Monotonic* > * evaluate(force (mergesort unsortedlist))* > * end <- getTime Monotonic* > * fprint (timeSpecs % "\n") start end* > > > What am I doing wrong? > _______________________________________________ > 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/20180705/6eef1232/attachment-0001.html> ------------------------------ Message: 5 Date: Fri, 6 Jul 2018 04:14:37 +0200 From: Awsaf Rahman <awsafrahman1...@gmail.com> To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell <beginners@haskell.org> Subject: Re: [Haskell-beginners] Execution time of Mergesort Message-ID: <caoh+qtevzwb7_-nef4gkt3wfzrzkdmwc4pyrty+rnyesnua...@mail.gmail.com> Content-Type: text/plain; charset="utf-8" I did consider strictness and that is why the execution time has come down from 16 seconds to 12 seconds! But I can't seem to find the issue anymore! On Fri, Jul 6, 2018 at 4:06 AM, KC <kc1...@gmail.com> wrote: > I haven't looked at the code much yet > But might want to consider strictness 😎 > > -- > Sent from an expensive device which will be obsolete in a few months > Casey > > On Thu, Jul 5, 2018, 7:01 PM Awsaf Rahman, <awsafrahman1...@gmail.com> > wrote: > >> Hello everyone! >> >> I am trying to find out the execution time of mergesort for a list size >> of 1 million integers. I have done the same in Erlang as well and the >> execution time in Erlang was around 0.93 seconds. I have implemented the >> program in almost the same way in Haskell as well but for some reason the >> Haskell implementation is taking around 12 seconds which doesn't seem >> right. >> >> Here is the implementation in Haskell: >> >> *{-# LANGUAGE OverloadedStrings #-}* >> *{-# LANGUAGE BangPatterns #-}* >> *import Control.Exception* >> *import Formatting* >> *import Formatting.Clock* >> *import System.Clock* >> *import Control.DeepSeq* >> >> *mergesort [] = []* >> *mergesort [x] = [x]* >> *mergesort xs = let (lhalf, rhalf) = splitAt (length xs `div` 2) xs* >> * in merge' (mergesort lhalf) (mergesort rhalf)* >> >> *merge' lhalf rhalf = merge lhalf rhalf []* >> >> *merge [] [] acc = reverse acc* >> *merge [] y acc = reverse acc ++ y* >> *merge x [] acc = reverse acc ++ x* >> >> *merge (l:ls) (r:rs) acc* >> * | l < r = merge ls (r:rs) (l:acc)* >> * | otherwise = merge rs (l:ls) (r:acc)* >> >> *toList :: String -> [Integer]* >> *toList input = read ("[" ++ input ++ "]")* >> >> *main = do* >> * file <- getLine* >> * contents <- readFile file* >> * let !unsortedlist = (toList contents)* >> * start <- getTime Monotonic* >> * evaluate(force (mergesort unsortedlist))* >> * end <- getTime Monotonic* >> * fprint (timeSpecs % "\n") start end* >> >> >> What am I doing wrong? >> _______________________________________________ >> Beginners mailing list >> Beginners@haskell.org >> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners >> > > _______________________________________________ > 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/20180706/73d17b7f/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 121, Issue 4 *****************************************