码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2016-12-03 14:36:10      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:解释   思想   c语言实现   style   程序   main   scheme   构造   str   

习题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实现的对比

标签:解释   思想   c语言实现   style   程序   main   scheme   构造   str   

原文地址:http://www.cnblogs.com/everydaykeepgoing/p/6128320.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!