4 Replies - 244 Views - Last Post: 29 November 2019 - 01:06 PM Rate Topic: -----

#1 ITnoob2   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 29-November 19

Question on recursive functions (Dr.Racket)

Posted 29 November 2019 - 08:00 AM

Hi peeps,

i am pretty new to functional programming, so this might be an obvious issue but I just dont find a solution myself.

So I predefined a "define-structure" whith types (element and priority)
element is a string and priority a number.

So now If I created a list of said structure arguments, I want to be able to update that list with a function that accepts a list and a new argument I want to add.

The catch is tho that the argument I want to add needs to be added to the exact position based on its priority number. So if my list had arguments with priority 5,4,2,1 and the argument I want to add has a priority of 3, the argument should be added between 4 and 2.
If the arguments prioirty number is equal to one of the numbers it should be added right behind that equal one.


So this is my function :

(define (update lst argument)
  (cond  ((not(structure-type? argument)) lst)
         ((= (structure-type-priority argument) (structure-type-priority (first lst)))(cons (first lst)(cons argument (rest lst))))
         ((not(= (structure-type-priority argument) (structure-type-priority (first lst))))(update (rest lst) argument))))



This works unless I add and argument which priority is equal to the first argument of the lst I want to update. But if the arguments priority number is equal to a lst priority number other than the first argument of list it will only print the list starting from the one which was equal because I lost the other ones through the recursion condition.

I hope I was able to get my problem across even tho my english isnt the best.

Thank you for reading and I hope you can help me.

Is This A Good Question/Topic? 0
  • +

Replies To: Question on recursive functions (Dr.Racket)

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15477
  • View blog
  • Posts: 62,007
  • Joined: 12-June 08

Re: Question on recursive functions (Dr.Racket)

Posted 29 November 2019 - 08:20 AM

Never heard of 'Racket', and just curious if this is a class requirement or personal choice. If the latter, why?
Was This Post Helpful? 0
  • +
  • -

#3 ITnoob2   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 29-November 19

Re: Question on recursive functions (Dr.Racket)

Posted 29 November 2019 - 08:58 AM

View Postmodi123_1, on 29 November 2019 - 08:20 AM, said:

Never heard of 'Racket', and just curious if this is a class requirement or personal choice. If the latter, why?


Its a requirement of my university. We concluded java. Now we are programming with racket
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15477
  • View blog
  • Posts: 62,007
  • Joined: 12-June 08

Re: Question on recursive functions (Dr.Racket)

Posted 29 November 2019 - 09:00 AM

Nifty. I guess LISP and Scheme don't cut the mustard anymore! :D
Was This Post Helpful? 0
  • +
  • -

#5 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7506
  • View blog
  • Posts: 15,556
  • Joined: 16-October 07

Re: Question on recursive functions (Dr.Racket)

Posted 29 November 2019 - 01:06 PM

Odd, Racket used to be just a nice Scheme REPL, but looks like it's gotten a little full of itself since last time I played with it.

Without knowing the rest of the functions in play here, I really couldn't guess. The not for the prior copy paste evaluation seems a little smelly. When in doubt, make more, smaller, functions, and test them separately.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1