# Question on recursive functions (Dr.Racket)

Page 1 of 1

## 4 Replies - 244 Views - Last Post: 29 November 2019 - 01:06 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=417942&amp;s=b22c2b61df3d95a765f894d85659807e&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 ITnoob2

Reputation: 0
• 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

• Suitor #2

Reputation: 15477
• 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?

### #3 ITnoob2

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

## Re: Question on recursive functions (Dr.Racket)

Posted 29 November 2019 - 08:58 AM

modi123_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

### #4 modi123_1

• Suitor #2

Reputation: 15477
• 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!

### #5 baavgai

• Dreaming Coder

Reputation: 7506
• 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.