1

My target is do define list as local variable in order to get max element from it. My code:

#lang racket
(define (f a b c)
    (list (+ (* a a) (* b b) (* c c)) (+ a c))
    (define (max-of-list-2 lst)
      (foldr max (first lst) (rest lst)))
  (max max_from_list 12))
(f 5 6 7)

In second row I have defined list with calculated falues. My target is to pass it to next row in order to get max number from it, and finaly to get max from max of list and 12. What I do wrong. How to handle it?

Will Ness
  • 70,110
  • 9
  • 98
  • 181
IntoTheDeep
  • 4,027
  • 15
  • 39
  • 84
  • https://www.gnu.org/software/mit-scheme/documentation/mit-scheme-ref/Lexical-Binding.html – bipll Apr 28 '19 at 13:30
  • name it with another define: `(define lst (list ...))`. then you can refer to it by name in any lisp expression, like e.g. a function call. – Will Ness Apr 28 '19 at 13:53
  • but I cannot call (max max-of-list-2 (lst) 12) – IntoTheDeep Apr 28 '19 at 14:08
  • @IntoTheDeep what about `(max (max-of-list-2 lst) 12)`? @IntoTheDeep – Will Ness Apr 28 '19 at 14:30
  • @WillNess please add this as an answer – IntoTheDeep Apr 28 '19 at 14:35
  • 1
    @IntoTheDeep It's difficult for us to take your non-working code and work out your actual intentions. Can you please describe the problem better? Can you think of a better name than `f`? What is the return value supposed to be? What is the point of the `(* a a)` `(* b b)` `(* c c)` and `(+ a c)` values? Is this just an assignment? – Mulan Apr 28 '19 at 15:30
  • For `(max max-of-list-2 (lst) 12)` to work `lst` cannot be a list, but a procedure. You'll get [application: not a procedure](https://stackoverflow.com/questions/48064955/my-code-signals-the-error-application-not-a-procedure-or-call-to-non-procedu) – Sylwester Apr 28 '19 at 23:39

3 Answers3

1

You can use several defines at the top of your function.

So you meant

(define (f a b c)
  (define lst (list (+ (* a a) (* b b) (* c c)) 
                    (+ a c)))
  (define (max-of-list-2 lst)
      (foldr max (first lst) (rest lst)))
  (max (max-of-list-2 lst) 12))

but that's just equivalent to

(define (f a b c)
  (foldr max 12 (list (+ (* a a) (* b b) (* c c)) 
                      (+ a c))))
Will Ness
  • 70,110
  • 9
  • 98
  • 181
0

You need to use an inner define, like this:

(define (f a b c)
    (define max_from_list (list (+ (* a a) (* b b) (* c c)) (+ a c)))
    (define (max-of-list-2 lst)
      (foldr max (first lst) (rest lst)))
  (max max_from_list 12))
river
  • 1,028
  • 6
  • 16
0

It's somewhat difficult to work out your intentions, but this is the best understanding I can come up with -

(define (max a b)
  (if (< a b)
      b
      a))

(define (f a . more)
  (if (empty? more)
      (max a 12)
      (max a (apply f more))))

(f 5 6 7)
; => 12

(f 5 20 30)
; => 30
Mulan
  • 129,518
  • 31
  • 228
  • 259