Serving the Quantitative Finance Community

 
User avatar
Cuchulainn
Topic Author
Posts: 64081
Joined: July 16th, 2004, 7:38 am
Location: Drosophila melanogaster
Contact:

Quizzies, Brainteasers: Functional Programming in C++

June 29th, 2018, 7:00 pm

Quiz
Consider a scalar-valued function of arity n [$]f = f(x_1,... ,x_n)[$] From this function we want to fix [$]n-1[$] arbitrary arguments leaving one argument free as it were, resulting in a (higher-order) function of arity 1.. The idea is to cycle over each argument from 1 to n, thereby creating a function of arity 1.

ex
f(x,y,z)

g1(x) = f(x, 2.0, 1.0)
g2(y) = f(2.0, y, 1.0)
g3(z) = f(2.0, 2.0, z)

Can it be done?

I bet Mathematica or Haskell yes?
 
User avatar
ppauper
Posts: 70239
Joined: November 15th, 2001, 1:29 pm

Re: Quizzies, Brainteasers: Functional Programming in C++

June 29th, 2018, 7:30 pm

I'm not sure what you're trying to do.
If the goal is to do what you did in the example, then didn't you already do it?
 
User avatar
Cuchulainn
Topic Author
Posts: 64081
Joined: July 16th, 2004, 7:38 am
Location: Drosophila melanogaster
Contact:

Re: Quizzies, Brainteasers: Functional Programming in C++

June 29th, 2018, 8:09 pm

I'm not sure what you're trying to do.
If the goal is to do what you did in the example, then didn't you already do it?
But i want to do it for any function of any arity n = 1,2,3,...
The compile-time example was only an example to show the intent.

At run-time I want to do it for a variable number of arguments that is only known at run-time.
 
User avatar
ppauper
Posts: 70239
Joined: November 15th, 2001, 1:29 pm

Re: Quizzies, Brainteasers: Functional Programming in C++

June 29th, 2018, 9:31 pm

       THIS?

       IMPLICIT DOUBLE PRECISION (A-H,L,O-Z)
       IMPLICIT INTEGER (I-K,M-N)
       PARAMETER (N=20)
       DIMENSION G(N),X(N),Y(N),X0(N)
       DO 110 J=1,N
       Y(J)=X0(J)
110 CONTINUE     
       DO 100 I=1,N
       Y(I)=X(I)
       G(I)=F(Y)
       Y(I)=X0(I)
100 CONTINUE
       END
    
       DOUBLE PRECISION FUNCTION F(X)
       IMPLICIT DOUBLE PRECISION (A-H,L,O-Z)
       IMPLICIT INTEGER (I-K,M-N)
       DIMENSION X(*)
       F = DSIN(X(1)) 
      RETURN
      END
 
User avatar
Cuchulainn
Topic Author
Posts: 64081
Joined: July 16th, 2004, 7:38 am
Location: Drosophila melanogaster
Contact:

Re: Quizzies, Brainteasers: Functional Programming in C++

June 10th, 2020, 6:57 pm

is this bad practice in C++
int netbuf[4* 1000000];
ABOUT WILMOTT

Wilmott

Wilmott.com has been "Serving the Quantitative Finance Community" since 2001. Continued...


Twitter LinkedIn Instagram

JOBS BOARD

JOBS BOARD

Looking for a quant job, risk, algo trading,...? Browse jobs here...


GZIP: On