Skip to content

Instantly share code, notes, and snippets.

@mwgkgk
Last active October 6, 2020 04:12
Show Gist options
  • Save mwgkgk/82f223b6de0a18a58b8ff691a53663b3 to your computer and use it in GitHub Desktop.
Save mwgkgk/82f223b6de0a18a58b8ff691a53663b3 to your computer and use it in GitHub Desktop.
LFE gotchas for Common Lispers, WIP

• 'true, 'false, '() instead of T, NIL.

Read more:

• There are no #+nil, #_ type reader macros.

To ignore the next form, we could define a comment macro like Clojure:

(defmacro comment args '(progn))

Returns a progn because progns are allowed at top-level.

Read more:

• Not every form is allowed at top level.

In top-level progns, only valid top-level forms are allowed.

• Instead of in-package, we slurp and unslurp.

See repl help with (help):

LFE shell built-in functions

(c file)       -- compile and load code in <file>
(cd dir)       -- change working directory to <dir>
(clear)        -- clear the REPL output
(doc mod)      -- documentation of a module
(doc mod:mac)  -- documentation of a macro
(doc m:f/a)    -- documentation of a function
(ec file)      -- compile and load code in erlang <file>
(ep expr)      -- print a term in erlang form
(epp expr)     -- pretty print a term in erlang form
(exit)         -- quit - an alias for (q)
(flush)        -- flush any messages sent to the shell
(h)            -- an alias for (help)
(help)         -- help info
(i)            -- information about the system
(i pids)       -- information about a list of pids
(l module)     -- load or reload <module>
(ls)           -- list files in the current directory
(ls dir)       -- list files in directory <dir>
(m)            -- which modules are loaded
(m mod)        -- information about module <mod>
(p expr)       -- print a term
(pp expr)      -- pretty print a term
(pid x y z)    -- convert <x>, <y> and <z> to a pid
(pwd)          -- print working directory
(q)            -- quit - shorthand for init:stop/0
(regs)         -- information about registered processes

LFE shell built-in commands

(reset-environment)             -- reset the environment to its initial state
(run file)                      -- execute all the shell commands in a <file>
(set pattern expr)
(set pattern (when guard) expr) -- evaluate <expr> and match the result with
                                   pattern binding
(slurp file)                    -- slurp in a LFE source <file> and makes
                                   everything available in the shell
(unslurp)                       -- revert back to the state before the last
                                   slurp

LFE shell built-in variables

+/++/+++      -- the tree previous expressions
*/**/***      -- the values of the previous expressions
-             -- the current expression output
$ENV          -- the current LFE environment

• LFE property-list is a list of tuples, CL plist is flat.

Common Lisp:

(defparameter my-plist (list 'foo "foo" 'bar "bar"))

LFE:

(set options '(#(debug true) #(default 42)))

Read more:

• MAP is a hashmap constructor.

lfe> (map "key" 42)
#M("key" 42)

Read more:

For the list processing, see lists:map/2 and her great many friends:

• Format

There are four ways to go about string formatting.

  1. lfe_io:format/2 and io:format/2 (the Erlang counterpart) print to stdout:
lfe> (lfe_io:format "hello world~n" ())
hello world
ok
lfe> (lfe_io:format "this outputs one LFE term: ~w~n" '(hello))
this outputs one LFE term: hello
ok
  1. lfe_io:format/3 and io:format/3 print to IoDevice:
  1. io_lib:format/2 prints to string:
  1. lfe_io functions that return strings end in 1: print1, prettyprint1, format1 and fwrite1
lfe> (lfe_io:print1 "hello")
(40 ("104" 32 "101" 32 "108" 32 "108" 32 "111") 41)
lfe> (lfe_io:prettyprint1 "hello")
(34 "hello\"")
lfe> (lfe_io:format1 "hello ~n" '())
(104 101 108 108 111 32 "\n")

• Erlang iolists

lfe> (lfe_io:format1 "hello ~n" '())
(104 101 108 108 111 32 "\n")
lfe> (lists:flatten (lfe_io:format1 "hello ~n" '()))
"hello \n"

Read more:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment