Oppa
Oppa

Reputation: 35

compare the length of two list and append in racket

I've tried to write a function, merge_longer, in Racket that takes as input two lists, L1 and L2. If L1 is longer than L2 the function appends L2 to L1. Otherwise, it appends L1 to L2.

(define L1 '(4 6 8 9))
(define L2 '(1 2 3)) 
(define (merge_longer L1 L2) (if (> length(L1) length(L2)) (append L1 L2)(append L2 L1)))  
(merge_longer L1 L2)

However, this error is displayed while I run it: application: not a procedure; expected a procedure that can be applied to arguments given: '(4 6 8 9) arguments...: [none]

Can someone please help me solve this problem?

Upvotes: 1

Views: 510

Answers (1)

soegaard
soegaard

Reputation: 31147

You were close.

(define (merge-longer l1 l2)
  (if (> (length l1) (length l2))
      (append l1 l2)
      (append l2 l1)))

Note that you can add extra parentheses in Racket. When you write (l1) it means "apply the function l1 to no arguments". Since l1 is a list, not a function, you get an error.

Upvotes: 1

Related Questions