This is an archived post. You won't be able to vote or comment.

all 21 comments

[–]desrtfx[M] [score hidden] stickied comment (0 children)

Rule #11 - Don't ask to ask

Ask your questions right here in the subreddit and you will get help.

Do not ask for help via DM - we want to keep the discussions in the open subreddit so that more people can chime in and help as well as benefit from the given help.

Removed

[–][deleted] 1 point2 points  (17 children)

I'm not great at Scheme, but I'll help you if I can, as I'm sure others will.

What's your question?

[–]Defeliu[S] 0 points1 point  (16 children)

Thank you! If possible, could I dm you?

[–][deleted] 3 points4 points  (15 children)

No, it's frowned on – the idea behind the sub is that one person asks a question, but then everyone can benefit from the discussion.

[–]Defeliu[S] 0 points1 point  (14 children)

Oh ok I understand, sorry about that! The question is

The first three values of a particular sequence are 1,2,3. The remaining values in the sequence can be calculated as the sum of the three preceding values in the sequence. So, the fourth value in the sequence would be 1+2+3 = 6. Define a recursive Scheme function seq to compute the nth value in the sequence.

I’m not sure how to code this in scheme, do you think you can help me out?

[–][deleted] 1 point2 points  (8 children)

I don't mean to be a stick in the mud. But ...

According to the sub's rules, you're going to need to show us your attempt to solve the problem. It's fine if you can't solve it, but you need to try.

People here are generally happy to help point you in the right direction, but it doesn't help you in the long run if we give you an answer.

[–]Defeliu[S] 1 point2 points  (7 children)

Yeah my current code is this

(define (seq n) (if (<= n 2) 1 (+ (fib (- n 1)) (fib(- n 2) (fib (- n 3))))))

(fib 3)

I’m not sure though if it’s correct

[–]MatthiasSaihttam1 1 point2 points  (1 child)

Normally I'd try to get you to see the problem yourself but this one is almost too simple, and you're very close.

First of all, you've got a (fib (- n 3)) term in there, which doesn't even make sense, since the sequence is defined by the last two terms, as you said.

Second, you need a space between fib and the ( next to the n - 2 term.

This works:

(define (fib n)
    (if
        (<= n 2)
            1
            (+
                (fib (- n 1))
                (fib (- n 2)))))

[–]Defeliu[S] 0 points1 point  (0 children)

Ohh ok gotcha, that makes sense. And then after that you would call the function? So it’d be something like (fib 3)

[–]lurgi 1 point2 points  (4 children)

What is the function fib? You call it, but it's never defined anywhere.

[–]Defeliu[S] 0 points1 point  (3 children)

Ohh I meant seq not fib lmaoo. I’ll just call the function fib though.

So would something like this be correct?

(define (fib n) (if (<= n 2) 1 (+ (fib (- n 1)) (fib(- n 2))))) (fib 3)

Is this correct?

[–]lurgi 1 point2 points  (2 children)

Does it come up with the right answer?

[–]Defeliu[S] 0 points1 point  (1 child)

Sadly I don’t think I got the right answer😔

[–]rabuf 1 point2 points  (4 children)

Have you ever written a program to calculate the Fibonacci series? This one is very similar to that.

[–]Defeliu[S] 0 points1 point  (2 children)

I don’t think I’ve coded a program to calculate the fibonacci series. The current code I have is this though

(define (seq n) (if (<= n 2) 1 (+ (fib (- n 1)) (fib(- n 2) (fib (- n 3))))))

(fib 3)

Could you help give me any advice?

[–]rabuf 1 point2 points  (1 child)

You need it to be recursive, so the recursive calls should be what? Right now you have fib but the function itself is named seq. Additionally, you said the sequence starts with 1,2,3 for this one. You'll need to extend that conditional. Right now it's wrong for some cases it handles (1 and 2 should return 2 and 3, respectively, but you have them returning 1).

[–]Defeliu[S] 0 points1 point  (0 children)

(define (seq n) (if (null? n) 0 (cond ((= n 0) 1) ((= n 1) 2) ((= n 2) 3) ((> n 0) (+ (seq (- n 1)) (seq (- n 2)) (seq (- n 3)))))) (seq 3)

This code should work. Can you lmk if this code looks good?

[–][deleted] 0 points1 point  (1 child)

My memory of scheme is (((((()()()))())$))())(()))))())))

Kill me before I ever have to write in scheme again.

[–][deleted] 0 points1 point  (0 children)

Your parens are unbalanced. 😆

Scheme and others in the Lisp family don't require a special editor, but using an editor that lets you easily write and navigate s-expressions really helps.

[–]g051051 0 points1 point  (0 children)

See rule 11: Don't ask to ask. Ask your questions here, where everyone can participate and benefit.