# Bigger scheme - sam's capsule: sloum's small s-expression based language - Part 2 ``` / \ / k --- a b --- f / \ / \ me --- l i \ / \ / j --- c d --- e \ / g --- h ``` => 2021-08-12-sloums_small_s-expression_based_language.gmi Part 1 ## Whaddazidoo Great, it seems to work, good for you, Sam. Where are the comments? ...rrrrigh. The comments. ## Let's write some comments Scheme, and 'slope' is a Scheme dialect, uses semicolon ';' as a comment marker. ``` ; Define our stack. ; - General note: I use names with capital letters to differentiate my own stuff from interpreter's command and internal variables ; - Noticed the tick ' in from of the ()? Good. We don't want this _list_ to be evaluated now (define Stack '()) ; ; PUSH - because an empty stack is no fun. ; Puts ('pushes') the value of the parameter on top of the stack. ; - accept: 1 parameter ; - modify: stack ; - return: none ; ; - we define PUSH as a function (define PUSH (lambda ; - with one parameter 'val' (val) ; - all it does is to set new value of the list Stack (set! Stack ; - to a new list, built with 'cons', consisting of our 'val' and the old list (cons val Stack)))) ; POP - because we are not hording the values, we are going to use them. ; Removes ('pops') the value from the top of the stack and returns it ; - accept: none ; - modify: stack ; - return: value on top of the stack ; ; - we define POP as a function (define POP (lambda ; - with no parameters () ; - as a compound expression -- we will use the result of evaluation of the first expression as a return value (begin0 ; - ... and that would be that: car (CAR - Contents of the Address part of Register number - an ancient lingo for 'head'/'first element' of the list) (car Stack) ; - now we set the new value of our Stack list to ; - cdr (CDR - Contents of the Decrement part of the Register number - and that would be the 'tail'/'rest'/'remainder' of the list) (set! Stack (cdr Stack))))) ``` ## Cute, but what if I want to access other elements of the list? You can use a combination of CAR's and CDR's to get there :) ### Let's have some fun [insert image: Spongebob "F.U.N."] Note: the '>' in the first column is slope's prompt, do not type or copy it when experimenting! Note: of course you don't have to type/copy the comments ;) - Start your 'slope' - Type (or copy-paste what I already typed in): ``` ; define our 'guinea pig' list > (define List '(1 2 3 4 5 6 7 8 9)) ; that's intepreter's return value, I left the first to let you know it happens, but will delete them in the following code #1=> ok ; display it, with a new line character at the end, for better readibility (yes, 'display' accepts one or more parameters) > (display List "\n") ; looks exactly as we define it (1 2 3 4 5 6 7 8 9) ; let's get it's 'head' > (display (car List) "\n") ; that worked :) 1 ; and now - the tail > (display (cdr List) "\n") ; great, that worked too (2 3 4 5 6 7 8 9) ; what about... a head of the tail? > (display (car (cdr List)) "\n") ; that would be '2', right? 2 ; right ; now... a head of the tail of the tail > (display (car (cdr (cdr List))) "\n") ; Great :) You get it? 3 ; if the tail of the tail had a head... what about the tail of the tail of the tail? > (display (car (cdr (cdr (cdr List)))) "\n") ; yup, that will work too 4 ; but don't get obssessed with the head... 😏 ; the tail of the tail of the tail has a tail too > (display (cdr (cdr (cdr (cdr List)))) "\n") ; yesss! It does (5 6 7 8 9) ; did we break something? > (display List "\n") ; Apparently we didn't. To change a list you need to assign a new value to it (1 2 3 4 5 6 7 8 9) ``` ## That's all for now, folks So far we learnt: - 'define' is used for... defining things *ba-dumm-tssss* - a function in slope is a list - lists have heads ('car') and tails ('cdr') - most tails had their heads and tails too (the shortest ones don't, but we're not there yet) - my understanding of 'fun' is weird. See you then, -- sam ## Get slope => https://git.rawtext.club/sloum/slope Gitea repository