Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Locked thread
Magissima
Apr 15, 2013

I'd like to introduce you to some of the most special of our rocks and minerals.
Soiled Meat
Is this code readable by functional standards? I'm in the middle of writing my first non-trivial project in haskell and I don't know if it's total spaghetti or I'm just not used to reading functional code. I know it would be less clunky if I used a more appropriate data structure but that's what I've got to work with for now.

code:
getGraphFromFile :: String -> [[Int]]
getGraphFromFile text = withoutIndex
  where (header:edges) = map (map read . words) . lines $ text
        numVertices = head header
        empties = foldr (\_ acc -> []:acc) [] [0..numVertices - 1]
        explicitIndex = zip [0..length empties] empties
        addEdge value index acc = (fst . (!! index) $ acc,
                                    (:) value . snd . (!! index) $ acc)
        result = foldr (\(first:(second:_)) acc ->
                        sortBy (\(i, _) (j, _) -> compare i j) .
                        nubBy (\(i, _) (j, _) -> i == j) .
                        (:) (addEdge first second acc) .
                        (:) (addEdge second first acc)
                        $ acc) explicitIndex edges
        withoutIndex = map snd result
If you're curious, it takes a file with a list of edges and turns it into an adjacency list representation of a graph.

e: noticed a mistake right after I posted :v:

Magissima fucked around with this message at 18:01 on Mar 13, 2016

Adbot
ADBOT LOVES YOU

Magissima
Apr 15, 2013

I'd like to introduce you to some of the most special of our rocks and minerals.
Soiled Meat
I think you're right that the problems originate from the data structure. The reason I have it that way is that this is from an assignment that was originally meant to be in java until the professor changed his mind and said we could use whatever, so I just directly took the java data structure and used that. It's obvious to me now that it makes a lot more sense to have a meaningful list index when you're working with loops than with functions, so lesson learned on that front. Thanks for the feedback!

Magissima
Apr 15, 2013

I'd like to introduce you to some of the most special of our rocks and minerals.
Soiled Meat
Whoa, that's way better than what I had. I don't think it even occurred to me to recurse to the correct list instead of manually indexing, but when you put it like that it makes total sense. I guess I'm still not used to thinking functionally. I probably should have come here before spending an hour banging my head against the wall coming up with my lovely solution. Thanks a lot for your help. I'll look into Data.Map as well.

Magissima
Apr 15, 2013

I'd like to introduce you to some of the most special of our rocks and minerals.
Soiled Meat

gonadic io posted:

If we're being pedantic, then getGraphFromFile is a bad name and it should be called getGraphFromString. No files are involved at this stage. :colbert:

It was indeed originally FilePath -> IO [[Int]] but I didn't change the name when I moved the file access to main :effort:

Also I tried out HLint and it seems pretty helpful, thanks for the recommendation.

  • Locked thread