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 of show. 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

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -