Scheme (Racket) if inside cond returns nothing

I worked in DrRacket, trying to create a function "prefix" (#lang racket). It should take two lists as input and should output #t if pf is either zero or equal to the start of ls .

My problem is that my code returns nothing at all when pf not a ls prefix and ls not null. If I replace #f in the if statement with something else, like '() , it will return it correctly, but if I try to capture '() and give a result based on this instead, it will give results that make no sense (for example , say that '() not null, or that '() not equal to '() ). This seems to be due to the fact that there is an if statement in the cond expression. Can someone tell me what he is doing, or why? Is it possible for this code to work correctly, or will I have to rework it in another way?

Thanks for the help!

 (define prefix (lambda (pf ls) (cond [(null? pf) #t] [(null? ls) #f] [(if (equal? (car pf) (car ls)) (prefix (cdr pf) (cdr ls)) #f)]) )) 
+4
source share
1 answer

Having an if inside a cond condition is usually a sign that something is wrong. I think you wanted to say this:

 (define prefix (lambda (pf ls) (cond [(null? pf) #t] [(null? ls) #f] [(equal? (car pf) (car ls)) (prefix (cdr pf) (cdr ls))] [else #f]))) 
+8
source

Source: https://habr.com/ru/post/1434872/


All Articles