[Chicken-users] scheme student perplexed by failures ...

2012-08-11 Thread john saylor
hi

been working through 'yet another scheme tutorial'
[http://www.shido.info/lisp/idx_scm_e.html], and have come up against
something i cannot figure out.

here is the answer:


(define (my-reverse ls)
  ;(my-reverse-rec ls ()))
  (my-reverse-rec ls '()))

(define (my-reverse-rec ls0 ls1)
  (if (null? ls0)
  ls1
  (my-reverse-rec (cdr ls0) (cons (car ls0) ls1


works great- reverses the list.

and here is my propsed solution:
define js-reverse
(lambda (ls)
((reverse-rec ls '()

(define js-reverse-rec
(lambda (asc dsc)
((if (null? asc)
dsc
;(js-reverse-rec (cdr asc) (list dsc (car asc)))
(js-reverse-rec (cdr asc) (cons (car asc) dsc))

pretty much identical [i left in my initial (incorrect) use of 'list']

but my code errors out!
#;38 (js-reverse '(1 2))

Error: call of non-procedure: (dsc)

Call history:

syntax  (quote (1 2))
syntax  (##core#quote (1 2))
eval(js-reverse (quote (1 2)))
eval[js-reverse] ((reverse-rec ls (quote (
eval[js-reverse] (reverse-rec ls (quote ()))
eval[reverse-rec] (null? asc)
eval[reverse-rec] (reverse-rec (cdr asc) (cons (car asc) dsc))
eval[reverse-rec] (cdr asc)
eval[reverse-rec] (cons (car asc) dsc)
eval[reverse-rec] (car asc)
eval[reverse-rec] (null? asc)
eval[reverse-rec] (reverse-rec (cdr asc) (cons (car asc) dsc))
eval[reverse-rec] (cdr asc)
eval[reverse-rec] (cons (car asc) dsc)
eval[reverse-rec] (car asc)
eval[reverse-rec] (null? asc) --

i have tried putting the dsc in parens, calling 'quote' all to no avail.

i hope it's something idiotic that i am just missing. any help is welcome.

-- 
\js [http://or8.net/~johns/] : complete obscure contrariness

___
Chicken-users mailing list
Chicken-users@nongnu.org
https://lists.nongnu.org/mailman/listinfo/chicken-users


Re: [Chicken-users] scheme student perplexed by failures ...

2012-08-11 Thread Thomas Chust

On Sat, 11 Aug 2012, john saylor wrote:

[...]
and here is my propsed solution:
define js-reverse
   (lambda (ls)
   ((reverse-rec ls '()
[...]


Hello John,

your code above lacks an opening paranthesis before the define and it has 
an extraneous pair of parentheses around the call to reverse-rec.



[...]
(define js-reverse-rec
   (lambda (asc dsc)
   ((if (null? asc)
   dsc
   ;(js-reverse-rec (cdr asc) (list dsc (car asc)))
   (js-reverse-rec (cdr asc) (cons (car asc) dsc))
[...]


Again, there is an extraneous pair of parentheses around the use of the if 
syntax.


In Scheme, parentheses are relevant syntax that indicate uses of syntax or 
calls to procedures. The expression


  ((reverse-rec ls '()))

means

  Call reverse-rec with the arguments ls and '(), then call the result
   with no arguments.

Since the results of reverse-rec and of the conditional in js-reverse-rec 
are not procedures but lists, these extra calls you programmed cause the 
type errors you see.


Ciao,
Thomas


--
When C++ is your hammer, every problem looks like your thumb.

smime.p7s
Description: S/MIME Cryptographic Signature
___
Chicken-users mailing list
Chicken-users@nongnu.org
https://lists.nongnu.org/mailman/listinfo/chicken-users