امفر哔 is a user on tiny.tilde.website. You can follow them or interact with them if you have an account anywhere in the fediverse. If you don't, you can sign up here.
امفر哔 @m455b

why does (define (my-length a-list)
(if (empty? a-list)
0
(add1 (my-length (rest a-list)))))

(my-length '(this is a test))

work, but this doesn't?

(add1 (rest '(this is a test)))

initially i was confused that the first example was actually working because it doesn't have numbers, but it does and returns 4 "magically", but the last one gives me the error i expected from the first example "expected number"

@m455b You need to evaluate this on paper maybe:

(add1 (rest '(this is a test))) -> (add1 '(is a test)) -> error.

whereas: (add1 (my-length (rest '(this is a test)))) -> (add1 (my-length '(is a test))) -> (add1 (add1 (my-length (rest '(is a test)))) -> (add1 (add1 (my-length '(a test)))) -> (add1 (add1 (add1 (my-ength (rest '(a test)))))) -> (add1 (add1 (add1 (my-length '(test))))) -> (add1 (add1 (add1 (add1 (my-length (rest '(test))))))) -> (add1 (add1 (add1 (add1 (my-length '()))))) -> ...

@m455b (add1 (add1 (add1 (add1 0)))) -> (add1 (add1 (add1 1))) -> (add1 (add1 2)) -> (add1 3) -> 4.

See the difference :)

@ckeen Omg! That really long reply really helped thank you so so much. I was thinking "how can the list members be turning into zero when expanding this???" When I get home after work I will take a good stare at this/try it on paper I think I get how it's recursion is working now. Thank you so much again you are too awesome!

@ckeen so basically it keeps doing rest recursively until it reaches the empty? :D

@m455b Close! It keeps calling my-length recursively always with the rest of the list, which means it will always get smaller and be empty at one point. That's called the base case and that's the only place where a value is returned, then the stack unravels and the pending add1's get evaluated...

@m455b So recursion takes a base case and a reduction step that will somehow guarantee that your input will reach the base case at one point. Otherwise this will be a loop nesting infinitely.

@m455b There's a chapter on recursoin in Land of Lisp / Racket but I don't have it handy right now to hint you at the pages...

@ckeen
Took me a few tries to comprehend it but now I understand thanks! I was trying to figure out why the plus 1 wasn't being evaluated before the next recursing but it's because it works from the inside out of all the brackets and the function is before the plus one when working that way.

Thank you so much I appreciate your help!

tiny.tilde.website/media/uNAUL

@ckeen also I'm really bad at math so I'm happy I got this haja

@ckeen oh so that's what the stack is! The recursing keeps hitting the function before it gets to the outside plus one. I always heard that word and never really understood. Haha it has to keep going through itself to get to the plus 1. Just like that highschool crush

@m455b Yes, that's what it does. It is used quite a bit in scheme and racket, keep going you are doing just fine!

@ckeen thanks so much for the motivation! It's really fun! Which lisp/scheme or any of those varients do you like using? :)

@ckeen sweet!! That's what I started in! But it was a little deep of a jump for a first language because I wasn't that familiar with lisp/scheme's and depended on beginner friendly docs. I love that it can compile to C code/native executables

@m455b We will always be there for you when you are comming back :) Try #chicken on freenode for help from nice people.

@ckeen thanks! I most definitely will be back ;)