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

Fuck You, Write Code

Name: Anonymous 2013-05-29 1:07

/prog/, I throw down a gauntlet to you.

From http://esolangs.org/wiki/Category:Unimplemented , select a programming language. Write an implementation for it and post it. If you want to pick something else and implement it better than what's already there, go for it. No prizes or shit, because real EXPERT PROGRAMMERS do it for the thrill of the chase.

(No, I'm not L.A. Calculus, I'm not angry enough.)

I'll throw my hand in as well, so you can at least get the satisfaction of laughing at me.  I might do CAT or Knight Shuffling Tower.

Name: Anonymous 2013-06-02 15:26

I wrote a minimal functional extension for m4, so it is easy to port pure haskell code to m4:


changecom(`#',`')dnl
dnl#
dnl# `Id`
dnl#
define(Id, $1)dnl
dnl# `Lambda'
dnl#
define(Lambda, `define($1, `$2')')dnl
define(Curry1, `define($1,`$2'($3,$`'1,$`'2,$`'3,$`'4))')dnl
define(Curry2, `define($1,`$2'($3,$4,$`'1,$`'2,$`'3,$`'4))')dnl
define(Curry3, `define($1,`$2'($3,$4,$5,$`'1,$`'2,$`'3,$`'4))')dnl
define(Curry4, `define($1,`$2'($3,$4,$5,$6,$`'1,$`'2,$`'3,$`'4))')dnl
define(Curry5, `define($1,`$2'($3,$4,$5,$6,$7,$`'1,$`'2,$`'3,$`'4))')dnl
define(Curry6, `define($1,`$2'($3,$4,$5,$6,$7,$8,$`'1,$`'2,$`'3,$`'4))')dnl
dnl#
dnl# `Logical operators'
dnl#
define(True, 1)dnl
define(False,0)dnl
define(Not, `ifelse($1, True, False, `ifelse($1, False, True, `errprint(Not a bool)')')')dnl
define(isBool, `ifelse($1, True, True, `ifelse($1, False, True, False)')')dnl
define(And, `ifelse($1, True, `ifelse($2, True, True, False)', False)')dnl
define(Or, `ifelse($1, True, True, `ifelse($2, True, True, False)')')dnl
dnl#
dnl# `List operators'
dnl#
define(Empty, [])dnl
define(Prep, `ifelse($2,[],[$1;],[$1;`substr($2,1,decr(len($2)))')')dnl
define(App, `ifelse($2,[],[$1;],`substr($2, 0, decr(len($2)))'$1;])')dnl
define(Next, `substr($2,$1,1)')dnl
define(Getpos, `ifelse(Next($1,$2),$3,$1,`Getpos(incr($1), $2, $3)')')dnl
define(Head, `substr($1, 1, decr(Getpos(0, $1,;)))')dnl
define(Tail, `ifelse($1,[],`errprint(tail: empty list)',[`substr($1,incr(Getpos(0,$1,;)))')')dnl
define(Index, `ifelse($1,0,`Head($2)',`Index(decr($1), Tail($2))')')dnl
define(Null, `ifelse($1,[],True,False)')dnl
dnl# `Foldr continuation passing style'
dnl# Foldr :: (a -> b ->  b) -> b -> [a] -> b
define(_Step, `$4(`$1',$1(Head($3),$2),Tail($3))')dnl
define(Foldr, `ifelse(Null($3),True,$2,`_Step(`$1',$2,$3,`Foldr')')')dnl
dnl# `Foldl continuation passing style'
dnl# Foldl :: (b -> a -> b) -> b -> [a] -> b
define(_Stepl, `$4(`$1',$1($2,Head($3)),Tail($3))')dnl
define(Foldl, `ifelse(Null($3),True,$2,`_Stepl(`$1',$2,$3,`Foldl')')')dnl
dnl# `Sum example usage of Foldr'
define(Plus, `eval($1+$2)')dnl
define(Sum, `Foldr(`Plus',0,$1)')dnl
dnl# `Filter'
dnl# `Filter creates an locally scoped curried function, implemented withdefine..undefine'
dnl# ` ```$1''' is just a trick to get the function unpacked at the right place. Every passing
dnl# `removes a `' from the functionname'
dnl# Filter :: (a -> Bool) -> [a] -> [a]
define(_Stepf, `ifelse($1($2), True, Prep($2, $3), $3)')dnl
define(Filter,`Curry1(__Stepf,`_Stepf',```$1''')Foldr(`__Stepf',Empty,`$2')undefine(`__Stepf')')dnl
dnl# ZipWith zips two lists with a binary operation
dnl# zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
define(ZipWith,`ifelse(Null($1), True, [], `ifelse(Null($2), True, [], Prep($3(Head($1),Head($2)), ZipWith(Tail($1), Tail($2), `$3')))')')dnl


Some examples:

dnl#
dnl# Example ZipWith
dnl#
define(s, Prep(3, Prep(2, Empty)))dnl
define(t, Prep(3, Prep(3, Prep(4, Empty))))dnl
dnl#
Lambda(z,`eval($1-($2))')dnl
`ZipWith'(-) [3;2] [3;3;4] : ZipWith(s,t,`z')
dnl#
dnl# `Example foldr and foldl'
define(p, Prep(3,Prep(2,Prep(1, Empty))))dnl
Lambda(x,`eval($1-($2))')dnl
`foldr'(`x',0,[3;2;1]) is : Foldr(`x',0,p)
`foldl'(`x',0,[3;2;1]) is : Foldl(`x',0,p)
dnl#
dnl# Example filter
dnl#
Lambda(y, `eval($1 == 1 )')dnl
filter : Filter(`y', p)dnl

define(xs, Prep(3,Prep(4, Prep(5, Empty))))dnl
define(ys, Prep(9,Prep(8, Prep(7, Empty))))dnl

Add two lists together:

Lambda(fs, `eval($1 + $2)')dnl

ZipWith(xs, ys, `fs')


Monadic code would be difficult. Lisp code is portable too, unless you start to use call/cc or side effects, which will fuck everything up.

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