I am new to elisp and am trying to write a simple program using an if statement.
The point is to read in a year of school and return either 1, 2, 3, 4, or 0 for the strings "freshman", "sophomore", "junior", "senior" respectively (defaulting to 0 when no match is made. My code is as follows:
(defun yearCode(name)
(if ( = name "freshman") 1
(if ( = name "sophomore") 2
(if ( = name "junior") 3
(if ( = name "senior") 4
(0))))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; main function ;;;
;;; input: year ;;;
;;; output: code corresponding to year ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun main (year)
(interactive "sEnter your academic year: ") ; read year
(message "%d" (yearCode year))) ; display its code
;;; Tests...
(main "junior")
(yearCode "junior")
I honestly know nothing about elisp so I am having trouble even having the first part compile. Can anyone help me construct an if statement correctly?
EDIT: I was testing in the wrong buffer -_- code worked using if statements.
This would be the way to write it using if statements. An if-statement has a condition part, which is either true or false. If it is true, it evaluates the FIRST part. It can only be one sentence really, unless you use
(progn ). The rest of it will be the else part of the if statement.Though this answers your question specifically about
if, I would certainly advise to usecondinstead , like the others suggested.Below the application of
progn, in the case of TRUE, to have more than 1 expression to be evaluated.