Can this FizzBuzz example be made more of an exemplar of LFE?

35 views
Skip to first unread message

Steven Proctor

unread,
Aug 15, 2015, 11:48:51 PM8/15/15
to Lisp Flavoured Erlang
I am going to be giving a little lightning talk at work about Erlang, as part of a "Hello World!" languages presentation, and one of the requirements of the talk is to show FizzBuzz in the language we are going to be talking about.

Some of the people at work thought Elixir would be interesting to show, so I thought I would try to make the presentation more about the Erlang VM than Erlang specifically, and with that I wanted to include FizzBuzz done in LFE.

Here is the solution I came up with, and I wanted to check with the list if this is the way you all would write it.

This feels very Erlang-ish centric, with just using parens, so any feedback on style, or using some built in LFE functions/macros instead of just calling the standard Erlang functions, or anything else that would help make it more illustrative of LFE.  


(defmodule fizzbuzz
 (export (fizzbuzz 1)))

(defun fizzbuzz (n)
  (lists:foreach
    (lambda (x) (io:format "~p~n" (list x)))
    (lists:map
      (lambda (x) (translate_number x))
      (lists:seq 1 n))))

(defun translate_number
  ((n) (when (and (== 0 (rem n 3))
                  (== 0 (rem n 5))))
    "FizzBuzz")
  ((n) (when (== 0 (rem n 3)))
    "Fizz")
  ((n) (when (== 0 (rem n 5)))
    "Buzz")
  ((n) n))


Thank you all for your feedback!!!

And Duncan, before you ask, yes, I can write this up as a blog post for blog.lfe.io, and I will even include my learnings from any feedback given about how to make this more LFE. ;)

--Proctor

Eric

unread,
Aug 16, 2015, 1:45:48 AM8/16/15
to lisp-flavo...@googlegroups.com
Have you seen examples/fizzbuzz.lfe in the LFE repo?

In this gist I’ve stripped out the commentary, modernized the syntax, capitalized on the fact that in LFE (and Erlang), functions with the same name but different arities are considered distinct, and made sure it matches your code logic-wise. It also uses quasiquoting for the format form, i.e. `(,x) instead of (list x).

One style point about your code is that translate_number should be translate-number, since dashes are preferred to underscores.


Cheers,

Eric


--
You received this message because you are subscribed to the Google Groups "Lisp Flavoured Erlang" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lisp-flavoured-e...@googlegroups.com.
To post to this group, send email to lisp-flavo...@googlegroups.com.
Visit this group at http://groups.google.com/group/lisp-flavoured-erlang.
For more options, visit https://groups.google.com/d/optout.

Robert Virding

unread,
Aug 16, 2015, 9:46:40 PM8/16/15
to lisp-flavo...@googlegroups.com
Three quick points:

- There is an lfe_io module for writing things out with LFE syntax
- If you just want the words Fizz and Buzz without quotes have then as atom (haven't read the actual problem)
- Strings are self quoting so there is no need to quote them.

Robert

Eric

unread,
Aug 16, 2015, 10:49:59 PM8/16/15
to lisp-flavo...@googlegroups.com
On Aug 16, 2015, at 8:46 PM, Robert Virding <rvir...@gmail.com> wrote:

Three quick points:

- There is an lfe_io module for writing things out with LFE syntax
- If you just want the words Fizz and Buzz without quotes have then as atom (haven't read the actual problem)
- Strings are self quoting so there is no need to quote them.

I didn’t know that, cool!

> (=:= "a string" '"a string")
true

I’ve updated the gist too. It now uses atoms and lfe_io.

Duncan McGreggor

unread,
Aug 17, 2015, 11:26:11 AM8/17/15
to Lisp Flavoured Erlang
Woo-hoo!

Can't wait to publish it :-D

d

Steven Proctor

unread,
Aug 18, 2015, 12:27:25 AM8/18/15
to Lisp Flavoured Erlang
Robert, Eric,

Thanks for your feedback and info.

Here is an updated solution that takes into account the feedback.  In addition to that I also introduced a let binding form to show off that aspect of the Lisp in LFE.

(defmodule fizzbuzz
 (export (fizzbuzz 1)))

(defun fizzbuzz (n)
  (lists:foreach
    #'write-line/1
    (translate-upto n)))

(defun translate-upto (n)
  (let ((numbers (lists:seq 1 n)))
    (lists:map
      #'translate/1
      numbers)))

(defun write-line
  ((string) (lfe_io:format "~p~n" `(,string))))

