Add Haskell port

This commit is contained in:
Sven Heyll committed 2015-02-24 05:11:15 +01:00
1 parent 64277b0219
commit 391e5c2e98
10 files changed
+214

No files matched your search

+70
View File
@@ -0,0 +1,70 @@
module GildedRose where
type GildedRose = [Item]
data Item = Item String Int Int
deriving (Eq)
instance Show Item where
show (Item name sellIn quality) =
name ++ ", " ++ show sellIn ++ ", " ++ show quality
updateQuality :: GildedRose -> GildedRose
updateQuality = map updateQualityItem
where
updateQualityItem (Item name sellIn quality) =
let
quality' =
if name /= "Aged Brie"
&& name /= "Backstage passes to a TAFKAL80ETC concert"
then
if quality > 0
then
if name /= "Sulfuras, Hand of Ragnaros"
then quality - 1
else quality
else quality
else
if quality < 50
then
quality + 1 +
(if name == "Backstage passes to a TAFKAL80ETC concert"
then
if sellIn < 11
then
if quality < 49
then
1 + (if sellIn < 6
then
if quality < 48
then 1
else 0
else 0)
else 0
else 0
else 0)
else quality
sellIn' =
if name /= "Sulfuras, Hand of Ragnaros"
then sellIn - 1
else sellIn
in
if sellIn' < 0
then
if name /= "Aged Brie"
then
if name /= "Backstage passes to a TAFKAL80ETC concert"
then
if quality' > 0
then
if name /= "Sulfuras, Hand of Ragnaros"
then (Item name sellIn' (quality' - 1))
else (Item name sellIn' quality')
else (Item name sellIn' quality')
else (Item name sellIn' (quality' - quality'))
else
if quality' < 50
then (Item name sellIn' (quality' + 1))
else (Item name sellIn' quality')
else (Item name sellIn' quality')
+44
View File
@@ -0,0 +1,44 @@
module Main where
import System.Environment
import GildedRose
main :: IO ()
main = do
putStrLn "OMGHAI!"
let inventoriesWithDay = inventories `zip` [0..]
inventories = iterate updateQuality initialInventory
numberOfDays <- daysFromArg
mapM_ printUpdate (take numberOfDays inventoriesWithDay)
where
printUpdate :: (GildedRose, Int) -> IO ()
printUpdate (items, day) = do
putStrLn ("-------- day " ++ show day ++ " --------")
putStrLn "name, sellIn, quality"
mapM_ (putStrLn . show) items
putStrLn ""
daysFromArg :: IO Int
daysFromArg = do
args <- getArgs
return $ if length args > 0
then read (head args)
else 20
initialInventory :: GildedRose
initialInventory =
[ Item "+5 Dexterity Vest" 10 20
, Item "Aged Brie" 2 0
, Item "Elixir of the Mongoose" 5 7
, Item "Sulfuras, Hand of Ragnaros" 0 80
, Item "Sulfuras, Hand of Ragnaros" (-1) 80
, Item "Backstage passes to a TAFKAL80ETC concert" 15 20
, Item "Backstage passes to a TAFKAL80ETC concert" 10 49
, Item "Backstage passes to a TAFKAL80ETC concert" 5 49
-- this conjured item does not work properly yet
, Item "Conjured Mana Cake" 3 6
]