Write a Lisp program to calculate the sum of the first N positive integers

Click For Summary

Discussion Overview

The discussion revolves around writing a Lisp program to calculate the sum of the first N positive integers. Participants explore different approaches and provide hints and code snippets related to the implementation of this concept.

Discussion Character

  • Exploratory
  • Technical explanation
  • Homework-related

Main Points Raised

  • One participant requests assistance in writing a Lisp program to compute the sum of the first N positive integers, providing an example with N = 6 resulting in a sum of 21.
  • Another participant suggests a recursive approach, stating that the sum of the first N numbers can be expressed as N plus the sum of the first N-1 numbers.
  • A proposed code snippet attempts to define a function called nsum, but it includes a syntax error and does not handle input validation correctly.
  • A later post points out an error in a different code attempt, highlighting issues with tokenization and incorrect assertions regarding input types.
  • Another participant reiterates the recursive definition of the sum, emphasizing that nsum of n should return 0 if n is less than 1, otherwise it should be n plus the nsum of (n-1).

Areas of Agreement / Disagreement

Participants generally agree on the recursive nature of the problem and the definition of the sum, but there are multiple competing views on the correct implementation in Lisp, and the discussion remains unresolved regarding the final code solution.

Contextual Notes

Some code snippets contain syntax errors and issues with input validation that remain unaddressed. The discussion also reflects varying interpretations of how to handle edge cases in the implementation.

fernanroy
Messages
6
Reaction score
0
Help! Please

I am trying to write a Lisp program to calculate the sum of the first N positive integers where, for example, when N = 6 the sum of the first N = 21

Any Ideas?
 
Technology news on Phys.org
Sure: the sum of the first N numbers is N plus the sum of the first N-1 numbers. That's a very strong hint. Heck, it's pretty much exactly the answer in LISP... :-p
 
out of whack said:
Sure: the sum of the first N numbers is N plus the sum of the first N-1 numbers. That's a very strong hint. Heck, it's pretty much exactly the answer in LISP... :-p

Would it be something like this?

(defun nsum (n)
(if (integerp n)
(if (or (= n 0) (< n 1))
Please enter the positive integer
(* n(/ 2(+ n 1))))))

Regards
 
This attempt yields the error: *** Read error: The input could not be tokenized at 2, 2. ***

> (defun sum (n1 n2)
“Returns the sum of two positive integers.”
(assert
(and (integerp n1) (>= n1 0))
(n1)
“N1 must be a nonnegative integer, instead it’s ~S.”
(n1)
(assert
(integerp n2)
(n2)
:N2 must be an integer, instead it’s ~S.”
n2)
(if (zerop n1) n2
(sum (1- n1) (1+n2))))
 
Even more obvious hint: nsum of n is defined as 0 if n is less than 1, otherwise it is n plus the nsum of (n-1). This is true in English, right? So write exactly that, but in LISP instead of English.
 

Similar threads

  • · Replies 25 ·
Replies
25
Views
3K
Replies
7
Views
3K
  • · Replies 15 ·
Replies
15
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 89 ·
3
Replies
89
Views
6K
  • · Replies 16 ·
Replies
16
Views
3K
  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K