Questions about homoiconicity: Scheme/Lisp to Julia

251 views
Skip to first unread message

Piotr X

unread,
Dec 20, 2013, 1:08:49 PM12/20/13
to julia...@googlegroups.com
Hi,

I am asking this question out of curiosity in regard to the homoiconic language features of Julia:

How would this scheme implementatation of cons and car look like in Julia?

(define (cons x y)
  (define (dispatch m)
    (cond ((= m 0) x)
          ((= m 1) y)
          (else (error "Argument not 0 or 1 -- CONS" m))))
  dispatch)

(define (car z) (z 0))

Cheers!

Steven G. Johnson

unread,
Dec 20, 2013, 2:21:29 PM12/20/13
to julia...@googlegroups.com
It doesn't seem like your question has anything to do with homoiconicity---your example is about first-class & higher-order functions.

An equivalent definition in Julia would be

cons(x, y) = m -> m == 0 ? x : m == 1 ? y : throw(BoundsError())
car(z) = z(0)
cdr(z) = z(1)

--SGJ

Piotr X

unread,
Dec 21, 2013, 2:57:24 PM12/21/13
to julia...@googlegroups.com
Hello Steven,


On Friday, December 20, 2013 8:21:29 PM UTC+1, Steven G. Johnson wrote:
It doesn't seem like your question has anything to do with homoiconicity---your example is about first-class & higher-order functions.

Mea culpa. I mixed things up - but fortunatly I got the right answer: I was not wondering about macros et al, but indeed about higher order functions. Thanks for your answer!
 

An equivalent definition in Julia would be

cons(x, y) = m -> m == 0 ? x : m == 1 ? y : throw(BoundsError())
car(z) = z(0)
cdr(z) = z(1)



I understand that "->" defines an anonymous function here, and ":" seperates the definition statements in that function.

Cheers!

  Piotr
 

Stefan Karpinski

unread,
Dec 21, 2013, 3:09:17 PM12/21/13
to Julia Users
On Sat, Dec 21, 2013 at 2:57 PM, Piotr X <beorol...@googlemail.com> wrote:
I understand that "->" defines an anonymous function here, and ":" seperates the definition statements in that function.

No, the : is part of the ternary operator syntax: https://en.wikipedia.org/wiki/%3F:

Piotr X

unread,
Dec 21, 2013, 5:57:47 PM12/21/13
to julia...@googlegroups.com

No, the : is part of the ternary operator syntax: https://en.wikipedia.org/wiki/%3F:

Thank you for clearing that up.

(http://docs.julialang.org/en/latest/manual/control-flow/)

- piotr
Reply all
Reply to author
Forward
0 new messages