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

C is also a functional programming language

Name: Anonymous 2013-11-18 10:44

Why not? I can just avoid side-effects. I can also treat functions as first-order data using Function pointers.

Name: Anonymous 2013-11-18 11:37

Of course.

Name: Anonymous 2013-11-19 21:07

functional programming is not about first-class functions
it's about code and algorithms composability
https://github.com/openj/core these sources is still pretty fun

Name: Anonymous 2013-11-19 23:21

it's kind of hard to write malloc/free like code in purely functional style. Also tail call optimization can't be depended on. gcc lets you define functions within functions so you can sort of get that lexical scoping but it isn't portable to other compilers and you shouldn't lock yourself in to gcc.

Name: Anonymous 2013-11-20 7:56

How do you represent purely functional data structures in functional-style C?

Name: You got to pick 'em all 2013-11-20 10:26

Name: Anonymous 2013-11-20 10:28

Name: Anonymous 2013-11-20 11:50

>>1
I can also treat functions as first-order data using Function pointers.

That's now what higher order functions mean, baka gaijin!!!

Name: Anonymous 2013-11-20 16:10

>>3
Even the most mediocre C programmer can implement currying and composure simply by hacking with the C ABI.

Name: Anonymous 2013-11-20 17:37

>>8
Isn't it, though? I always thought ``higher order function'' just meant ``A function that has inputs and/or outputs that are functions themselves''. In C, you do that with function pointers. Are OP and I mistaken?

Name: Anonymous 2013-11-20 21:00

>>10
How do you even do partial application, currying and point-free in C?

Name: Anonymous 2013-11-20 21:06

>>11
By representing a closure as a structure with the args that have been filled in plus the function pointer. Every time you curry another argument onto the function, you fill in another field of the arg structure, and when they're all completed you apply the closure. It won't give you the sexy syntax of Haskell where making a closure is the same as just stopping a function call midway through, but it will allow you to fill in arguments in any order and modify arguments all you want prior to actual application.

Name: Anonymous 2013-11-20 21:11

>>11
How about use a search engine, bud, like >>6,7 exemplified:
http://asg.unige.ch/site/papers/Dami91a.pdf
There's more @ news:comp.lang.functional

Name: Anonymous 2013-11-22 5:53

C is also a object-oriented programming language.

Why not? I can treat structs as classes in their own source files and use function pointers as methods.

Name: http://is.gd/Pznipt 2013-11-22 10:19

>>14
Indeed, without the overhead of recursion of virtual functions.

Name: Anonymous 2013-11-23 7:14

>>14
Now you're getting it. Although I'd advise not actually using function pointers except in cases where you really need polymorphism, and instead just call the damn functions. You're going to need to pass in a Thing *self parameter anyway, and as long as you respect the message passing abstractions, you can get all the ``benefits'' of OOP.

Name: Anonymous 2013-11-23 18:39

>>1

That's not usually what "functional programming language" is used to mean because going by this logic you could claim that any programming language is functional, imperative, declarative, object oriented, or anything else. Because they are (almost) all turing complete.

Name: Anonymous 2013-11-24 7:39

>>17
No - that's not the logic OP was using at all. Transforming a program
from C to Haskell via Turing Completeness says absolutely nothing
about the style of programming used to create the original C
program. Furthermore, OP (and others since) have made valid points
about viability of using functional programming with C, and these
points cannot be transferred to an arbitrary language like
BASIC or Brainfuck through Turing Completeness.

Name: Anonymous 2013-11-24 8:54

>>18

I see nothing in this thread that can't be translated to BASIC or any other imperative programming language. There are ways around even the lack of function pointers, for example using a dispatch table, and once you allow manually closing over free variables with structures as "functional programming" I don't see what makes handcoding a dispatch table any different.

Name: Anonymous 2013-11-24 23:49

>>19
That's actually my point - that dispatch tables aren't practical for
actually using functional programming, while closure structs of
function pointers + args are. I've actually used the latter, and I
find the former laughable. Have you used both for non-trivial
projects?

Name: Anonymous 2013-11-25 8:59

>>20

I have used both, manual closures are definitely less inane but I don't think it's a valid point, it doesn't make C a functional programming language. It would be like claiming that C is purely functional because you pinky swear to never change variables after initialization.

Name: Anonymous 2013-11-25 10:30

>>21
BUT IT'S A PINKY SWEAR

Name: Anonymous 2013-11-25 19:49

>>21
That way lies pointless declassification, in which no language can be described as following any paradigm. Of course, that's as it should be, since it is programmers who follow such things, not languages, but for the wrong reason.

Name: Anonymous 2013-11-28 12:11

If Haskell was a purely functional programming language (where moving registers isn't involved) then why is it implemented using C? Checkm8 Haskellers.

Name: Anonymous 2013-11-29 5:09

>>24
then why is it implemented using C
Source?

Name: Anonymous 2013-11-29 6:12

>>25
Haskell is an interpreted language written in C and Perl.

Name: Anonymous 2013-11-29 20:40

>>24,26
He means, [citation needed]

Name: Anonymous 2013-11-30 16:40

If Haskell is a functional language, why can I write a NON-functional C program that is equivalent to a Haskell program by Turing Completeness?

Checkmate!

Name: Anonymous 2013-12-01 4:38

>>1
Let's see your loeb written in a functional manner in C.

-- requires import Control.Monad.Instances
let loeb x = fmap ($ loeb x) x in

Name: Anonymous 2013-12-09 12:14

>>1

Yes. You can implement Haskell, using C/C++, but you can't implement C/C++, using Haskell.

Name: Anonymous 2013-12-09 13:23

>>30

Congratulations on misunderstanding the post, retard

Name: Anonymous 2013-12-09 20:21

>>30
You can implement C/C++ in any Turing complete language. Haskell is a Turing complete language. The standards for C/C++ don't specify anything on an implementation detail level, you can make a C interpreter that uses hash tables for automatic variables and it will still be C for all intents and purposes, as far as the standard is concerned.

C is C when it is parsed and run according to the rules of whatever C standard you're abiding to, C isn't C because of how it is implemented.

In short,
YA AIN'T READ DA STANDARD STACK BOY

Name: Anonymous 2013-12-16 7:25

>>29

Here is part 1. Part 2 will use all sigle variable lambdas. Part 3 will be in C.


(define id (lambda (x) x))

(define true (lambda (x y) x))

(define false (lambda (x y) y))

(define cons (lambda (x y) (lambda (f is-nil) (f x y))))

(define nil (lambda (f is-nil) (is-nil)))

(define do-nothing (lambda () nil))

(define car
(lambda (x is-nil ret)
(x (lambda (xcar xcdr) (ret (xcar)))
is-nil)))

(define cdr
(lambda (x is-nil ret)
(x (lambda (xcar xcdr) (ret (xcdr)))
is-nil)))

(define zero nil)

(define increment
(lambda (x)
(cons (lambda () zero)
(lambda () x))))

(define decrement cdr)

(define add
(lambda (x y)
(decrement y
(lambda () x)
(lambda (y-dec)
(add (increment x) (y-dec))))))

(define sub
(lambda (x y is-positive is-zero is-negative)
(decrement x
(lambda ()
(decrement y is-zero is-negative))
(lambda (x-dec)
(decrement y
(lambda () (is-positive x))
(lambda (y-dec)
(sub (x-dec) (y-dec) is-positive is-zero is-negative)))))))

(define disp1
(lambda (lis)
(lis (lambda (car cdr)
(disp (car))
(disp2 (cdr)))
do-nothing)))

(define disp2
(lambda (lis)
(lis (lambda (car cdr)
(display " ")
(disp (car))
(disp2 (cdr)))
do-nothing)))

(define disp
(lambda (lis)
(display "(")
(disp1 lis)
(display ")")))

(define prin
(lambda (lis)
(disp lis)
(newline)))

(define len1
(lambda (lis acc)
(lis (lambda (car cdr)
(len1 (cdr) (increment acc)))
(lambda () acc))))

(define len
(lambda (lis)
(len1 lis zero)))

(define fmap
(lambda (f lis)
(lis (lambda (car cdr)
(cons (lambda () (f (car)))
(lambda () (fmap f (cdr)))))
(lambda () nil))))

(define loeb
(lambda (lis)
(fmap (lambda (f)
(f (loeb lis)))
lis)))

;(prin zero)
;(prin (increment zero))
;(prin (increment (increment zero)))
;(prin (cons (lambda () (cons (lambda () nil) (lambda () nil)))
; (lambda () (cons (lambda () nil)
; (lambda () nil)))))
;(prin (fmap increment (increment (increment zero))))
(prin (loeb (cons (lambda () len)
(lambda ()
(cons (lambda () len)
(lambda ()
(cons (lambda () len)
(lambda () nil))))))))

Name: Anonymous 2013-12-17 15:48

>>1
man 2 read

Game, set and match.

Name: Anonymous 2013-12-17 16:36

>>33
where's part 2, african american

Name: Anonymous 2013-12-17 18:55

>>34
What does the POSIX specification for a system call have to do with C being used for functional programming?

Name: Prolog 2013-12-17 19:02

>>36
Yes.

Name: Anonymous 2013-12-19 4:34

Part 2


(define id (lambda (x) x)) ;; lda_id_1

(define unit id)

(define cons
(lambda (x) ;; lda_cons_1
(lambda (y) ;; lda_cons_2
(lambda (f) ;; lda_cons_3
(lambda (is-nil) ;; lda_cons_4
((f x) y))))))

(define nil
(lambda (f) ;; lda_nil_1
(lambda (is-nil) ;; lda_nil_2
(is-nil unit))))

(define do-nothing
(lambda (unit) nil)) ;; lda_do_nothing_1

(define car
(lambda (x) ;; lda_car_1
(lambda (is-nil) ;; lda_car_2
(lambda (ret) ;; lda_car_3
((x (lambda (xcar) ;; lda_car_4
(lambda (xcdr) ;; lda_car_5
(ret (xcar unit)))))
is-nil)))))

(define cdr
(lambda (x) ;; lda_cdr_1
(lambda (is-nil) ;; lda_cdr_2
(lambda (ret) ;; lda_cdr_3
((x (lambda (xcar) ;; lda_cdr_4
(lambda (xcdr) ;; lda_cdr_5
(ret (xcdr unit)))))
is-nil)))))

(define zero nil)

(define increment
(lambda (x) ;; lda_increment_1
((cons (lambda (unit) zero)) ;; lda_increment_2
(lambda (unit) x)))) ;; lda_increment_3

(define decrement cdr)

(define add
(lambda (x) ;; lda_add_1
(lambda (y) ;; lda_add_2
(((decrement y)
(lambda (unit) x)) ;; lda_add_3
(lambda (y-dec) ;; lda_add_4
((add (increment x))
(y-dec unit)))))))

(define sub
(lambda (x) ;; lda_sub_1
(lambda (y) ;; lda_sub_2
(lambda (is-positive) ;; lda_sub_3
(lambda (is-zero) ;; lda_sub_4
(lambda (is-negative) ;; lda_sub_5
(((decrement x)
(lambda (unit) ;; lda_sub_6
(((decrement y) is-zero)
(lambda (y-dec) ;; lda_sub_7
(is-negative y)))))
(lambda (x-dec) ;; lda_sub_8
(((decrement y)
(lambda (unit) (is-positive x))) ;; lda_sub_9
(lambda (y-dec) ;; lda_sub_10
(((((sub (x-dec))
(y-dec))
is-positive)
is-zero)
is-negative)))))))))))

(define disp1
(lambda (lis) ;; lda_disp1_1
((lis (lambda (car) ;; lda_disp1_2
(lambda (cdr) ;; lda_disp1_3
(disp (car unit))
(disp2 (cdr unit)))))
do-nothing)))

(define disp2
(lambda (lis) ;; lda_disp2_1
((lis (lambda (car) ;; lda_disp2_2
(lambda (cdr) ;; lda_disp2_3
(display " ")
(disp (car unit))
(disp2 (cdr unit)))))
do-nothing)))

(define disp
(lambda (lis) ;; lda_disp_1
(display "(")
(disp1 lis)
(display ")")))

(define prin ;; lda_prin_1
(lambda (lis)
(disp lis)
(newline)))

(define len1
(lambda (lis) ;; lda_len1_1
(lambda (acc) ;; lda_len1_2
((lis (lambda (car) ;; lda_len1_3
(lambda (cdr) ;; lda_len1_4
((len1 (cdr unit))
(increment acc)))))
(lambda (unit) acc))))) ;; lda_len1_5

(define len
(lambda (lis) ;; lda_len_1
((len1 lis) zero)))

(define fmap
(lambda (f) ;; lda_len_1
(lambda (lis) ;; lda_len_2
((lis (lambda (car) ;; lda_len_3
(lambda (cdr) ;; lda_len_4
((cons (lambda (unit) ;; lda_len_5
(f (car unit))))
(lambda (unit) ;; lda_len_6
((fmap f) (cdr unit)))))))
(lambda (unit) nil))))) ;; lda_len_7

(define loeb
(lambda (lis) ;; lda_loeb_1
((fmap (lambda (f) ;; lda_loeb_2
(f (loeb lis))))
lis)))

(prin (loeb ((cons (lambda (unit) len)) ;; lda_main_1
(lambda (unit) ;; lda_main_2
((cons (lambda (unit) len)) ;; lda_main_3
(lambda (unit) ;; lda_main_4
((cons (lambda (unit) len)) ;; lda_main_5
(lambda (unit) nil)))))))) ;; lda_main_6

Name: Anonymous 2013-12-27 4:31

>>33
(define true (lambda (x y) x))

(define false (lambda (x y) y))


wat

Name: Anonymous 2013-12-27 5:01

>>39
You heard the man. It makes writing if trivial - given a predicate p and statements F,G, if p then F else G is simply pFG

For reference,
a and b := (a)(b)(false)
a or b := (a)(true)(b)
not p := p(false)(true)
a xor b=(a)((b)(false)(true))(b)

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