
Hi there! I was hoping you could help me a bit here with making a function. I am asked to write functions to make a mini robot game, which uses these data and types: data Program = Move Direction Program |Radar (Robot -> Point) Program |Shoot Point Program |Surrender data Direction = N|S|E|O type Point = (Int,Int) data Robot = R1|R2 And then I have to write functions like this: prog1::Program prog1 = Move E $ Radar R1 (2,4) $ Radar R2 (5,6) $ Shoot (5,7) prog1 My problem is the function adjust::Direction->Program->Program that takes one direction and a program and moves every shot in that program one unit in the given direction. I can't figure out how to do this, I would really appreciate you could help me, thanks!

If you pass a program into this instruction, if it sees a shoot, it will modify it slightly, if it is some other instruction, it will simply leave the instruction unchanged, and then call itself on the rest of the program. Use pattern matching to do this (untested code): adjust :: Direction -> Program -> Direction adjust dir (Shoot point next) = Shoot (adjustPoint dir point) $ adjust dir next where adjustPoint :: Dir -> Point -> Point adjustPoint N (x,y) = (x, y+1) adjustPoint ... = ... adjust dir (Move d next) = Move d $ adjust dir next adjust ... = ... adjust _ (Surrender) = Surrender On Mon, Jun 22, 2015 at 12:08 PM, Bruno Sotelo Klinec < janus_1118@outlook.com> wrote:
Hi there! I was hoping you could help me a bit here with making a function. I am asked to write functions to make a mini robot game, which uses these data and types:
data Program = Move Direction Program |Radar (Robot -> Point) Program |Shoot Point Program |Surrender data Direction = N|S|E|O type Point = (Int,Int) data Robot = R1|R2
And then I have to write functions like this: prog1::Program prog1 = Move E $ Radar R1 (2,4) $ Radar R2 (5,6) $ Shoot (5,7) prog1
My problem is the function adjust::Direction->Program->Program that takes one direction and a program and moves every shot in that program one unit in the given direction. I can't figure out how to do this, I would really appreciate you could help me, thanks!
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

Thanks! I thought pattern matching or case analysis wouldn't work well with functions
participants (2)
-
Bruno Sotelo Klinec
-
David McBride