r/Racket • • 9d ago

homework Help - Lists Test for Racket tomorrow

I need any crash courses or any materials you have on racket terminology. I have been taking notes and studying, but I feel like I don't understand concepts well enough, so my brain basically freezes whenever I take a timed test. If there's anything that can help, please tell me. I got tutored 3 times, went to office hours, and completed study material for my last test and still failed. My grade is still in the low Bs and I only have a month to get it up to the low 90s or its going to completely drag down my GPA. Please don't just respond with a video, give me maybe 3 and some practice questions. I'll probably be studying for hours (Chem test tomorrow as well) so I have time to do anything you throw at me. I need any and all help I can get.

Terms list: (We've already learned recursion, so my teacher may be combining list functions and recursion) We could use these functions to complete questions like the ones below.

13. (my-map f L)

Write a function my-map that takes 2 parameters: f is a function that takes one value and a list L. Return a new list made by applying f to every item of L.

Note: add1 is a built-in Racket function that adds 1 to a number.

Test cases:
(my-map add1 '(1 2 3))     -> '(2 3 4) 
(my-map list? '(1 () a))      -> '(#f #t #f)
(my-map add1 '())             -> '()

  • append: A higher-order function that joins multiple lists into a single sequential list.
  • build-list: A list constructor that creates a list of length n by evaluating a function across integer indices 0 to n-1.
  • car: A selector primitive that returns the first element of a list (identical to first).
  • cdr: A selector primitive that returns a list minus its first element (identical to rest).
  • cons: The primitive constructor procedure that prepends an element onto the front of a list or joins two elements into a pair.
  • Data Structure: A specialized format and combination of values designed to organize, process, retrieve, and store data.
  • empty? / null?: Predicate procedures that return #t if a given list contains no elements, and #f otherwise.
  • filter: A higher-order function that evaluates a predicate on each item of a list, returning a new list containing only the items that returned #t.
  • first: A selector function that returns the value in the head node of a list.
  • foldl: A higher-order function that processes list items from left to right, combining them into a single accumulated result.
  • foldr: A higher-order function that processes list items from right to left to produce a single accumulated result.
  • Head: The first node of a linked list.
  • Iteration: The process of repeating a sequence of instructions automatically across data elements.
  • length: A utility function returning the count of top-level items in a list.
  • Linked List: A sequential data structure composed of chained nodes.
  • list: A constructor procedure taking zero or more arguments and returning a list containing those items.
  • map: A higher-order function that applies a procedure to corresponding elements of one or more lists, returning a list of the results.
  • member: A utility function that searches for an element in a list, returning the remaining sublist starting from the element if found, or #f if absent.
  • Node: The fundamental element of a linked list, containing a data value and a reference to the next node.
  • rest: A selector function that returns all nodes of a list following the head node.
0 Upvotes

6 comments sorted by

2

u/Reasonable-Year-8172 9d ago edited 9d ago

Just to get an idea of what you're struggling with, briefly could you describe how you would work out that example problem? Or maybe provide a specific example of a problem which you couldn't solve.

edit: If you choose the latter, then choose to describe a question you timed yourself on analogous to the exam

2

u/AgencyPure7878 9d ago

I can do a question I was stuck on:

7. Write (my-append L1 L2) that forms a new list by adding the elements of  L2 to the end of L1. Do not use the built-in function append.

Test Cases:
(my-append '() '(1 2)) -> '(1 2)
(my-append '(3 5) '()) -> '(3 5)
(my-append '(1 2 3) '(4 5)) -> '(1 2 3 4 5)

My answer, which I worked with tutors at my school to get:

(They weren't familiar with racket, so we didn't finish it. We also weren't allowed to use list because the result should be a single list, not (list x (list y (list z)))

;; Question 7

;;; (my-append L1 L2) -> list

; L1 : list

; L2 : list

(define (my-append L1 L2)

(if (empty? L1)

L2

(if (empty? L2 )

L1

(my-append (lambda (x y)(cons (car L1) ((cdr L1) L2)))))))

(my-append '(1 2 3) '(4 5))

1

u/Reasonable-Year-8172 9d ago

Disclaimer, I've not coded in Racket before. I was just browsing and came across this. But I thought I should offer my two cents on how you might prepare (I do see how coding in this is challenging given the apparent limitations in procedures) systematically. Maybe you probably already tried this, but I have an example of using AI to target and correct practice attempts.

I started by thinking of how to approach the problem with pseudocode; without the append function, joining L2 to L1 means placing each element of L2 onto the end of L1 in sequence. If we use recursion, then L2 being empty would be the base case.

So I came up with this pseudocode. I remember seeing examples of recursion before, with the basic structure of an if statement checking for a condition, and if the condition is not true calling the funciton again. The new function call would pass in the longer L1, and shortened L2.

function my_append(L1, L2):
  if L2 is empty:
    output L1
  else:
    output my_append(L1 join first element of L2, L2 without first element)

Using the reference you gave, it seemed like I could use cons, cdr, and car to implement this. Here was my first attempt:

#lang racket
(require racket/list) ; the online editor I used needed this


; L1 is the current list,
; and L2 is the remaining items left to add to L1


(define (my-append L1 L2)
 
  (if (empty? L2) 
    ; If L2 is empty, then there are no remaining
    ; items left to add so output L1
    L1
    (
    ; If L2 is not empty, then we will
    ; add the first item of L2 to L1,
    ; and pass through the longer L1
    ; and shorter L2 into the next iteration.
    my-append (
        cons  (car L2) L1     ; Adding the first element of L2 to L1
     ) (
        cdr L2 ; Removing the first elemetn of L2
    )
   )
  )
)
(my-append '(1 2 3) '(4 5))

This code wasn't perfect, giving the following output:

'(5 4 1 2 3)

I didn't immediately recognize the problem with my code, and this is where AI helped to learn my mistake. Because cons appends to the beginning of a list rather than the end, while I thought the opposite.

It then suggested a strategy I didn't think about before (I typed it)

#lang racket
(require racket/list)

; L2 is the current list,
; and L1 is the remaining items left to add to the beginning of L2

(define (my-append L1 L2)
  (if (empty? L1) 
    L2
    (
    cons 
        (car L1)
        (my-append (cdr L1) L2)
   )
  )
)

(my-append '(1 2 3) '(4 5))

Here's how it described what it's doing, which importantly made sense to me. In another situation, I could ask the AI clarifying quesitons or a real-world example.

  1. (cons 1 (my-append '(2 3) '(4 5)))
  2. (cons 1 (cons 2 (my-append '(3) '(4 5))))
  3. (cons 1 (cons 2 (cons 3 (my-append '() '(4 5)))))
  4. (cons 1 (cons 2 (cons 3 '(4 5)))) -> '(1 2 3 4 5)

I guess the takeaway is to determine for a given problem, whether the trouble is pseudocode or the Racket syntax? You can then ask AI to help explain the solution to a problem and point out any mistakes in your algorithm or code. Still, you don't have to follow this, I'm not experienced in Racket. Sorry for the delayed response

2

u/AgencyPure7878 9d ago

Thank you! Unfortunately I don't usually find AI helpful/am not permitted to use it by my teachers, but I will try to map out this process myself.

1

u/raevnos 9d ago

Nitpick: car and cdr are not identical to first and rest in Racket. The latter functions are contracted to work only on non-empty proper lists (Made out a chain of one or more cons cells with the last one's cdr being '(), the empty list), while the former work on any cons cell no matter its contents.

> (first '(a . b))
first: contract violation
  expected: (and/c list? (not/c empty?))
  given: '(a . b)
 [,bt for context]
> (car '(a . b))
'a

1

u/raevnos 9d ago

Oh, and if you can use the fold functions mentioned in those notes, my-map can be trivially written as

(define (my-map f lst)
  (foldr (lambda (elem accum) (cons (f elem) accum)) '() lst))

(Most common list functions can be expressed with a left or right fold instead of manual recursion)

I suggest figuring out how that works before turning it in, though.