Skip to content

Commit

Permalink
Apply works as expected.
Browse files Browse the repository at this point in the history
  • Loading branch information
arlaneenalra committed May 11, 2019
1 parent 095f35b commit e71048c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
8 changes: 6 additions & 2 deletions lib/baselib.scm
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,12 @@
;; For cond
(define else #t)

(define (apply proc . args)
(asm (args) (proc) tail_call_in ))
(define (apply proc l . args)
(define new-args
(if (null? args)
l
(asm (args) (l) cons (append) call_in)))
(asm (new-args) (proc) tail_call_in))

(include "dynamic.scm")

Expand Down
4 changes: 2 additions & 2 deletions lib/loop.scm
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
(if (or (null? val-list) (null? (car val-list)))
'()
(begin
(apply proc (car val-list))
(apply proc (list (car val-list)))
(inner (cdr val-list)))))

(inner (prep-args val-list)))
Expand All @@ -66,7 +66,7 @@
(reverse result)
(inner (cdr val-list)
(cons
(apply proc (car val-list))
(apply proc (list (car val-list)))
result))))

(inner (prep-args val-list) '()))
Expand Down
24 changes: 21 additions & 3 deletions test/test_apply.scm
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,32 @@

(include "expect.scm")

(expect "Verify that apply appends to the list of args"
(lambda ()
(apply list (list 1 2 3) (list 4 5 6)))
(list 1 2 3 4 5 6))

(expect "Verify that apply feeds a list to the proc as expected"
(lambda ()
(apply list (list 1 2 3)))

(list 1 2 3))

(expect "Verify that apply appends to the list of args"
(expect "That a simple apply works"
(lambda ()
(apply (lambda (x y ) (+ x y)) (list 1 2)))
3)

(define (div30 x) (/ x 30))
(define (mul x y) (* x y ))

(define compose
(lambda (f g)
(lambda args
(f (apply g args)))))

(expect "Check a more complex example"
(lambda ()
(apply list (list 1 2 3) 4 5 6)
(list 1 2 3 4 5 6)))
((compose div30 mul) 12 75))
30)

0 comments on commit e71048c

Please sign in to comment.