Shop OBEX P1 Docs P2 Docs Learn Events
Pure functional programming on the Propeller — Parallax Forums

Pure functional programming on the Propeller

ersmithersmith Posts: 6,053
edited 2015-03-02 12:10 in Propeller 1
Tired of languages with complicated syntax? Want to experience the joys of purely mathematical computing? Lazy K is a language which is utterly simple. It eschews complications like variables, loops, side effects, and even integers. All it has are functions, and only 3 of them have names -- the others are all unnamed. You can program with just parentheses and 3 letters. And you don't even really need the close parenthesis.

Despite all of this Lazy K is in fact Turing complete, so any computable function can be expressed in it. (Google "SKI combinator calculus" for further information.)

Lazy K was originally developed for the PC. The Esoteric Languages wiki entry for it is here: http://esolangs.org/wiki/Lazy_K

Now there's a Propeller version. I've attached binaries for PC and Raspberry Pi (both Propeller compilers and a native interpreter). The sources are checked in to:

https://github.com/totalspectrum/proplazyk

Here's "hello world" in Lazy K (using the alternate Unlambda syntax that replaces parentheses with a single backquote):
`k``s``si`k``s`k```sii``s``s`kski``s``s`ksk``s``s`ksk```s``siii``s``s`kski`k``s``si`k``s``s`ksk```s``s`kski``s`k``s``s`kski
``s``s`ksk```sii``s``s`kski`k``s``si`k``s`k```sii``s``s`kski```sii``s``s`ksk``s``s`kski`k``s``si`k``s`k```sii``s``s`kski```sii`
`s``s`ksk``s``s`kski`k``s``si`k``s``s`ksk``s`k``s``s`kski``s``s`ksk``s`k``s``s`kski```sii``s``s`ksk``s``s`kski`k``s``si`k
````s``s`ksk```s``siii``s``s`kski`s``s`ksk```sii``s``s`ksk``s``s`kski`k``s``si`k``s`k``s``s`kski```s``siii``s``s`kski`k``s``si
`k``s`k``s``s`ksk``s`k``s``s`kski``s``s`ksk``s``s`kski``s``s`ksk```s``siii``s``s`kski`k``s``si`k``s``s`ksk``s`k``s``s`kski
``s``s`ksk``s`k``s``s`kski```sii``s``s`ksk``s``s`kski`k``s``si`k``s`k``s``s`kski``s``s`ksk``s`k``s``s`kski``s``s`ksk```sii`
`s``s`ksk``s``s`kski`k``s``si`k``s`k```sii``s``s`kski```sii``s``s`ksk``s``s`kski`k``s``si`k```s``s`kski``s`k``s``s`kski``s``s
`ksk```sii``s``s`kski`k``s``si`k``s``s`ksk``s`k``s``s`kski```s``siii``s``s`kski`k``s``si`k``s`k``s``s`kski``s``s`ksk```sii``s
``s`kski`k``s``si`k```sii```sii``s``s`kski`k```sii```sii``s``s`kski

I'll have to grant that programming in Lazy K is not always intuitive. Fortunately, the Lazy K distribution comes with a translator called "lazier" from a subset of Scheme to Lazy K. Here's "hello world" in lazier:
(load "../lazier.scm")
(load "../prelude.scm")
(load "../prelude-numbers.scm")

(lazy-def '(main input)
 '(
   (cons 72 (cons 101 (cons 108 (cons 108 (cons 111 (cons 44
            (cons 32
            (cons 119 
	    (cons 111 (cons 114 (cons 108 (cons 100 (cons 33
     (cons 10
     (cons 256 256)))))))))))))))
  )
)

Which, if you know ASCII, is a lot clearer.

On a serious note, the runtime environment for Lazy K might usefully serve as an interpreter for other functional languages like Lisp and such (for that matter it's possible to compile Lisp to Lazy K; the lazier package is a good start in that direction). Right now the runtime is about 6K of code or so, but with optimization it might be possible to fit it into COG memory. The distribution's Readme.md also has some other ideas of things to do with Lazy K.

What's in the distribution?

lazyk.exe is a Lazy K interpreter for the PC.

proplazyk.exe is a compiler that takes Lazy K code and produces a binary for loading into a Propeller.

There are also binaries for the Raspberry Pi, but I haven't tested those (if they don't work, you'll have to build them yourself from the sources).

hello.lazy is the hello world program
fib.lazy prints Fibonacci numbers as rows of asterisks
ab.lazy prints the string AB endlessly
rot13.lazy applies the rot13 encoding to its input

The lazier folder contains the Scheme to Lazy K compiler and a folder of examples copied from Ben Rudiak-Gould's original Lazy K program. Unfortunately many of the examples will not fit in the Propeller's memory.

Comments

  • RsadeikaRsadeika Posts: 3,837
    edited 2015-02-25 08:31
    Thanks for the info, but I think I would rather take on PASM. :-)

    Ray
  • Heater.Heater. Posts: 21,230
    edited 2015-02-25 08:41
    Excellent. A language that makes Forth look readable.

    That Turing guy has a lot to answer for don't you think?
  • JonnyMacJonnyMac Posts: 9,105
    edited 2015-02-25 10:05
    Excellent. I language that makes Forth look readable.

    :)

    Simple is in the eye of the beholder, isn't it?
  • Heater.Heater. Posts: 21,230
    edited 2015-02-25 11:42
    JonnyMac,
    Simple is in the eye of the beholder, isn't it?
    Probably.

    I'm really simple so I can't tell.

    But, I thought the idea of a high level language, compiled or interpreted, was to make using the machine easier than writing assembler. Or writing HEX or binary directly.

    In the case of the likes of Forth, Lisp, Lazy K, BrainF*** and others this seems to not be true.
  • ersmithersmith Posts: 6,053
    edited 2015-02-25 19:13
    Heater. wrote: »
    But, I thought the idea of a high level language, compiled or interpreted, was to make using the machine easier than writing assembler. Or writing HEX or binary directly.

    In the case of the likes of Forth, Lisp, Lazy K, BrainF*** and others this seems to not be true.

    To be fair, Lazy K and BrainF*** are both deliberately obfuscated (although I do think Lazy K might make a useful assembly language of sorts for functional languages). Lisp is actually quite readable once you get into it. The same may be true of Forth -- certainly it has its dedicated adherents -- but I'm afraid I've never got the knack for it, so for me it does fall into the obfuscated languages category.

    (I did some APL programming many years ago... there's a language that's intended seriously, and quite useful, but is pretty close to write only :) ).
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2015-02-25 22:47
    ersmith wrote:
    (I did some APL programming many years ago... there's a language that's intended seriously, and quite useful, but is pretty close to write only ).
    ... unless your first natural language is Arabic or Hebrew, in which case, reading right-to-left comes naturally. :)

    -Phil
  • David BetzDavid Betz Posts: 14,516
    edited 2015-03-02 12:10
    Hi Eric,

    I saw this when you first posted it but didn't have time to reply. It looks really cool! I'm tempted to steal your GC and use it to create a tiny Lisp. I've been looking at Peter Henderson's "Functional Programming: Application and Implementation" which is where I started when I wrote XScheme. It describes a really simple dialect of Lisp that is self-compiling. A PASM implementation of that VM along with a GC would probably run really well on the Propeller. If only I had time to build it. :-)
Sign In or Register to comment.