; #lang at-exp racket #lang racket ; Implementation of a Wolfram Blog article at http://bit.ly/HWdUqK ; for doing infix arithmetic for evaluating strings which contain numbers and operators (require (planet soegaard/infix) (planet soegaard/infix/parser)) ; (display (format "1+2*3 is ~a\n" @\${1+2*3} )) ; (parse-expression #'here (open-input-string "1+2*3")) ; (define x (parse-expression #'here (open-input-string "10*9*8*7/6"))) ; (eval x) ; Need to convert operators to operator TUPLES of 9 ; opTuples = Tuples (operators, 9) ;; product : (listof a) (listof b) -> (listof (cons a b)) (define (product lst1 lst2) (for*/list ([x (in-list lst1)] [y (in-list lst2)]) (cons x y))) ;; tuples : (listof a) natural -> (listof (listof a)) (define (tuples lst n) (cond [(= n 0) (list empty)] [else (product lst (tuples lst (- n 1)))])) ; We need to riffle shuffle the list of numbers (10) and operators (9) ; The new riffled lists has to be converted into string ; USE DERANGEMENT OF THE LISTS - so that all the permutations are figured out ; Crude and inefficient version of interleave, but it works for me! (define (inter2 l1 l2) ( if (null? l2) (first l1) (string-append (first l1) (first l2) (inter2 (rest l1) (rest l2))) ) ) (define (riffle ml1 ml2) ( if (null? ml2) '() (cons (inter2 ml1 (first ml2)) (riffle ml1 (rest ml2))) ) ) ; The strings obtained as result of riffling have to be evaluated using infix arithmetic (define (expVal s) (eval (parse-expression #'here (open-input-string s)))) ; Filter out the string expressions which evaluate to "year" value ; 'rfList' is the comprehensive list of strings which needs to be evaluated ;________________________________________________ ;The main definition that needs improvement? ; ; expListY - list of string expressions that evaluated to year Y in value ; ;________________________________________________ (define (expListY rfList year) (if (null? rfList) '() (if (equal? year (expVal (first rfList))) (cons (first rfList) (expListY (rest rfList) year)) (expListY (rest rfList) year) ) ) ) (define numbers '("10" "9" "8" "7" "6" "5" "4" "3" "2" "1")) (define operators '("*" "/" "+" "-" "")) ;________________________________________________ ; The main section of the code ; tuList : Combination list of tuples of 9 operators ; samRF : Total combination of strings generated by riffling 10 descending numbers list and tuples operators list ; expListY : Expressions that match the value of current year (2012) ; You will need to comment out all the lines upto --END OF PROGRAM-- if you want to do some quick dirty testing using sample code provided below that line (define tuList (tuples operators 9)) (define samRF (riffle numbers tuList)) (expListY samRF 2012) ; -------- END OF PROGRAM ----- ; Testing code - which can be ignored if you are trying to optimize the above code ; ; Test cases for running the program with list of numbers and smaller list of operators - 3 ; (define ttuList (tuples operators 3)) (define ssamRF (riffle numbers ttuList)) ;Some sample tests before going for the big one ;Below, we are using only 3 operators and then testing for which combination evaluates to 196 and 1105 (expListY ssamRF 196) (expListY ssamRF 1105)