Passing a file containing a list as an input

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

Passing a file containing a list as an input

Awsaf Rahman
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

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

Re: Passing a file containing a list as an input

Tobias Brandt-3

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 <[hidden email]> ---------
     Datum: Thu, 5 Jul 2018 22:21:15 +0200
       Von: Awsaf Rahman <[hidden email]>
Antwort an: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell <[hidden email]>
   Betreff: [Haskell-beginners] Passing a file containing a list as an input
        An: [hidden email]

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 <[hidden email]> -----


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

Re: Passing a file containing a list as an input

Olivier Revollat
In reply to this post by Awsaf Rahman
Hi,
Maybe you should try "interact"

main = io (map processIt) 
io f = interact (unlines . f . lines)



Le jeu. 5 juil. 2018 à 22:21, Awsaf Rahman <[hidden email]> a écrit :
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
_______________________________________________
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: Passing a file containing a list as an input

Olivier Revollat

Example (take stdin and stdout the uppercased)
import Data.Char(toUpper)
main = interact (map toUpper)


Le ven. 6 juil. 2018 à 09:33, Olivier Revollat <[hidden email]> a écrit :
Hi,
Maybe you should try "interact"

main = io (map processIt) 
io f = interact (unlines . f . lines)



Le jeu. 5 juil. 2018 à 22:21, Awsaf Rahman <[hidden email]> a écrit :
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
_______________________________________________
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