Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Functions that churn out lambdas - when?

Name: Anonymous 2009-09-12 1:29

I just wrote my own "repeated" implementation (SICP lecture 3x), and I thought the same could have been done with something of the format (repeated procedure item times), instead of dealing with another layer of abstraction by churning out a procedure that does the actual iteration ((repeated procedure times) item). At this point, choosing between both forms seems like an arbitrary flip of the coin - can anyone on /prog/ enlighten me?

Name: Anonymous 2009-09-12 1:52


(define (repeated' procedure item times)
  ((repeated procedure times) item))


But you have no idea how to order arguments, and I suggest:

(((repeated times) procedure) item)

Since scheme doesn't automatically curry :(

Name: Anonymous 2009-09-12 3:53

[/code]
(defun repeated (f n)
  (lambda (item)
    (loop repeat n do (funcall f item))))
[/code]

Name: Anonymous 2009-09-12 8:03

Or:

(defmacro repeated (f n)
  `(lambda (x)
     (progn ,@(make-list n
                          :initial-element `(,f x)))))

Name: Anonymous 2009-09-12 8:11

(and if you must absolutely do it like >>2)

(defmacro repeated (n)
  `(lambda (f)
      (lambda (item)
        (dotimes (n ,n) (funcall f item)))))

Note you can use &rest items and APPLY instead of FUNCALL (but then f might be called as an 0-ary function).

Use:

(funcall (funcall (repeated 10) #'princ) '|SICP |)
SICP SICP SICP SICP SICP SICP SICP SICP SICP SICP
NIL

Never said it's going to look pretty!

Name: Anonymous 2009-09-12 8:14

There's a difference between the code from >>3 and >>4.
>>3's should return NIL, while >>4's will return the value of the last call to f.

Name: Anonymous 2009-09-12 8:19

>>5
I don't see the benefit of using a defmacro here? Why not use a regular function and declare it to be inlined, it should yield the same behaviour.

Name: Anonymous 2009-09-12 8:37

>>1
Both can be expressed in terms of each other, but unless you have a good reason just go with the simplest one.
The form in SICP is good for teaching abstract bullshite.

Also, SRFI 26 is better than automatic currying.

Name: Anonymous 2009-09-12 8:40

>>6
REPEATED will not be used with a functional form obviously: you are interested in the side-effects, not the value computed.

>>7
There is none. I nonetheless implemented it as a macro.

Name: Anonymous 2009-09-12 13:51

>>1,5
This really emphasizes the fact that LIFP (Lisp Isn't Functional Programming).

Name: Anonymous 2009-09-12 14:17

>>10
No, this really emphasizes the fact that common lisp isn't a LISP1; in scheme you can write (((repeated times) procedure) item).

Name: Anonymous 2009-09-12 14:22

>>11
I saw someone make a CL extension which let you use results from function in the CAR of the S-Expression, this would basically make the code (((repeated times) procedure) item) valid, and still keep it a LISP2. I'm not sure why they explicitly forbid this behaviour when they designed ANSI CL. Personally, I don't mind FUNCALL and APPLY at all, as they provide useful visual cues, but I don't understand their decision to forbid that case.

Name: Anonymous 2009-09-12 14:53

>>12

I saw someone make a CL extension which let you use results from function in the CAR of the S-Expression, this would basically make the code (((repeated times) procedure) item) valid, and still keep it a LISP2.
Either the extension was limited (therefore rendered pointless by the commitee), or it introduced other limits in the language (incompatible with the rest design).

((foo bar) baz) is just syntactic sugar for me and I don't need it, because the occasions I do this are very rare.

but I don't understand their decision to forbid that case.
I don't know the answer. You can ask in #lisp on irc.freenode.org or in <news:comp.lang.lisp>.

Name: Anonymous 2009-09-12 15:08

>>13
((foo bar) baz) is just syntactic sugar for me and I don't need it, because the occasions I do this are very rare.
In other words, LIFP!

Name: Anonymous 2009-09-12 15:30

>>14
What's the matter, too lazy to type a funcall or apply? Having 2 namespaces doesn't make it less functional, it just makes the syntax for calling functional values just slightly longer. Lisp never claimed to be purely fictiunctional, it allows you that and much more. If we didn't have Lisp-2, macros would be harder to write as reliably, and that's a price I'm willing to pay. Lisp-2 also makes code using functions more clear to understand visually.

Name: Anonymous 2009-09-12 16:31

LIFP Isn't Functional Programming

Name: Anonymous 2009-09-12 16:35

>>14
No, it is not the case that CURRYing rarely occurs in the language, (on the contrary), but that you don't need THIS syntax for it. LIFP isn't true, the functional paradigm can be embraced in either CL or scheme.

>>16
About as true as "TIRA isn't a recursive acronym".

Name: Anonymous 2009-09-12 16:59

But to get back to OPs question:

When you have nested lambdas like that, you might ask yourself what the utility does and what is the most natural way of going about it, in accordance to the rest of your utilities and style you've adopted. So, for instance, given that (((repeated times) proc) item) repeats a (proc item) form 'times' times, I'd design it as:

(define (repeated proc item times)
  (if (> times 0)
    (begin
      (proc item)
      (repeated proc item (- times 1)))))


or in its more general form (to execute a block of forms N times), like this:


; this is common lisp because I don't know anything about scheme macros
(defmacro repeated (times &rest forms)
  `(progn
     ,@(make-list times :initial-element `(progn . ,forms))))


and if you're willing to go even more general, you'll write REPEATED like this:
(repeated (3 (form1) (form2)) (5 (form1) (form4)))


(defmacro repeated (&rest args)
  `(progn                
     ,@(mapcar (lambda (x)
                 `(progn
                    ,@(make-list (car x)                              
                                 :initial-element `(progn ,@(cdr x)))))
               args)))

Now you are able to execute arbitrary blocks of code arbitrary times in sequence, for instance:


(repeat (3 (princ 'X)) ; print X

Name: Anonymous 2009-09-12 17:04

>>18
The last code didn't get through, hit the submit too early.

(repeated
  (3 (princ 'X)) ; print XXX
  (2 (princ '|random number: |)
     (princ (random 10))))
==> XXXrandom number 6random number 0

Name: Anonymous 2009-09-13 0:53

>>16
LISP Isn't Serious Programming

Name: Anonymous 2010-11-28 7:47

Name: Anonymous 2011-02-03 12:14

Bump to remind about the times when there were threads with a lot of LISP CODE in it.

Name: Anonymous 2011-02-03 12:15

>>20
LISP Is Satori Programming

>>23
I still post LISP CODE.

Name: Anonymous 2011-02-03 12:52

>>24
Your ````in'' ``Lisp'''' DSL doesn't count.

Name: Anonymous 2011-02-03 12:54

>>25
I'm not the ````in Lisp'' guy'', I'm LISPPER.

Name: Anonymous 2011-07-09 2:46

yo nigga I write programs that write programs

Name: Anonymous 2011-07-09 2:59

lol

Name: Anonymous 2011-07-09 13:26

read Let Over Lambda.

Name: Anonymous 2011-07-09 13:36

fuck off and die lithpfags

Name: Anonymous 2011-08-02 20:02

>>30
Back to /b/, please

Don't change these.
Name: Email:
Entire Thread Thread List