0

I have a question regarding trying to define a recursively defined foldl function in Racket.

Here is my approach:

(define foldl
  (lambda (z c xs)
    (match xs
      (empty z)
      ((make-pair x xs) (foldl c (c z x) xs)))))

Unfortunately, when I do this, I get the Error:

expected a function after the open parenthesis but received 1

I cannot quite figure out why this message is popping up. Can someone help me?

Ethan
  • 876
  • 8
  • 18
  • 34

1 Answers1

0

The error you are seeing is use to swapping the arguments to foldl.

Your definition is:

(define foldl
  (lambda (z c xs)

Here z is an element a c is a constructor. In

(foldl c (c z x) xs)))))

you swapped the first two argument.

Note: You need to change the match patterns.

Change empty to '(). Change make-pair to cons.

soegaard
  • 30,661
  • 4
  • 57
  • 106