首页 > 代码库 > scip习题(1) scheme和c实现的对比

scip习题(1) scheme和c实现的对比

习题1.3

Define a procedure thats three numbers as argument and return the sum of the square of two large number.

scheme实现

(define (square x)
  (* x x))
(define (sum_of_square x y)
  (+ (square x)
     (square y)))
(define (bigger x y)
  (if (> x y)
      x
      y))
(define (smaller x y)
  (if (< x y)
      x
      y))
(define (sum_square x y z)
  (sum_of_square (bigger x y)
                 (bigger (smaller x y) z)))
                 


        
      
        

c语言实现

//用C语言实现scip(计算机程序构造与解释)的一些习题。对比并进一步了解scheme的函数式思想
//Define a procedure thats three numbers as argument and return the sum of the square of two large 
//numbers.
//C language implementation

#include "stdafx.h"
#include<iostream>
using namespace std;

int square(int x);
int sum_of_square(int a, int b);
int bigger_num(int a, int b);
int two_more_bigger_sum_of_square(int a, int b, int c);
int smaller_num(int a, int b);

//check the code
int main()
{
    cout<<two_more_bigger_sum_of_square(1,2,3);
    return 0;
}
int square(int x) {
    return x*x;
}

//retun the sum of the num of square
int sum_of_square(int a, int b) {
    return square(a)+square(b);

}

//return the bigger num
int bigger_num(int a, int b) {
    if (a > b)
        return a;
    else
        return b;
}

//takes the two num as agrument and return the smaller num
int smaller_num(int a, int b) { //return the bigger num
    if (a > b)
        return b;
    else
        return a;
}

 

scip习题(1) scheme和c实现的对比