forked from charleso/haskell-in-haste
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCalculator.hs
94 lines (80 loc) · 1.98 KB
/
Calculator.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
module Chat.Bot.Calculator where
import Chat.Bot.Answer.Calculator
import Chat.Bot.Misc.Calculator
import Chat.Data
{-
LEVEL: Easy
USAGE: /calculate a operation b
Simple calculator to add, minus or multiply two numbers
EXAMPLE:
> /calculate 2 + 5
7
> /calculate 10 - 2
8
> /calculate 3 * 5
15
> /calculate 8 / 3
Invalid calculation
-}
-- |
--
-- This method makes the calculation itself
-- Based on operation return a result of calculation or an error if operaiton is not supported
--
-- >>> calculate Plus 2 5
-- 7
-- >>> calculate Minus 5 2
-- 3
-- >>> calculate Multiply 2 5
-- 10
--
calculate :: Operation -> Int -> Int -> Int
calculate op a b =
notImplemented "Calculator.calculate" (calculateAnswer op a b)
-- |
--
-- >>> operationParser "2 + 5"
-- JustCalculation Plus 2 5
-- >>> operationParser "5 - 2"
-- JustCalculation Minus 5 2
-- >>> operationParser "2 * 5"
-- JustCalculation Multiply 2 5
-- >>> operationParser "2 / 5"
-- NothingCalculation
--
-- HINT:
-- words :: String -> [String]
-- read :: String -> Int
-- Try pattern matching on lists. :)
--
operationParser :: String -> MaybeCalculation
operationParser input =
notImplemented "Calculator.operationParser" operationParserAnswer input
-- |
--
-- This is a main method you invoke to calculate the expression
--
-- Here you will parse your operation and if it is successfull you will make a calculation
--
-- >>> calculateParse "2 + 5"
-- "7"
-- >>> calculateParse "5 - 2"
-- "3"
-- >>> calculateParse "2 * 5"
-- "10"
-- >>> calculateParse "2 / 5"
-- "Invalid calculation"
--
-- HINT:
-- show :: Int -> String
--
calculateParse :: String -> String
calculateParse input =
notImplemented "Calculator.calculateParse" (calculateParseAnswer input)
-- See Misc/Calculator.hs
{-
data Operation = Plus | Minus | Multiply
data MaybeCalculation = JustCalculation Operation Int Int | NothingCalculation
-}