jordan
jordan

Reputation: 3546

Scheme - Need to replace the value of a list element

My list grabs user input, and creates a list - this list is in characters.

I would like to be able to check if the (car myList) is a character like #\1 or #\2, and then change the car of the list into 1 or 2.

I am using DrRacket.

The problem so far has been attempting to call either (set! (car myList) 1) or (list-set! (car myList) 1 )

Both are undefined references in my environment.

I just started working with scheme today, for a university assignment.

Any help would be greatly appreciated if anyone has time

Thanks

Upvotes: 0

Views: 7006

Answers (4)

jordan
jordan

Reputation: 3546

I used a stack implementation from this site:

http://zoo.cs.yale.edu/classes/cs201/Fall_2007/materials/pdfs/stacks.pdf

The stack let me , well i guess "mirror" my list. after i checked what symbol the user input character was, i would just push an appropriate character onto my stack.

(define (makeListFromSymbols myList)

  (display "Length of List = ")
  (display(length myList)) (newline)
   (cond

      ((null? myList) 
       (display "LIST IS NULL")(newline)

       (popTheRestOfStack)

       )
      ((eq? #\0 (car myList)) 
       (display "Equals 0") 
       (stackForExpression 'push! #\0)
       (makeListFromSymbols(cdr myList))
       (newline)
       )
      ((eq? #\1 (car myList)) 
       (display "Equals 1") 
       (stackForExpression 'push! #\1)
       (makeListFromSymbols(cdr myList) )
       (newline)
       )
      ((eq? #\2 (car myList)) 
       (display "Equals 2") 
       (stackForExpression 'push! #\2)
       (makeListFromSymbols(cdr myList))
       (newline)
       )
      ((eq? #\3 (car myList)) 
       (display "Equals 3") 
       (stackForExpression 'push! #\3)
       (makeListFromSymbols(cdr myList))
       (newline)
       )
      ((eq? #\4 (car myList)) 
       (display "Equals 4") 
       (stackForExpression 'push! #\4)
       (makeListFromSymbols(cdr myList))
       (newline)
       )
      ((eq? #\5 (car myList)) 
       (display "Equals 5") 
       (stackForExpression 'push! #\5)
       (makeListFromSymbols(cdr myList))
       (newline)
       )
      ((eq? #\6 (car myList)) 
       (display "Equals 6") 
       (stackForExpression 'push! #\6)
       (makeListFromSymbols(cdr myList))
       (newline)
       )
      ((eq? #\7 (car myList)) 
       (display "Equals 7") 
       (stackForExpression 'push! #\7)
       (makeListFromSymbols(cdr myList))
       (newline)
       )
      ((eq? #\8 (car myList)) 
       (display "Equals 8") 
       (stackForExpression 'push! #\8)
       (makeListFromSymbols(cdr myList))
       (newline)
       )
      ((eq? #\9 (car myList)) 
       (display "Equals 9") 
       (stackForExpression 'push! #\9)
       (makeListFromSymbols(cdr myList))
       (newline)
       )
      ((eq? #\+ (car myList)) 
       (display "Equals +") 
       (handleAdditionOperator)
       (makeListFromSymbols(cdr myList))
       (newline)
       )
      ((eq? #\/ (car myList)) 
       (display "Equals /") 
       (handleDivisionOperator)
       (makeListFromSymbols(cdr myList))
       (newline)
       )
      ((eq? #\- (car myList)) 
       (display "Equals -") 
       (handleSubtractionOperator)
       (makeListFromSymbols(cdr myList))
       (newline)
       )
      ((eq? #\* (car myList)) 
       (display "Equals *") 
       (handleMultiplicationOperator)
       (makeListFromSymbols(cdr myList))
       (newline)
       )
      ((eq? #\( (car myList)) 
       (display "Equals (")
       (stack1 'push! #\( )

       (makeListFromSymbols(cdr myList))
       (newline)
       )
      ((eq? #\) (car myList)) 
       (display "Equals )") 

       (popAndTransferUntilLeftBracket) 

       (makeListFromSymbols(cdr myList))
       (newline)
       )
      (else (display "Character is invalid")(newline) (makeListFromSymbols(cdr myList)))
      )

  )

if anyone ever has similar questions on this, or about my code / functions, email me. ( i think my email is shown on my user page?)

i also have a pseudo code algorithm for conversion of infix to post fix notation (that was the assignment here)

Upvotes: 0

jacobm
jacobm

Reputation: 14015

The other answers are correct, but might not be what you want. The more idiomatic Scheme/Racket way to do this would be to write a function that consumes the original input, and returns a new, separate list that has the modifications you want. (This is strongly preferred, hence lists being immutable by default.) So, for instance, you might write

(define (numberify-head lst)
  (cond
    [(eq? (car lst) #\1) (cons 1 (cdr lst))]
    [(eq? (car lst) #\2) (cons 2 (cdr lst))]
    [else lst]))

Your program would then go something like

(let* ([input (read-input-from-the-user)]
       [processed-list (numberify-head input)])
  ;; ... code that uses processed-list ...
  )

Upvotes: 5

newacct
newacct

Reputation: 122419

Pairs in Scheme are mutable. But you don't mutate them using (set! (car myList) 1 ); rather, you would do (set-car! myList 1). You need to first find the pair that you want to mutate, and then call set-car! or set-cdr! on the pair (once you do car or cdr on it, that's just accessing the pair and not mutating it).

Upvotes: 0

C. K. Young
C. K. Young

Reputation: 222973

By default, Racket does not provide mutable pairs, and thus no mutable lists either. That means that the values of pairs and lists are unchangeable.

However, you can (require racket/mpair), which, as the name implies, provides mutable pairs. You then use mcons, mcar, mcdr, mlist, etc. instead of cons, car, cdr, list.

Upvotes: 7

Related Questions