haskell - Show basic arithmetic functions as string -
for homework assignment, subtask make arithmetic functions (+)
, (-)
, (*)
, div
showable.
we're solved rest of assignment, we're stuck here. right we're using the solution question here distinguish between operations:
showop op = case op 3 3 of 6 -> "plus" 0 -> "minus" 9 -> "times" 1 -> "divide" _ -> "undefined"
however, strikes me kind of ugly things showop (\a b -> * 3 - y)
yield "plus"
.
is there way better distinguish between operators?
we using winhugs atm appropriate switches -98 +o
in order able use needed extensions.
edit: requested, actual assignment has arrays (specifically array int (int -> int -> int)
). has generating arrays of operators fulfill conditions.
the assignment states:
make data type
array int (int->int-int)
instance ofshow
. arithmetic operations previous exercises should represented "plus", "minus", "times" , "div".
thx in advance
use induction :)
{-# language flexibleinstances #-} instance eq (int-> int -> int) f == g = induce f g base = 1 n = 2 induce f g = , [f 1 n' == g 1 n' | n' <- [base, n, n+1]] instance show (int-> int -> int) show = showop showop op = case lookup op ops of -> otherwise -> "undefined" ops = [((+),"plus") ,((-),"minus") ,((*),"times") ,(div,"divide")]
output:
*main> (\a b -> * 3 - b) :: (int->int->int) undefined
Comments
Post a Comment