JavaScriptの例を下記に示しますが、
「odd=not(even)」で関数の処理内容自体を変換することが可能です。
これを単純にCに書き換えられないか
試みましたが、「not」の書き方で
躓いてしまいました。
書きかけのCのコードを
JavaScriptの例の下側に示します。
「not()」の部分の記述は仮のものです。
そもそも、この処理は
Cで書けるのでしょうか?
御教授下さい。
lang
1// JavaScriptの例 2 3importPackage(java.lang) 4print=function(x){ 5 System.out.print(x+' ')} 6 7not=function(predicate) 8{ 9 return function(arg) 10 { 11 return!predicate(arg) 12 } 13} 14 15multipleOf=function(n) 16{ 17 return function(m) 18 { 19 if(m%n===0) 20 { 21 return true 22 } 23 else 24 { 25 return false 26 } 27 } 28} 29 30even=multipleOf(2) 31odd=not(even) 32 33print(odd(2)) 34print(odd(3))
lang
1// C言語による書き換えを試みた途中結果 2 3#include<stdio.h> 4 5#define FALSE 0 6#define TRUE !FALSE 7 8void print(int x){printf("%d ",x);} 9 10int _n; 11int _multipleOf(int m) 12{ 13 if(m%_n==0) 14 { 15 return TRUE; 16 } 17 else 18 { 19 return FALSE; 20 } 21} 22 23int(*multipleOf(int n))() 24{ 25 _n=n; 26 return _multipleOf; 27} 28 29int (*not(int(*f)()))() 30{ 31 // 変換せずに返すので「 even()」のまま 32 return f; 33} 34 35int main() 36{ 37 int(*even)(),(*odd)(); 38 39 even=multipleOf(2); 40 odd=not(even); 41 42 print(odd(2)); 43 print(odd(3)); 44}
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。