Link 24 Feb 5 notes Rising»

This is why Lisp is better, compare.

CLISP:

(setq tuples ())                                                                   
(do ((n (read *standard-input* nil) (read *standard-input* nil))                   
     (k (read *standard-input* nil) (read *standard-input* nil))) ((null n))       
     (setq tuples (append tuples (cons (list n k) nil))))                          
(sort tuples #'< :key                                                              
   #'(lambda (x) (cond ((eq (nth 1 x) 'K) (+ (* (- (car x) 273.15) 1000) 2))       
                       ((eq (nth 1 x) 'F) (+ (* (/ 5 9) (- (car x) 32) 1000) 1))   
                       (t (* (car x) 1000)))))                                     
(loop for x in tuples                                                              
  do (format t "~A ~A~%" (car x) (nth 1 x)))   

C++:

#include
#include

using namespace std; 

typedef struct 
{
   int t;
   char c;
} tuple ;

double sortValue (int temp, char c)
{
   switch(c)
   {
      case 'C': return (temp*1.8+32)*1000; break;
      case 'F': return (temp)*1000+1; break;
      default: return (temp*1.8-459.67)*1000+2;
   }
}

class mycomparison
{
public:
  mycomparison(){};
  bool operator() (tuple &lhs, tuple &rhs) { return (sortValue(rhs.t, rhs.c) < sortValue(lhs.t, lhs.c)); }
};

int main ()
{
   priority_queue, mycomparison> queue;
   int temp;
   char ch;
   while ( fscanf(stdin, "%d %c", &temp, &ch) != EOF)
   {
      tuple cont;
      cont.t = temp;
      cont.c = ch;
      queue.push(cont);
   }
   int stop = queue.size();
   for(int i = 0; i < stop; i++)
   {
      tuple var = queue.top();
		printf("%d %c\n", var.t, var.c);
      queue.pop();
   }
   return 0;
}
  1. pafluxa reblogged this from fuckyeahterminals and added:
    OK, now compare execution time. Also, CLIPS is
  2. tyb reblogged this from fuckyeahterminals
  3. fuckyeahterminals posted this

Design crafted by Prashanth Kamalakanthan. Powered by Tumblr.