Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read data from IO into data-structure and then process the data-structure?

Tags:

haskell

first off sorry for doing the typical thing of 'where do I begin', but I'm totally lost.

I've been reading the 'Learn you a haskell for great good' site for what feels like an age now (pretty much half a semester. I'm just about to finish the 'Input and Output' chapter, and I still have no clue how to write a multi line program.

I've seen the do statement, and that you can only use it to concat IO actions into a single function, but I can't see how I'm gonna go about writing a realistic application.

Can someone point me in the right direction.

I'm from a C background, and basically I'm using haskell for one of my modules this semester at uni, I want to compare C++ against haskell (in many aspects). I'm looking to create a series of searching and sorting programs so that I can comment on how easy they are in the respective languages versus their speed.

However, I'm really starting to loose my faith in using Haskell as its been six weeks, and I still have no idea how to write a complete application, and the chapters in the site I'm reading seem to be getting longer and longer.

I basically need to create a basic object which will be stored in the structure (which I know how to do), more what I'm struggling with is, how do I create a program which reads data in from some text file, and populates the structure with that data in the first place, then goes on to process it. As haskell seems to split IO and other operations and it won't just let me write multiple lines in a program, I'm looking for something like this:

main = data <- getContent
       let allLines = lines data
       let myStructure = generateStruct allLines
       sort/search/etc
       print myStructure

how do I go about this? any good tutorials which will help me get going with realistic programs?

-A

like image 919
Andrew Broadbent Avatar asked Mar 05 '11 17:03

Andrew Broadbent


People also ask

How do you create a data structure?

Select Data Structure on the Add Object form and click the OK button. Enter the name, description, and product code of a data structure. For a regular data structure, select Regular Data Structure. Complete the Add Object form and select Regular Data Structure, then click the OK button.

What is structure and data structure?

A data structure is a storage that is used to store and organize data. It is a way of arranging data on a computer so that it can be accessed and updated efficiently. A data structure is not only used for organizing the data. It is also used for processing, retrieving, and storing data.

What are the four basic data structures in JavaScript?

The four types of basic data structures supported by JavaScript are: array lists, maps, records and JSON tables.


1 Answers

You mentioned seeing do notation, now it's time to learn how to use do. Consider your example main is an IO, you should be using do syntax or binds:

main = do
  dat <- getContent
  let allLines = lines dat
      myStructure = generateStruct allLines
      sorted = mySort myStructure
      searchResult = mySearch myStructure
  print myStructure
  print sorted
  print searchResult

So now you have a main that gets stdin, turns it into [String] via lines, presumably parses it into a structure and runs sorting and searches on that structure. Notice the interesting code is all pure - mySort, mySearch, and generateStruct doesn't need to be IO (and can't be, being inside a let binding) so you are actually properly using pure and effectful code together.

I suggest you look at how bind works (>>=) and how do notation desugars into bind. This SO question should help.

like image 80
Thomas M. DuBuisson Avatar answered Oct 05 '22 08:10

Thomas M. DuBuisson