Advertisement
Guest User

Untitled

a guest
Sep 30th, 2011
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (use '[clojure.core.match.core :only [match] ])
  2.  
  3.  
  4. ; this is expected to work
  5.  
  6.  
  7. (defn rpn'[ stack symb ]
  8.     (match [stack symb]
  9.         [ [x y & z ] "*" ] (cons (* x y ) z)
  10.         [ [x y & z ] "+" ] (cons (+ x y ) z)
  11.         [ x "sum" ] [ (reduce + x) ]
  12.         [ x y ] (cons (read-string y) x)
  13.             ))
  14.  
  15. (defn calculator'[ input ]
  16.     (first (reduce rpn' [] (re-seq #"\S+" input))))
  17.  
  18. (calculator' " 1 2 10 2 3 + * sum ")
  19.  
  20.  
  21.  
  22.  
  23. ; like this below, but it doesn't, why?
  24.  
  25.  
  26.  
  27. (defn rpn[ stack symb ]
  28.     (match [stack symb]
  29.         [ x "*" ] (cons (* (first x) (second x) ) (drop 2 x))
  30.         [ x "+" ] (cons (+ (first x) (second x) ) (drop 2 x))
  31.         [ x "sum" ] [ (reduce + x) ]
  32.         [ x y ] (cons (read-string y) x)
  33.             ))
  34.  
  35. (defn calculator[ input ]
  36.     (first (reduce rpn [] (re-seq #"\S+" input))))
  37.  
  38. (calculator " 1 2 10 2 3 + * sum ")
  39.  
  40.  
  41. ; and standard clojure destruction
  42.  
  43. (let [ [ x y & z ] '( 2 3 4 5) ]
  44.     (println x)
  45.     (println y)
  46.     (println z))
  47.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement