Advertisement
Guest User

L-System Parser

a guest
Nov 25th, 2014
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- Automation                                                                                                                        
  2.                                                                                                                                      
  3. convertChar :: Char -> String                                                                                                        
  4. convertChar '+' = "p"                                                                                                                
  5. convertChar '-' = "n"                                                                                                                
  6. convertChar '[' = "Branch ("                                                                                                          
  7. convertChar ']' = ")"                                                                                                                
  8. convertChar x   = [x] ++ " (x-1)"                                                                                                    
  9.                                                                                                                                      
  10. convertCharNode :: Char -> String                                                                                                    
  11. convertCharNode '+' = "p"                                                                                                            
  12. convertCharNode '-' = "n"                                                                                                            
  13. convertCharNode '[' = "Branch ("                                                                                                      
  14. convertCharNode ']' = ")"                                                                                                            
  15. convertCharNode x   = [x] ++ " x"                                                                                                    
  16.                                                                                                                                      
  17. format :: [String] -> String                                                                                                          
  18. format strs = foldr1 f strs where                                                                                                    
  19.   f a b | a == "Branch (" || head b == ')' = a ++ b                                                                                  
  20.   f a b =       a ++ " :#: " ++ b                                                                                                    
  21.                                                                                                                                      
  22. autoProgram :: String -> String                                                                                                      
  23. autoProgram = format . (map convertChar)                                                                                              
  24.                                                                                                                                      
  25. autoProgramNode :: String -> String                                                                                                  
  26. autoProgramNode = format . (map convertCharNode)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement