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

Pages: 1-

Bootleg /prog/ challenge: NestList Function

Name: Anonymous 2018-01-09 8:59

Quiz. write a function \(r(f,x,n)\) that returns a list \([f(x), f(f(x)), f(f(f(x))), ...]\), length \(n\).

Name: Anonymous 2018-01-09 9:12

function r(f,x,n)
return [f(x), f(f(x)), f(f(f(x))), n];

Name: Anonymous 2018-01-09 10:30

def y(f,x,n):a=[f(x)];exec("a.append(f(a[-1]));"*(n-1));return a

Name: Anonymous 2018-01-09 12:14

(define (r f x n)
(if (= n 0)
'()
(append (list (f x)) (r f (f x) (- n 1)))))

Name: Anonymous 2018-01-09 12:17

Even better:
(define (r f x n)
(if (= n 0)
'()
(cons (f x) (r f (f x) (- n 1)))))

Name: Anonymous 2018-01-09 12:29

(define(r f x n)(if(= n 0)'()`(,(f x).,(r f(f x)(- n 1)))))
ZOMG OPTIMISED!!!1

Name: Anonymous 2018-01-09 12:53

r(f,x,n) = take n $ tail $ iterate f x

Name: Anonymous 2018-01-09 19:17

Forth does not have lists or functions, but here you go anyway:
\(f\) will be the XT of any word, results are left on the stack.

VARIABLE F
VARIABLE X
VARIABLE N

: R
N ! X ! F !
N @ 0 DO
X @ F @
EXECUTE
DUP X !
LOOP ;

Name: Cudder !cXCudderUE 2018-01-10 1:26

; eax:x
; ecx:n
; ebx:f(eax)->eax
; edi:list[n] out
r:
jecxz rret
rloop:
call ebx
stosd
loop rloop
rret:
ret

Name: Anonymous 2018-01-10 1:41

(defun nst-lst (f x n)
(do ((v (make-array n :fill-pointer 0))
(acc (funcall f x) (funcall f acc)))
((null (vector-push acc v)) v)))

Name: Anonymous 2018-01-10 1:50

function nstLst(f, x, n) {
return n > 0 ? [f(x), ...nstLst(f, f(x), n-1)] : []
}

function nstLst2(f, x, n) {
let xs = [f(x)]
for (; n > 0; n--, xs.push(f(xs[xs.length-1])));
return xs
}

Name: Anonymous 2018-01-10 17:12

r(f,x,n+1) = x' : r(f,x',n) where x' = f x
r _ = []

Name: Anonymous 2018-01-10 18:07

>>12
Haskell is illegal to use in /prog/ challenges.

Name: Anonymous 2018-01-10 19:04

>>13
/prog/
Good thing that the rules were not written by a Redditor.

Name: Anonymous 2018-01-10 23:21

As soon as she touched my penis I came and started farting
not cool

Name: Anonymous 2018-01-10 23:27

>>15
Who the hell are you quoting? lol

Name: Anonymous 2018-01-11 13:19

>he doesn't know who I am quoting

Name: Anonymous 2018-01-11 13:20

>>17
who are you quoting?

Name: Anonymous 2018-01-11 13:20

>>18
Stop replying to yourself, it's sad and pathetic.

Name: Anonymous 2018-01-11 13:21

I need these dubs

Name: Anonymous 2018-01-11 13:21

check my dubs

Name: Anonymous 2018-01-11 13:21

>>19
but I'm not >>17, I only quote when I know who I'm quoting.

Name: Anonymous 2018-01-11 13:21

Fuck off

Name: Anonymous 2018-01-11 13:22

>>22
faggot you ruined these perfectly good dubs too

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