Part 4 — A complete beginner’s guide to Computer Programming with Clojure: Maths.

Photo by Jeswin Thomas on Unsplash

Polish notation

(+ 5 5) ;; addition, 5 + 5.=> 10
(- 20 10)
;; subtraction, 20 -10.
=> 10
(* 5 2) ;; multiplication, 5 x 2.=> 10
(/ 50 5) ;; division, 50 divided by 5.=> 10
(mod 24 14)
;; modulo, 24 mod 14.
=> 10
(inc 9) ;; increment, 9 + 1.=> 10
(dec 11) ;; decrement, 11 -1.
=> 10
(Math/pow 2 4)
;; 2 to the power of 4, 2 x 2 x 2 x 2.
=> 16.0
(Math/sqrt 100)
;; Square root of 100.
=> 10
(Math/cbrt 1000)
;; Cube root of 1000.
=> 10
(Math/hypot 6 8) ;; Hypotenuse for a right angled triangle
=> 10

Square Root Calculator App

Listing 1.

(import ‘(javax.swing JFrame JLabel JTextField JButton)‘(java.awt.event ActionListener)‘(java.awt GridLayout))((let [frame (JFrame. “Square Root Calculator”)input-text (JTextField.)inputNo-label (JLabel. “Input Number”)square-button (JButton. “Find Sqr Root”)result-label (JLabel. “result”)](. square-button(addActionListener(proxy [ActionListener] [ ](actionPerformed [evt](let [i (Double/parseDouble (. input-text (getText)))](. result-label(setText (str (Math/sqrt i) “ is the square root”))))))))(doto frame(.setLayout (new GridLayout 2 2 3 3))(.add input-text)(.add inputNo-label)(.add square-button)(.add result-label)(.setSize 400 100)(.setVisible true))))

Listing 1. High-Level Overview

Listing 1. In-depth explanation

SUMMARY

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store