Altering A Single Value in List of Lists in Haskell [Homework] -


been having real issues , haven't been able find guidance on doing in reading. have been tasked implementing functions complete haskell version of connect 4. board represented list of lists of pieces using data.list.

one of functions drop piece given piece , column number. add piece appropriate column , done way seem able recursing through list until right column , add piece.

is there way better?

my horrendous code below:

cheatpiece :: gamestate -> int -> piece -> gamestate cheatpiece [] _ _ = [] cheatpiece (xs:xss) 0 x = (x:xs) : xss cheatpiece (xs:xss) n x = xs : cheatpiece xss (n-1) x 

i don't think implementation horrendous @ all. that's pretty standard way work immutable, linked lists.

i think main thing makes feel clumsy working indices , linked lists never going natural.

so, in context of homework assignment, implementation is, think, correct way implement cheatpiece. if had control on board presentation might consider using, example, vector or intmap store columns.

there's lens lets work nested, immutable structures using terser abstractions if still new haskell lens package not have gentlest of learning curves.

import control.lens  data piece = x | o deriving show type gamestate = [[piece]]  cheatpiece :: gamestate -> int -> piece -> gamestate cheatpiece st p = st & ix %~ (p:) 

Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -