module NHole where data CNCprogram = Header Body data Header = Maybe Comment type Comment = (String) data Maybe a = Nothing | Just a type Body = [Block] data Block = BComment Comment | Code Command type Command = [Instruction] data Instruction = X Int | Y Int | Z Int | G String --data Word = X Int | Y Int | Z Int --type GCode = String -- creates a [Block] containing the CNC instructions for -- making n holes in a straight line nHolesLine :: Int -> Int -> Int -> Int -> Int -> [Block] nHolesLine n x y incX incY = [posType,initMov] ++ concat (map makeHole nLine) where line = createLine x y incX incY nLine = finiteLine n line posType = Code [G "90"] initMov = Code [G "00",Z 5] -- creates an infinite list of absolute coordinates createLine :: Int -> Int -> Int -> Int -> [(Int,Int)] createLine x y incX incY = (x,y) : createLine (x + incX) (y + incY) incX incY ---takes an list and returns a sublist containing the first n elements finiteLine :: Int -> [a] -> [a] finiteLine _ [] = [] finiteLine 0 _ = [] finiteLine n (x:xs) = x : finiteLine (n-1) xs -- takes a XY coordinate and return a block containing -- all instructions needeed to make a hole at such position makeHole :: (Int,Int) -> [Block] makeHole (x,y) = [posXY,down,up] where up = Code [Z 5] down = Code [Z (-5)] posXY = Code [X x,Y y] -- takes an initial numbering and a list of blocks -- and print them through the output console printBlocks :: Int -> [Block] -> IO () printBlocks _ [] = putStrLn "" printBlocks numBlock (x:xs) = do printBlock numBlock x printBlocks (numBlock+10) xs -- takes a number and a block and block and prints it printBlock :: Int -> Block -> IO () printBlock numBlock block = putStrLn ("N"++(show numBlock)++"\t"++(blockToStr block)) -- converts a block to a String blockToStr :: Block -> String blockToStr (BComment c) = "("++c++")" blockToStr (Code []) = "" blockToStr (Code (x:xs)) = (instrToStr x) ++ " " ++ (blockToStr (Code xs)) -- converts an instruction to a String instrToStr :: Instruction -> String instrToStr (X n) = "X"++(show n) instrToStr (Y n) = "Y"++(show n) instrToStr (Z n) = "Z"++(show n) instrToStr (G code) = "G"++code main = printBlocks 10 (nHolesLine 10 10 10 5 5)