removing last element of a list(scheme)

后端 未结 6 1905
轻奢々
轻奢々 2021-02-07 17:44

So I have to remove the last element of a list in scheme.

For example, let\'s say I have a list (1 2 3 4). I need to return:

(1 2 3)
         


        
6条回答
  •  佛祖请我去吃肉
    2021-02-07 18:48

    I've done something simpler than: reverse(list), car(list), reverse(list) to get the last element, check out:

    (define (last-one liste)
      (if(null? (cdr liste))
         null
         (cons (car liste) (last-one (cdr liste)))
      )
    )
    

提交回复
热议问题