147pm
147pm

Reputation: 2233

ML version of anonymous map inside let?

I've learned in Scheme and Lisp how to do a let that encases a map that takes an anonymous (lambda) function and list and gives back a "worked on by the function" list or answer. Can someone show me a bare-bones version of this in SML?

Update:

Here's a very simple map and anonymous function combo in Scheme:

(map (lambda (x) (+ x 1)) ’(4 5 8))

which produces the list

 ’(5 6 9)

Again, this is very common.

Here's a Lisp function using all three:

(defun sequence1_v1 (vallist)
     (let ((a 4))
       (mapcar #'(lambda (val)
                   (/ (+ (* -3 val a) 10) (+ (* (expt val 2) (expt a 2)) 1)))
               vallist)
       ))

The math is just a sequence generator and the value of a set to 4 means the 4th element of the sequence. If we ask for which of the following values of k will a_4 be positive, −2, −1, 0, 1, or 2, we have

(sequence1_v1 '(-2 -1 0 1 2)) ->

(34/65 22/17 10 -2/17 -14/65)

So the first three are positive. . . . So yes, the Lisp/Scheme world does this sort of thing a lot. Just wondering how this would look in ML.

Upvotes: 1

Views: 137

Answers (1)

molbdnilo
molbdnilo

Reputation: 66459

If your general form you want to translate is (in Scheme)

(define (name ls)
     (let (<bindings>)
       (map <function> ls)))

then it looks almost exactly the same in SML, but with different punctuation:

fun name ls = let <bindings> 
              in 
                  map <function> ls 
              end

And the anonymous function (lambda (parameters) body) is fn parameters => body.

(map (lambda (x) (+ x 1)) ’(4 5 8))

in SML:

map (fn x => x + 1) [4,5,8]

Your Lisp example, translated:

fun sequence1_v1 ls = 
    let val a = 4
    in
        map (fn v => (-3 * v * a + 10) / ((pow(v, 2) * (pow(a,2)) + 1)) ls
    end 

Upvotes: 1

Related Questions