(defun translate
  ((n) (translate n (rem n 3) (rem n 5))))

(defun translate
  ([_ 0 0] 'FizzBuzz)
  ([_ 0 _] 'Fizz)
  ([_ _ 0] 'Buzz)
  ([n _ _] n))

I will be presenting it on Wednesday, so I will be putting the final touches on the presentation tomorrow night, so if you see something else that should be updated, I am all ears.

--Proctor

Robert


To unsubscribe from this group and stop receiving emails from it, send an email to lisp-flavoured-erlang+unsub...@googlegroups.com.

To post to this group, send email to lisp-flavo...@googlegroups.com.
Visit this group at http://groups.google.com/group/lisp-flavoured-erlang.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Lisp Flavoured Erlang" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lisp-flavoured-erlang+unsub...@googlegroups.com.

To post to this group, send email to lisp-flavo...@googlegroups.com.
Visit this group at http://groups.google.com/group/lisp-flavoured-erlang.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Lisp Flavoured Erlang" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lisp-flavoured-erlang+unsub...@googlegroups.com.

Duncan McGreggor

unread,
Aug 18, 2015, 1:10:14 AM8/18/15
to Lisp Flavoured Erlang
Quick question below:

On Mon, Aug 17, 2015 at 11:27 PM, Steven Proctor <steven....@gmail.com> wrote:
Robert, Eric,

Thanks for your feedback and info.

Here is an updated solution that takes into account the feedback.  In addition to that I also introduced a let binding form to show off that aspect of the Lisp in LFE.

(defmodule fizzbuzz
 (export (fizzbuzz 1)))

(defun fizzbuzz (n)
  (lists:foreach
    #'write-line/1
    (translate-upto n)))

(defun translate-upto (n)
  (let ((numbers (lists:seq 1 n)))
    (lists:map
      #'translate/1
      numbers)))

(defun write-line
  ((string) (lfe_io:format "~p~n" `(,string))))

(defun translate
  ((n) (translate n (rem n 3) (rem n 5))))

For both of those last two functions, they each do no matching... is there are reason you didn't write them like this?

(defun write-line (string)
  (lfe_io:format "~p~n" `(,string)))

(defun translate (n)
  (translate n (rem n 3) (rem n 5)))
 
Did they used to be part of a multi-clause function whose parts were later removed?

Just curious,

d


Robert


To unsubscribe from this group and stop receiving emails from it, send an email to lisp-flavoured-e...@googlegroups.com.
To post to this group, send email to lisp-flavo...@googlegroups.com.
Visit this group at http://groups.google.com/group/lisp-flavoured-erlang.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Lisp Flavoured Erlang" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lisp-flavoured-e...@googlegroups.com.
To post to this group, send email to lisp-flavo...@googlegroups.com.
Visit this group at http://groups.google.com/group/lisp-flavoured-erlang.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Lisp Flavoured Erlang" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lisp-flavoured-e...@googlegroups.com.
To post to this group, send email to lisp-flavo...@googlegroups.com.
Visit this group at http://groups.google.com/group/lisp-flavoured-erlang.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Lisp Flavoured Erlang" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lisp-flavoured-e...@googlegroups.com.

Eric Bailey

unread,
Aug 18, 2015, 1:56:03 AM8/18/15
to lisp-flavo...@googlegroups.com
I like your showcasing of let and the #'f/a syntax. Also +1 to Duncan's edits.

I'm excited for the blog post and to hear how LFE is received at your work.

Sent from my iPhone

Steven Proctor

unread,
Aug 18, 2015, 9:03:46 AM8/18/15
to lisp-flavo...@googlegroups.com
Thanks for catching that!

Was just lack of experience in actually writing LFE for any length of time, which I am slowly trying to address. ;)

--Proctor
You received this message because you are subscribed to a topic in the Google Groups "Lisp Flavoured Erlang" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/lisp-flavoured-erlang/3ZRsjsLmSec/unsubscribe.
To unsubscribe from this group and all its topics, send an email to lisp-flavoured-e...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages