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

Pages: 1-

Functional or not?

Name: Anonymous 2010-12-23 17:29

Let's say I wrote a function like this:


(defun count-up-to (n)
  (let ((num 0))
    (labels ((count-up ()
               (when (> n num)
                 (format t "~A / ~A~%" (1+ num) n)
                 (incf num)
                 (count-up))))
      (count-up))))


Would this function be considered functional or not?

Name: Anonymous 2010-12-23 17:31

Aside from the format of course.

Name: Anonymous 2010-12-23 18:04

If I defecated like this:


OOOOHARRRRRRR BRPPPP EEEEUUUUGGHH PARRRRP FLLLOOOOP SPLOOOSH


Would this defecation be considered defecational or not

Name: Anonymous 2010-12-23 19:11

Aside from the PARRRRP of course.

Name: Anonymous 2010-12-23 20:08

>>4
My other PARRRRP is a PDRRRRP

Name: Anonymous 2010-12-23 21:17

>>1
incf when (format t)
My functional senses hurts.

Name: Anonymous 2010-12-23 22:22

>>6
Yes, that's why I'm asking.
The function is completely self-contained and doesn't change behavior based on any state outside of it and doesn't have any side-effects except for what it does internally (ignore format). So if you look at it as some kind of black box wouldn't that make it functional in a sense?

I probably should have left out the format from the start to make it more clear what I mean.

Name: Anonymous 2010-12-23 22:46

>>7
You also should not modify variables (incf), and a pure function must return a value and when the predicate in when is false, it returns void.

You should build a list of strings and then map format t to the list.

(define (count-up-to n)
  (let loop ((i 0)
         (r '()))
       (if (> n num) (reverse r)
       (loop (add1 i)
         (cons (format "~a / ~a~%" (add1 i) i) r)))))
(map (lambda (x) (display x)) (count-up-to 10))


Translating the scheme code to common lisp is left as exercise for the reader.

Name: >>8 2010-12-23 22:49

Read that as (map display (count-up-to 10))

Name: Anonymous 2010-12-23 23:10

>>1
If your function doesnt change global variables, you should be okey. Anyway, IRL all functions are "functional", they just replace whole global state with new state. So this "functional" term is just mathematical nonsense. But usually you want to decompose and localize changes, whenever possible.

Name: Anonymous 2010-12-23 23:26

>>10
Please, read your SICP.

Name: Anonymous 2010-12-23 23:49

>>11
SICP says my code is functional.

Name: Anonymous 2010-12-23 23:51

>>12
You forgot your sage, your opinion is invalid.

Name: Anonymous 2010-12-23 23:53

>>13
Oh okay.

>>11
SICP says my code is functional.

Name: Anonymous 2010-12-24 0:05

>>14
but do you wrote it with the standard editor?

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