PDA

View Full Version : returning a Function (something like a pointer)?


Unknownz
09-18-2003, 09:21 AM
Hello !
I am wondering how to return a function in a function. I want to do this to be able to return a operator.

The result I would like to achieve is something like this:

returnOperator::Char->(here i want to return a "pointer" to a operator)
returnOperator chr
| chr == '*' = (now I want to return the multiply operator)

chooseOperation::Int->Int->Char->Int
chooseOperation val1 val2 op = val1 (returnOperator op) val2


Would be very greatfull if someone could help me out or at least give me some clue on what to look for.

Thx!

Strike
09-18-2003, 11:01 AM
returnOperator chr
= case chr of
'*' -> (*)
'+' -> (+)
_ -> (-)


And in use:


*Operator> (returnOperator '+') 2 5
7
*Operator> (returnOperator '*') 4 10
40

Smerdyakov
09-18-2003, 06:58 PM
This question is suspicious because no one who had read a Haskell tutorial would be asking it. I don't think Strike should have answered it. Are you sure you're not hurting yourself by not reading pre-written material, Unknownz?