r/Common_Lisp • • Jul 21 '20

I need help with 2 functions !

I need to write 2 functions for a school assignment. I have already written them and they both work, but my teacher is not happy with them and I have no idea how to do them otherwise.

The first one is supposed to return the position in the list of the argument it's been called with, nil if it's not in the list.

Here's what I got

(defun myposition (lettre liste)
  (cond
    ((not (member lettre liste)) (return-from myposition nil))
    ((equal (car liste) lettre) 0)
    ((+ 1 (myposition lettre (cdr liste)))) ) )

He won't allow us to use member, or return-from, and I don't know how one function can return either a number or nil...

The second function is supposed to return the n cdr of a list, cdr 0 being the list itself

Here's what I came up with

(defun niemecdr (item liste)
  (cond
    ((not liste) (return-from niemecdr nil))
    ((equal item 0) (return-from niemecdr liste))
    (t (niemecdr (- item 1) (cdr liste))) ) )

For this one he said that my function was crazy (sorry I don't know how to better translate what he actually said) and again I shouldn't use return-from or what am I going to use when we start writing complexe functions, an atomic bomb?

(Sorry about the rant, I'm kinda pissed about the whole thing)

1 Upvotes

11 comments sorted by

3

u/kazkylheku Jul 21 '20 edited Jul 21 '20

No wonder the teacher is not happy.

You're using gratuitous return-from forms that can be removed.

Say, did you notice that your first function works even though you have this:

    ((equal (car liste) lettre) 0)

and not this:

    ((equal (car liste) lettre) (return-from myposition 0))

?

This is because when the 0 evaluates, the cond constructs terminates and yields that 0 value. And since that cond is the last form of myposition, that function ends, and returns that 0. The return-from is not doing anything which does not already happen naturally.

If return-from is used in such a way that it is actually required (the code stops working if (return-from x val) is replaced by val), it implies that the code isn't written in "pure Lisp". Programming in "pure Lisp" refers to a functional subset of Lisp which avoids variable assignment, and object mutation.

Your teacher probably wants assignments to use only the functions and operators that keep the code pure Lisp, and not the full language (like someone writing real-world applications). That is why return-from is not allowed.

Even if that were not the case, the teacher probably would still want you to to realize that you have some unnecessary elements in your program that can simply be removed without touching anything else.

1

u/matmatomate Jul 21 '20

thank you for your help, I actually never realized that I don't need to specifically write return for the function to return something

6

u/anydalch Jul 21 '20

, your usage of return-from shows that you do not understand how common lisp is evaluated. the fact that you only return-from in some branches but not others shows that you wrote this code not through understanding but through trial and error. unlike in e.g. c, java or js, in common lisp, there is no distinction between statements and expressions: every expression returns a value. cond returns the value of whatever branch it takes, so

(cond (nil :not-taken)
      (t :taken))

evaluates to :taken. in lisp, usually you do not return or return-from a function, you just have the last expression in the function's body evaluate to its result. so,

(defun replace-foo-with-bar (thing)
  (if (eq thing :foo) :bar thing))

will return :bar if called with :foo, and will return its argument otherwise.

I don't know how one function can return either a number or nil...

your myposition already does this. lisp functions can just do that, thanks to the miracle of dynamic typing.

what am I going to use when we start writing complexe functions, an atomic bomb?

this is a serious concern. common lisp contains some special forms which are comparable to an atomic bomb in power and thus in ability to misuse. if you reach for early return, multiple inheritance, signal handling or the meta-object protocol to solve simple problems, you will end up writing inefficient, unreadable code. you should be able to solve most problems using only a small handful of special forms; once you understand how those building blocks combine into complex software, you can start to use some of the turbo-powered shortcuts included in common lisp.

i'm not really sure what else to tell you, since you're more complaining than asking questions, so here are the simple recursive versions of the functions you're supposed to write:

(defun my-position (elt list)
  (flet ((pos-inner (list idx)
           (cond ((null list) nil)
                 ((equal (car list) elt) idx)
                 (:otherwise (pos-inner (cdr list) (1+ idx))))))
    (pos-inner list 0)))

(defun my-nthcdr (n list)
  (if (= n 0) list
      (my-nthcdr (1- n) (cdr list))))

another version of my-position which i find cleaner:

(defun my-position (elt list &optional (idx 0))
  (cond ((null list) nil)
        ((equal (car list) elt) idx)
        (:otherwise (my-position elt (cdr list) (1+ idx)))))

note that all of these functions are tail-recursive. your myposition is not tail recursive, which is one of the many ugly things about it. you seem to have stumbled upon a tail-recursive niemecdr, though, so kudos on that.

1

u/[deleted] Jul 21 '20

this is a serious concern. common lisp contains some special forms which are comparable to an atomic bomb in power and thus in ability to misuse

gold

1

u/defmacro-jam Jul 21 '20

Are you allowed to use position?

(defun myposition (lettre liste)
   (position (character lettre) (coerce liste 'list)))

1

u/matmatomate Jul 21 '20

No we're not allowed to use position, nor find

1

u/defmacro-jam Jul 21 '20

How about loop?

 ;;;; Untested -- probably works
 (defun myposition (lettre liste)
       (loop for char in (coerce liste 'list)
               for counter from 1 to (length liste)
               when (string= (coerce lettre 'string) (string char))
                   return counter))

2

u/daybreak-gibby Jul 21 '20

First thing to note is that everything is an expression so no need for return-from in this code. Instead of (return-from niemecdr nil) you could just have nil and if that branch is evaluated it will return nil.

The second thing to note for the first one is to use recursion. You can't use member because you are basically supposed to write member.

For the first function, I would use recursion. Here is the general strategy. Look at the first item of the list. If it is not what you are looking for, then search the rest of your list. What if the list is empty? You can't find an item in an empty list so return nil. Make sure you check if the list is empty first.

The second function works in a similar way. Is the list empty? Return nil. Is n 0? return the list. I will let you figure out what the recursive case would be. The key to thinking recursively is to know when to stop and to know how to proceed. I hope this helps.

2

u/mdbergmann Jul 21 '20

I think you should tell us what this assignment you got is about.

Is this a functional programming class?

In any case, you should learn a few basics about the language. I.e. that the value of the last expression is returned.

0

u/ccQpein Jul 21 '20

Let me guess

(defun myposition (lettre liste)
  (labels ((inner-function (lettre liste start)
             (if (not liste)
                 nil
                 (if (eql lettre (car liste))
                     start
                     (inner-function lettre (cdr liste) (1+ start))))))
    (inner-function lettre liste 0)))

(defun niemecdr (item liste)
  (dotimes (a item)
    (setf liste (cdr liste)))
  liste)

well, no return-from at least