[Snark] [Haskell] Deeply nested bowling

Jonathan Lamothe submissions at badcode.rocks
Sat Feb 9 18:40:08 UTC 2019


Only found out about this at DevHouse. I hope I'm not too late. It's the
worst I could do in this short amount of time.

== License ==

As much as I hate to admit it, this program was written by Jonathan
Lamothe, and is licensed under CC-BY-SA.

== bowling.hs ==

import System.Environment
import System.Exit

main = do
  args <- getArgs
  if (length (args) == 0)
    then do
      exitWith (ExitFailure 3)
    else do
      check_loop (1, length (args), args)
      val <- loop (1, length (args), args, 1, False)
      putStrLn (show val)
      exitWith (ExitSuccess)

check_loop (i, len, args) = do
  if (length (args) >= i)
    then do
      make_num (args !! (i - 1))
      next_i <- return (i + 1)
      check_loop (next_i, length (args), args)
    else do
      return ()

loop (i, len, args, frame, second_throw) = do
  if (i > length (args))
    then do
      exitWith (ExitFailure 3)
    else do
      x_string <- return (args !! (i - 1))
      x <- make_num (x_string)
      if (x > 10)
        then do
          exitWith (ExitFailure 1)
        else do
          if (second_throw)
            then do
              y_string <- return (args !! (i - 2))
              y <- make_num (y_string)
              z <- return (x + y)
              if (z > 10)
                then do
                  exitWith (ExitFailure 1)
                else do
                  if (z == 10)
                    then do
                      if (length (args) < i + 1)
                        then do
                          exitWith (ExitFailure 3)
                        else do
                          z_string <- return (args !! i)
                          z <- make_num (z_string)
                          if (frame == 10)
                            then do
                              if (length (args) == i + 1)
                                then do
                                  return (x + y + z)
                                else do
                                  if (length (args) < i + 1)
                                    then do
                                      exitWith (ExitFailure 3)
                                    else do
                                      exitWith (ExitFailure 2)
                            else do -- spare, not last frame
                              next_i <- return (i + 1)
                              next_frame <- return (frame + 1)
                              zz <- loop (next_i, length (args), args, next_frame, False)
                              return (x + y + z + zz)
                    else do -- 2nd th4ow, not spare
                      if (frame == 10)
                        then do
                          if (length (args) == i)
                            then do
                              return (x + y)
                            else do
                              if (length (args) < i)
                                then do
                                  exitWith (ExitFailure 3)
                                else do
                                  exitWith (ExitFailure 2)
                        else do -- 2ns, not spare last frame
                          next_i <- return (i + 1)
                          next_frame <- return (frame + 1)
                          z <- loop (next_i, length (args), args, next_frame, False)
                          return (x + y + z)
            else do -- 1st throw
              if (x == 10)
                then do
                  if (frame == 10)
                    then do
                      if (length (args) == i + 2)
                        then do
                          y_string <- return (args !! i)
                          y <- make_num (y_string)
                          z_string <- return (args !! (i + 1))
                          z <- make_num (z_string)
                          if (y == 10)
                            then do
                              return (x + y + z)
                            else do
                              if (y + z > 10)
                                then do
                                  exitWith (ExitFailure 1)
                                else do
                                  return (x + y + z)
                        else do -- strike, last, wrong throws
                          if (length (args) < i + 2)
                            then do
                              exitWith (ExitFailure 3)
                            else do
                              exitWith (ExitFailure 2)
                    else do -- strike, not last frame
                      if (length (args) < i + 2)
                        then do
                          exitWith (ExitFailure 3)
                        else do
                          y_string <- return (args !! i)
                          y <- make_num (y_string)
                          z_string <- return (args !! (i + 1))
                          z <- make_num (z_string)
                          if (y + z > 10 && y /= 10)
                            then do
                              exitWith (ExitFailure 1)
                            else do
                              next_i <- return (i + 1)
                              next_frame <- return (frame + 1)
                              zz <- loop (next_i, length (args), args, next_frame, False)
                              return (x + y + z + zz)
                else do -- not strike
                  next_i <- return (i + 1)
                  loop (next_i, length (args), args, frame, True)

make_num (string) = do
  case (string) of
    "0" -> return (0)
    "1" -> return (1)
    "2" -> return (2)
    "3" -> return (3)
    "4" -> return (4)
    "5" -> return (5)
    "6" -> return (6)
    "7" -> return (7)
    "8" -> return (8)
    "9" -> return (9)
    "10" -> return (10)
    _ -> exitWith (ExitFailure 1)


More information about the Snark mailing list