Re: I want to append to lists without using append

2022-04-04 Thread Vinod K Chandran via Digitalmars-d-learn
On Monday, 4 April 2022 at 13:32:19 UTC, Steven Schveighoffer 
wrote:


This looks more like lisp or scheme. You know this is a forum 
for the D programming language?



This was the same question in my mind.




Re: I want to append to lists without using append

2022-04-04 Thread Paul Backus via Digitalmars-d-learn

On Monday, 4 April 2022 at 12:57:28 UTC, V3nom wrote:

define the lists:

(define liste (cons 10(cons 20(cons 30(cons 40 ' ())
(define liste2 (cons 20(cons 30(cons 10(cons 40 ' ())


define the "function":

(define (listapp list1 list2)(
 if (null? (cdr list1))
(cons (car list1) (listapp list2 (cdr list1)))
  (if (null? (cdr list2))
  (cons (car list2) (cdr list2))
(cons (car list1) (listapp (cdr list1)  list2))
  )))

append them:

(listapp liste liste2)

What do i do wrong? i think this should work but it is giving 
me an error message like:mcdr: contract violation expected: 
mpair? given: ()


In this line:

if (null? (cdr list1))

...it is possible for list1 itself to be null. If that happens, 
the call to cdr will fail with an error. Before calling car or 
cdr on a list, you must first check to see if the list is null.


Your code contains several other instances of the same error. 
I'll let you find them on your own. :)


Re: I want to append to lists without using append

2022-04-04 Thread Steven Schveighoffer via Digitalmars-d-learn

On 4/4/22 8:57 AM, V3nom wrote:

define the lists:

(define liste (cons 10(cons 20(cons 30(cons 40 ' ())
(define liste2 (cons 20(cons 30(cons 10(cons 40 ' ())


This looks more like lisp or scheme. You know this is a forum for the D 
programming language?


-Steve


I want to append to lists without using append

2022-04-04 Thread V3nom via Digitalmars-d-learn

define the lists:

(define liste (cons 10(cons 20(cons 30(cons 40 ' ())
(define liste2 (cons 20(cons 30(cons 10(cons 40 ' ())


define the "function":

(define (listapp list1 list2)(
 if (null? (cdr list1))
(cons (car list1) (listapp list2 (cdr list1)))
  (if (null? (cdr list2))
  (cons (car list2) (cdr list2))
(cons (car list1) (listapp (cdr list1)  list2))
  )))

append them:

(listapp liste liste2)

What do i do wrong? i think this should work but it is giving me 
an error message like:mcdr: contract violation expected: mpair? 
given: ()



Thanks for helping me :)

Greetings