racket: Why doesn't apply work with "bitmap" function?

racket shebang
racket executable
racket lang
racket shell
racket clear console
racket startup time
learn racket
racket app

I have been cautioned against using eval in my code. However, in this bit of racket code, I can get eval working but not something more recommended, like apply. Here is the code:

(require 2htdp/image)
(define (get_img filename)
  (let ([img (eval `(bitmap ,filename))])
    (image->color-list mask)
  ))

I tried doing the naive replace of eval with apply:

(require 2htdp/image)
(define (get_img filename)
  (let ([img (apply `(bitmap ,filename))])
    (image->color-list mask)
  ))

and when I run it I get:

; apply: arity mismatch;
;  the expected number of arguments does not match the given number
;   expected: at least 2
;   given: 1
; [,bt for context]

I have tried a few permutations of this code, but to no avail. I was hopeful this one

(let ([img (apply bitmap `(filename))])
 (image->color-list img)`)

would work, but clearly there's still something I'm not understanding

EDIT:

The first thing I tried, with error message:

> (require 2htdp/image)
> (define (get_img filename)
    (let ([img (bitmap filename)])
      (image->color-list img)))
; readline-input:6:15: bitmap: expected a path with a / in it
;   in: (bitmap filename)
; [,bt for context]

Another failed attempt:

> (define (get_img filename)
    (let ([img (apply bitmap (list filename))])
      (image->color-list mask)))
; readline-input:16:20: bitmap: bad syntax
;   in: bitmap
; [,bt for context]

You're using it wrong. So behind the variable + there is a procedure object which can be applied. These are equal:

(+ (* 2 3) 5)              ; ==> 11
(apply + (list (* 2 3) 5)) ; ==> 11
(apply + `(,(* 2 3) 5))    ; ==> 11

In your example you are using bitmap and it isn't a procedure at all but a macro and it seems it is to get bitmaps from a racket package and with strings it expects at least a slash since images should't be in the package root. You should replace it with bitmap/file which is a procedure and takes a file path absolute or relative to current working directory instead.

In your example (apply `(bitmap/file ,filename) you are passing apply a list as first argument instead of a procedure object and a last argument with parameters.

In your example (apply bitmap/file `(filename)) you are applying bitmap with a literal list with a symbol filename which has nothing to do with the variable with the same name. You were so close since I think you wanted (apply bitmap/file `(,filename)) which is a funny way of doing (apply bitmap/file (list filename)). What I don't understand is why can't you just do this:

(define (get_img filename)
  (let ([img (bitmap/file filename)])
    (image->color-list mask)))

for loop in racket doesn't seem to work for me, use the racket language: #lang racket (for ([i '(1 2 3)]) (display i)) Welcome to DrRacket, version 5.2.1 [3m]. Language: racket [custom]; memory  I created Pollen with the Racket program­ming language. Racket is a descen­dant of Scheme, which in turn is a descen­dant of Lisp. So while Racket is not Lisp (in the specific Common Lisp sense), it is a Lisp (in the familial sense). Its core ideas—and core virtues—are shared with Lisp.

The combination apply and bitmap doesn't work together because bitmap is not a function. Note that the entry in the documentation on bitmap says "Syntax" and not "procedure".

If f is a function, then (apply f (list a b c)) will compute (f a b c). However, bitmap is not a function, it is a "special form".

You are in luck though, because bitmap/file is a function, so you can use that instead.

#lang datalog doesn't work in racket-repl-mode buffer · Issue #305 , On #racket IRC @odanoburu reports that #lang datalog works fine in DrRacket, a DrRacket interactions REPL, and in racket-mode buffer --- but  Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. Learn more racket: Why doesn't apply work with “bitmap” function?

21.1 Running racket and gracket, Depending on command-line arguments, racket or gracket runs in interactive you would need to protect the flags with a --, so that racket doesn't try to parse  >If when you plug the racket to your computer there is a Red or Green light (And it's a racket you just bought or that you didn't use for a while) but the racket still doesn't switch on: The battery of the racket might be in safety mode. To revive the battery you need to do two charging cycles of 3 hours each.

My racket doesn't switch on, Why? – Babolat, If when you plug the racket to your computer there is no Red or Green light : The USB Port of the computer where you plug the Racket is not a very popular language in teaching communities. The list of schools using Racket for CS1 is pretty short compared to other popular languages. This github page lists less than a dozen CS1 courses. The schools that do use it are not particularly notable - old adopters like MIT have since moved on to other languages (i.e., Python).

Learn racket in Y Minutes, Racket is a general purpose, multi-paradigm programming language in the #f (​and 0 #f (error "doesn't get here")) ; => #f (or #f 0 (error "doesn't get here"))  The reason Racket doesn't include it out of the box is likely just because it adds a lot of extra primitives without much benefit. I will admit that a lot of languages do have != for not equal, but even in Java, if you want to do a deep equality check using equals() (analogous to equal? in Racket), you have to manually invert the result with a ! yourself.

CSE 341 -- Racket Basics, In contrast to Haskell, Racket doesn't use lazy evaluation. Examples: (+ 2 3) (abs -4) (+ (* 2 3) 8) (+ 3 4 5 1) ;; note that  Racket is a de­scen­dant of Scheme, which in turn is a de­scen­dant of Lisp. An up­dated ver­sion of this piece is part of Beau­ti­ful Racket, my book about mak­ing pro­gram­ming lan­guages with Racket. So while Racket is not Lisp (in the spe­cific Com­mon Lisp sense), it is a Lisp (in the fa­mil­ial sense)

Comments
  • Looking at the docs, it seems like (let ([img (bitmap/file filename)]) ...) should work. bitmap is a macro that treats symbols differently from literal strings.
  • In fact, your (apply bitmap (list filename)) suggestion didn't quite work either