Skip to content

Instantly share code, notes, and snippets.

@zetavg
Created December 30, 2014 14:19
Show Gist options
  • Save zetavg/58375301e2793cd5f031 to your computer and use it in GitHub Desktop.
Save zetavg/58375301e2793cd5f031 to your computer and use it in GitHub Desktop.
Programming Assignment #2                                   CS4001301
=====================================================================

1. Scheme (50pt)

	function name: inv_app
	input: 2 lists, list1 and list2
	output: inverse of list2 + inverse of list1

	Example:
	list1 = (1 2 3)
	list2 = (a b c)

	output = (c b a 3 2 1)
	  
	Put your source code in a file named pa2scheme1.scm
	and include at lease one test case for your implementation. 

2. Scheme (50pt)

	function name: dbl_atm
	input: an atom and a list
	output: a list with all occurrences of the given atom doubled 

	Example:
	atom = a
	list2 = (a (b c a (a d)))

	output = (a a (b c a a (a a d)))
	  
	Put your source code in a file named pa2scheme2.scm
	and include at lease one test case for your implementation.

Results

(inv_app '(1 2 3) '(a b c))
; => (c b a 3 2 1)

(dbl_atm 'a '(a (b c a (a d))))
; => (a a (b c a a (a a d)))

http://repl.it/languages/Scheme/

(define (inverse l)
(if (null? l)
'()
(append (inverse (cdr l)) (list (car l)))
)
)
(define (inv_app l1 l2)
(append (inverse l2) (inverse l1))
)
; (inv_app '(1 2 3) '(a b c))
; => (c b a 3 2 1)
(define (dbl_atm atom list2)
(if (null? list2)
'()
(append
(if (pair? (car list2))
(list (dbl_atm atom (car list2)))
(if (equal? atom (car list2))
(list (car list2) (car list2))
(list (car list2))
)
)
(dbl_atm atom (cdr list2))
)
)
)
; (dbl_atm 'a '(a (b c a (a d))))
; => (a a (b c a a (a a d)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment