From 7888d341776f23849c60cd82ca816ed13e5bc9c2 Mon Sep 17 00:00:00 2001 From: jim Date: Sat, 21 Mar 2015 13:18:13 -0400 Subject: [PATCH] add set_equal? --- exercises/assignment4_answers.mdwn | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/exercises/assignment4_answers.mdwn b/exercises/assignment4_answers.mdwn index 844ce38d..c7e23f5b 100644 --- a/exercises/assignment4_answers.mdwn +++ b/exercises/assignment4_answers.mdwn @@ -90,7 +90,19 @@ For instance, `fact 0 ~~> 1`, `fact 1 ~~> 1`, `fact 2 ~~> 2`, `fact 3 ~~> * The native Scheme function that most resembles the `mem?` you're defining is `memv`, though that is defined for more than just numbers, and when that `memv` finds a match it returns a *list* starting with the match, rather than `#t`. - ANSWER: TODO + ANSWER: + + let neq? = \n m. neg (num_eq? n m) in + let mem? = \n xs. neg (empty? (drop_while (neq? n) xs)) in + let without = \n xs. append (take_while (neq? n) xs) (tail (drop_while (neq? n) xs)) in + let set_cons = \x xs. (mem? x xs) xs (cons x xs) in + let set_equal? = Y (\set_equal?. \xs ys. (empty? xs) + (empty? ys) + ; else when xs aren't empty + ((mem? (head xs) ys) + (set_equal? (tail xs) (without (head xs) ys)) + ; else when head xs not in ys + false)) 7. Linguists often analyze natural language expressions into **trees**. We'll need trees in future weeks, and tree structures provide good opportunities for learning how to write recursive functions. Making use of our current resources, we might approximate trees as follows. Instead of words or syntactic categories, we'll have the nodes of the tree labeled with Church numbers. We'll think of a tree as a list in which each element is itself a tree. For simplicity, we'll adopt the convention that a tree of length 1 must contain a number as its only element. -- 2.11.0