Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9248731
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:00:47+00:00 2026-06-18T10:00:47+00:00

I am new to elisp and am trying to write a simple program using

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T10:00:48+00:00Added an answer on June 18, 2026 at 10:00 am

    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)))))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

new to c#. I'm trying to make a simple system where I can search
I read elisp's network server manul, but I cann't find write a socket listening
new on ruby and using windows xp and rails 3, i want to send
New to Node.js and Express, I am trying to understand the two seems overlapping
New programmer here, I am trying to understand and break down this code below
New developer here,Im using the Custom Image Picker by ray wenderlich. But what I
New to wordpress and web design in general. I'm trying to setup a website
How do you move the point in Elisp to the beginning of <enter description
New to java here. I'm trying to split a string into variables in an
I'm trying to create a small interactive elisp function in order to quickly display

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.