Common Lisp: Definitions and Datatypes
Defining functions in Python and Lisp
from math import sqrt def quadratic_roots(a, b, c): return ( (-b + sqrt(b**2 - 4 * a * c)) / (2 * a), (-b - sqrt(b**2 - 4 * a * c)) / (2 * a), ) quadratic_roots(1, 3, 2)
(-1.0, -2.0)
(defun quadratic-roots (a b c) (list (/ (+ (- b) (sqrt (- (* b b) (* 4 a c)))) (* 2 a)) (/ (- (- b) (sqrt (- (* b b) (* 4 a c)))) (* 2 a)))) (quadratic-roots 1 3 2)
(-1.0 -2.0)
from cmath import sqrt def quadratic_roots(a, b, c): discriminant = b**2 - 4 * a * c return ( (-b + sqrt(discriminant)) / (2 * a), (-b - sqrt(discriminant)) / (2 * a), ) quadratic_roots(1, 3, 20)
((-1.5+4.2130748865881795j), (-1.5-4.2130748865881795j))
(defun quadratic-roots (a b c) (let ((discriminant (- (* b b) (* 4 a c)))) (list (/ (+ (- b) (sqrt discriminant)) (* 2 a)) (/ (- (- b) (sqrt discriminant)) (* 2 a))))) (quadratic-roots 1 3 20)
(#C(-1.5 4.2130747) #C(-1.5 -4.2130747))
(defun quadratic-roots (a b c) (let ((discriminant (- (* b b) (* 4 a c)))) (values (/ (+ (- b) (sqrt discriminant)) (* 2 a)) (/ (- (- b) (sqrt discriminant)) (* 2 a))))) (quadratic-roots 1 3 20)
#C(-1.5 4.2130747) #C(-1.5 -4.2130747)
The Common Lisp language
The Common Lisp Cookbook
Much of the material below is adapted from The Common Lisp Cookbook under its license
Data types
- Numbers
- Strings
- "this is a string"
- Escape using backslash as usual
- There are no single-quoted strings or triple-quoted strings as in Python.
- Symbols
A Lisp type that Python does not have is symbol. You can consider a symbol to be a word or a tag. To write a symbol, begin with a quote
'. Note that Lisp converts a symbol to upper case (by default) and that it is case insensitive.CL-USER> 'landmark LANDMARK CL-USER> 'aBcDeFg ABCDEFG CL-USER>
- Other types
etc
- Jotter
- What's the carbon footprint of using ChatGPT?
Using ChatGPT is not bad for the environment
Sitting down to watch 1 hour of Netflix has the same impact on the climate as asking ChatGPT 100 questions.
The agreed-on best guess right now for the average chatbot prompt’s energy cost is actually the same as a Google search in 2009: 0.8 Wh including the cost of the answering your prompt, idling AI chips between propmts, cooling in the data center, and other energy costs in the data center.
0.8 Wh is enough to:
- Stream a video for 35 seconds
- Watch an LED TV (no sound) for 50 seconds
- Upload 9 photos to social media
- Drive a car at a consistent speed for 1.2 metres
- Leave your digital clock on for 50 minutes
- Run a space heater for 0.7 seconds
- Print a fifth of a page of a physical book
- Spend 1 minute reading this blog post. If you’re reading this on a laptop and spend 20 minutes reading the full post, you will have used as much energy as 20 ChatGPT prompts. ChatGPT could write this blog post using less energy than you use to read it!
- A short summary of my argument that using ChatGPT isn't bad for the environment