2

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.

Seki
  • 11,135
  • 7
  • 46
  • 70
ola
  • 882
  • 2
  • 12
  • 29
  • That last edit totally changed the question... The current `yearCode` definition will fail when you pass in a string that is not one of the four expected strings (after changing `=` to `string=`) because it has an expression where the first element isn't a function: `(0)`. – Trey Jackson Feb 06 '13 at 23:24

3 Answers3

4

When you have if..elseif..elseif..else in ruby, you use cond in emacs lisp:

(defun yearCode (name)
  (cond
   ((string= name "freshman") 1)
   ((string= name "sophomore") 2)
   ((string= name "junior") 3)
   ((string= name "senior") 4)))   

That's not how I would write it, however.

(defun yearCode (name)
  (cdr (assoc name
              '(("freshman" . 1)
                ("sophomore" . 2)
                ("junior" . 3)
                ("senior" . 4)))))

There are many kind of equality, and = is strictly for numbers (or markers). string= is for strings and string designators, eq is for object identity, equal is a kind of deep equality of elements (that's why it works for strings, but it would just return false for string vs. non-string comparison, never detecting an error). This latter one, equal, is used by assoc.

Anton Kovalenko
  • 20,999
  • 2
  • 37
  • 69
  • doing it this way does in fact work. But is there a way to use if-statements? The whole point is to compare the different ways if-statements look using all different languages. – ola Feb 06 '13 at 22:55
  • It would really be worth it to spend time getting to know `cond`. You can see it as an extension of if-statements, it really is just simplifying everything! – PascalVKooten Feb 06 '13 at 23:25
2

The way to write your yearCode with minimal changes would be this:

(defun yearCode (name)
  (cond ((string= name "freshman") 1)
        ((string= name "sophomore") 2)
        ((string= name "junior") 3)
        ((string= name "senior") 4)
        (0)))

Edited to add: the original version of the question was structured very differently, so the explanation below no longer applies...

The difference between what you wrote and the above code is that in the above code, yearCode has one (cond) expression, and the return value of that function is the return value of the cond statement.

Your original code had 4 different expressions, only the value of the last expression is actually returned (the last if) statement - meaning the return value is either 4 or nil.

I recommend reading an introductory lisp book, my favorite is the SICP, but you should probably check out the SO question asking for best introductory books.

Community
  • 1
  • 1
Trey Jackson
  • 73,529
  • 11
  • 197
  • 229
  • thank you for your input... I was working in the wrong buffer and was testing wrongly. Thank you so much for the information however I will use conditionals from now on :) – ola Feb 06 '13 at 23:13
1

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.

(defun yearCode(name)
    (if (string= name "freshman")    ; true or false?
       1                            ; if true, returns 1 

  ; Note that you can't really write anything else here. 
  ; From here on, it is the first else part!

     (if (string= name "sophomore")  ; if false, else part  etc.
       2
      (if (string= name "junior") 
        3
       (if (string= name "senior") 
         4)))))

Though this answers your question specifically about if, I would certainly advise to use cond instead , like the others suggested.

Below the application of progn, in the case of TRUE, to have more than 1 expression to be evaluated.

(defun yearCode(name)
    (if (string= name "freshman")    ; true or false?
       (progn 1                      ; if true, returns 1 
              (message "this is possible!"))          

     (if (string= name "sophomore")  ; if false, else part  etc.
       2
      (if (string= name "junior") 
        3
       (if (string= name "senior") 
         4)))))
PascalVKooten
  • 20,643
  • 17
  • 103
  • 160