turning a value into an expression

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

turning a value into an expression

John Moore
Hi,
   How do I turn a value into an expression
I want to do for e.g. 8 - 1 turn it into (subtract (Val8) (Val1)

Any ideas

J
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/beginners/attachments/20091109/973318cf/attachment.html
Reply | Threaded
Open this post in threaded view
|

turning a value into an expression

Deniz Dogan-3
2009/11/9 John Moore <[hidden email]>:

> Hi,
> ?? How do I turn a value into an expression
> I want to do for e.g. 8 - 1 turn it into (subtract (Val8) (Val1)
>
> Any ideas
>
> J
> _______________________________________________
> Beginners mailing list
> [hidden email]
> http://www.haskell.org/mailman/listinfo/beginners
>
>

import Prelude hiding ((-))

data Val a = Val a
  deriving Show

data Expr a b = Subtract a b
  deriving Show

(-) :: Num a => a -> a -> Expr (Val a) (Val a)
x - y = Subtract (Val x) (Val y)



> 4 - 3
Subtract (Val 4) (Val 3)

--
Deniz Dogan
Reply | Threaded
Open this post in threaded view
|

turning a value into an expression

Brent Yorgey-2
In reply to this post by John Moore
On Mon, Nov 09, 2009 at 10:05:43PM +0000, John Moore wrote:
> Hi,
>    How do I turn a value into an expression
> I want to do for e.g. 8 - 1 turn it into (subtract (Val8) (Val1)
>
> Any ideas

Is this a homework problem?

One good approach would be to make a data type Expr which represents
expressions.  It will have a constructor Val, a constructor Subtract,
etc., one constructor for each operation you want to have in your
expressions.  Then make Expr an instance of the Num type class.

-Brent