haskell-scheme icon indicating copy to clipboard operation
haskell-scheme copied to clipboard

Add float support.

Open whagsv opened this issue 5 years ago • 1 comments
trafficstars

Adds a new type for which numeric operations mixing ints/floats are defined. Sure could use some refactoring but fuck me if I know why ghc would not let me.

Quite ad hoc. Ought to be a nicer way to do this. Something something typeclasses?

whagsv avatar Aug 10 '20 14:08 whagsv

You could add something like

numValToFloat :: NumVal -> Float
numValToFloat (Float f) = f
numValToFloat (Integer i) = fromInteger i

to make all the other functions a lot shorter, e.g.

(+) (Integer a) (Integer b) = Integer $ a+b
(+) a b = Float $ (numValToFloat a)+(numValToFloat b)

also note that you can define infix operators without writing them prefix, like so:

Integer a + Integer b = Integer $ a + b
a + b = Float $ numValToFloat a + numValToFloat b

finally, adding tests would probably be a good idea ^^

steffahn avatar Aug 13 '20 13:08 steffahn