1

I'm a C# programmer learning Scheme, and I have a lot of troubles, because I don't understand Scheme.

I have write it this code with a lot of help:

#lang racket

(define sort-asc-by-second
  (lambda (lst)
    (sort lst
          (lambda (x y) (< (cdr x) (cdr y))))))

(define sum
  (lambda (lst)
    (apply + (map cdr lst))
  )
)

(define my-function
  (lambda (lst)
   (
    (define sorted (sort-asc-by-second lst))
    (define suma (sum lst))
    (define lista (map (lambda (p) (cons (car p) (/ (cdr p) suma)))))
   ))
)

But I get the following error:

define: not allowed in an expression context in: (define sorted (sort-asc-by-second lst))

At this line:

(define sorted (sort-asc-by-second lst))

How can I declare a variable? Maybe, the problem here is that I'm a C# programmer and I don't understand Scheme.

VansFannel
  • 45,055
  • 107
  • 359
  • 626
  • local might be what you want. I'm not really sure what you're trying to do though. – CoffeeTableEspresso Oct 19 '18 at 00:01
  • Programming in Scheme in very different from something like C#. If you try to program in Scheme like you would in C# you're in for a world of pain. – CoffeeTableEspresso Oct 19 '18 at 00:02
  • @CoffeeTableEspresso Thanks, but your comments don't help so much. You could add books or tutorials instead of saying the obvious. – VansFannel Oct 19 '18 at 05:48
  • Just a note, it looks like you're learning Racket, not Scheme, based on the first line of your code. The two are very similar though, as described here: https://stackoverflow.com/questions/3345397/how-is-racket-different-from-scheme. Are you specifically trying to learn Scheme for something, or just any LISP dialect? – CoffeeTableEspresso Oct 19 '18 at 17:19
  • @CoffeeTableEspresso I have to learn Scheme, but my professor said that we have to use DrRacket. But there isn't any `#lang Scheme` in DrRacket. Maybe there is another interpreter for Scheme that I can use. – VansFannel Oct 19 '18 at 18:41
  • 1
    If you are using DrRacket then there's a good chance you are learning Racket, since DrRacket is a Racket IDE. The professor might be referring to it as Scheme since the old name for Racket is `PLT Scheme`, and the two languages are very similar, especially at a beginner level. But I can't say for certain without seeing your course material. Also, does the textbook for your course not cover enough? – CoffeeTableEspresso Oct 19 '18 at 18:46
  • @CoffeeTableEspresso He said that we can learn Scheme with the book: Teach Yourself Scheme in Fixnum Days. I have asked about if we are going to use Scheme or Racket, and he said Scheme. – VansFannel Oct 19 '18 at 18:49

2 Answers2

2

I have no idea what this program is trying to do, but the current error you are seeing is due to an extra pair of brackets.

(define my-function
  (lambda (lst)

    (define sorted (sort-asc-by-second lst))
    (define suma (sum lst))
    (define lista (map (lambda (p) (cons (car p) (/ (cdr p) suma)))))
    )
  )
merlyn
  • 2,273
  • 1
  • 19
  • 26
2

I think it's going to be very important for you to follow the steps of the Design Recipe. In this case:

  • write a signature: the types that the function takes in and returns.
  • Write test cases, using check-equal?.
  • Follow the template for the data that you've chosen.

Put differently (and perhaps a bit more bluntly): stop wandering. Start designing!

John Clements
  • 16,895
  • 3
  • 37
  • 52