Advertisement
Guest User

Jim Downing

a guest
Sep 4th, 2009
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 1.54 KB | None | 0 0
  1. ; Comments
  2.  
  3. ; Obligatory
  4. (println "Hello, JISCRIans!")
  5.  
  6. ; Numbers
  7. 25
  8. 1.0
  9. 22/7
  10.  
  11. ; Strings
  12. "eggs"
  13.  
  14. ; Keywords evaluate to themselves
  15. :this
  16. :that
  17.  
  18. ; Everything else is a "symbol", and represents something else
  19. ;
  20. ; Lists, by default evaluate as function calls
  21. (+ 5 5)
  22. ; N.B. + is not an operator, just a symbol that represents a function
  23. ; use ' to prevent evaluation
  24. '("pig" "cow" "aufschnitt")
  25.  
  26. ; Vectors
  27. [1 2 3 4 5]
  28. ["eggs" "bacon" "black pudding"]
  29.  
  30. ; Maps (commas are considered to be whitespace)
  31. {"fred" 5, "bill" 10}
  32. ; Keywords useful here, and used to define structs
  33. {:id 3, :name "Jack"}
  34. ; Maps are functions of their keys, and keywords are functions of any map
  35. (:name {:id 3, :name "Jack"})
  36. ({:id 3, :name "Jack"} :id)
  37.  
  38. ; Sets
  39. #{"this" "that" "the other"}
  40. #{"this" "that" "the other" "this"}
  41.  
  42. ; You've now seen pretty much all of the syntax
  43.  
  44. ; Defining things (creating a symbol to refer to them)
  45. (def jack {:id 3 :name "Jack"})
  46. (println jack)
  47. (:name jack)
  48.  
  49. ; Functions
  50. (fn [person]
  51.    (:name person))
  52. ((fn [person]
  53.    (:name person)) jack)
  54.  
  55.  
  56. (defn get-name [person]
  57.    (:name person))
  58. (get-name jack)
  59.  
  60. ; Java interop
  61. (def sb (StringBuilder. "Quo Vadis?"))
  62. (.reverse sb)
  63. ; or
  64. (. sb reverse)
  65. ; Chaining methods with ..
  66. (.. sb reverse toString toUpperCase)
  67.  
  68. (doto (new java.util.HashMap)
  69.     (.put "a" 1)
  70.     (.put "b" 2))
  71.  
  72.  
  73. ; Exception handling
  74. (def f (java.io.File. "not there"))
  75. (try
  76.     (java.io.FileInputStream. f)
  77.     (catch java.io.FileNotFoundException _ (println "Didn't work!")))
  78.  
  79.  
  80.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement