[racket] `def' ?

From: Jens Axel Søgaard (jensaxel at soegaard.net)
Date: Sat May 12 12:54:31 EDT 2012

I have amused myself with implementing this and other brevity constructs
useful for working with strings, vectors and other sequences.

In the repl one can now write:

(define-struct person (first-name last-name age height) #:transparent)
(define persons
    (vector (person "John" "Doe" 42 1.80)
                 (person "Jane" "Dae" 43 1.81)))
(define i 1)

(string (@ persons i .first-name 0) (@ persons i .last-name 0))

The file string-lang.rkt  implements a form {...} that allows one
one to reference the i'th element of lists, vectors, strings etc using
the syntax {v i}.

For substrings, sublists and subvectors and subsequences one can write {v i j}.

See the test file for examples.


There is one problem though.

The syntax
  {john .age}  should evaluate as (person-age john)
since john is an instance of a person structure.

Using information from structure-type-info I can construct the
identifier person-age, but I can not figure how to retrieve the
actual accessor without using eval.

This means that {john .age} only works in the repl and not
in a module context.

Any pointers for improving the macro dot (which implements this)
is appreciated.

-- 
Jens Axel Søgaard


2012/5/10 Matthias Felleisen <matthias at ccs.neu.edu>:
>
> Thanks for the nice illustration. It would be
>
> (define initials
>  (string (string-ref (id-firstname (vector-ref person i)) 0)
>          (string-ref (id-lastname (vector-ref person i)) 0)))
>
> but even with that we don't get the density of some path expression:
>
> (define initials (string (@ person i firstname) (@ person i lastname)))
>
> And now we're comparing with
>
> initials = person[i].firstname[0] + person[i].lastname[0]
> or
> initials = (@ person i firstname) + (@ person i lastname)
>
> Hmph.
>
>
>
> On May 10, 2012, at 5:18 PM, Justin Zamora wrote:
>
>> In my experience, the heaviness of Racket doesn't come from words like
>> "define", etc.  It comes in certain categories of programs that deal
>> extensively with strings, vectors, and structured data.  For example,
>>
>> initials = person[i].firstname[0] + person[i].lastname[0]
>>
>> This is very readable and useful.  It can be written quickly and is
>> read and understood easily The overloading of "+" (or your operator of
>> choice) and the implicit coercion of characters to strings is exactly
>> what is wanted here.  Even evaluating person[i] more than once doesn't
>> clutter up the expression.
>>
>> Compare this to the equivalent Racket:
>>
>> (define initials
>>  (string-append (string (string-ref (id-firstname (vector-ref person i)) 0))
>>                 (string (string-ref (id-lastname (vector-ref person i)) 0))))
>>
>> For this sort of thing, the Racket version is much harder to write,
>> read, and verify.  It would be nice to have something akin to
>> at-expressions that would allow such expressions to be written more
>> clearly.
>>
>> Justin
>>
>> On Thu, May 10, 2012 at 4:00 PM, Matthias Felleisen
>> <matthias at ccs.neu.edu> wrote:
>>>
>>> I will assert something about readability:
>>>
>>>  Racket programs look heavy when compared with Haskell programs.
>>>
>>> This is probably true for Python instead of Haskell, too. It is also true for ML. I conjecture that part of that heaviness comes from wide lines, long names, deep nesting. Who knows. I don't even know how to measure this kind of property.
>>>
>>> At this point, I can express certain ideas more easily in Racket than in Haskell, Python, ML or whatever, which is why I am fine. But if this advantage ever disappeared, heaviness would definitely be a factor to weigh.
>>>
>>> -- Matthias
>>>
>>>
>>>
>>>
>>>
>>>
>>> On May 10, 2012, at 3:49 PM, ozzloy-racket-users wrote:
>>>
>>>> i didn't assert that word length has nothing to do with readability, just that word frequency has more impact on reading time than word length.
>>>>
>>>> On Thu, May 10, 2012 at 3:39 PM, Luke Vilnis <lvilnis at gmail.com> wrote:
>>>> I can only speak for myself but I think it's a bit much to assert that word length has nothing to do with readability. Heck, maybe that's even true for you, but not for everyone. I have certainly felt it to be an issue. If the "define" keyword was 50 letters long it would definitely have an impact on my ability to read code - it seems to be an issue of degree, not existence.
>>>>
>>>> On Thu, May 10, 2012 at 3:26 PM, ozzloy-racket-users <ozzloy+users_racket-lang_org at gmail.com> wrote:
>>>> am i the only one that thinks not having abbreviated names for anything is good?
>>>> i like not having "def".  especially if it's going to be redundant.
>>>> i see this as a slippery slope i don't want to go down.
>>>> it annoys me when switching to other languages to have to ask: which way of shortening "function" does this language go with?  was it "fn"? maybe "fun"?
>>>> if the language has a strict policy of not using short versions of words, i don't have to guess.
>>>>
>>>> as for "def" being easier to read than "define", that's not true.  word frequency has more impact on reading time than word length for normal reading.  having more aliases makes both less frequent, so adding "def" could plausibly make reading both take longer.  most people read whole words at a time, rather than letter-by-letter.
>>>>
>>>>
>>>> On Thu, May 10, 2012 at 2:56 PM, Grant Rettke <grettke at acm.org> wrote:
>>>> There is always pretty mode in Emacs.
>>>>
>>>> On Thu, May 10, 2012 at 1:45 PM, Ray Racine <ray.racine at gmail.com> wrote:
>>>>> FYI for those who may not know.  Racket supports λ as an alias for lambda.
>>>>>  ctrl-\ in DrRacket.
>>>>>
>>>>>
>>>>> On Thu, May 10, 2012 at 1:59 PM, Nikita B. Zuev <nikitazu at gmail.com> wrote:
>>>>>>
>>>>>> +1 for `def' as alias for `define'.
>>>>>> May I also suggest `fun' for `lambda' alias?
>>>>>> Three letter names are the best =)
>>>>>>
>>>>>> (well one can always do it with require rename-in)
>>>>>>
>>>>>> --
>>>>>> Regards,
>>>>>> Nikita B. Zuev
>>>>>> ____________________
>>>>>>  Racket Users list:
>>>>>>  http://lists.racket-lang.org/users
>>>>>
>>>>>
>>>>>
>>>>> ____________________
>>>>>  Racket Users list:
>>>>>  http://lists.racket-lang.org/users
>>>>>
>>>>
>>>>
>>>>
>>>> --
>>>> http://www.wisdomandwonder.com/
>>>> ACM, AMA, COG, IEEE
>>>>
>>>> ____________________
>>>>  Racket Users list:
>>>>  http://lists.racket-lang.org/users
>>>>
>>>>
>>>> ____________________
>>>>  Racket Users list:
>>>>  http://lists.racket-lang.org/users
>>>>
>>>>
>>>>
>>>> ____________________
>>>>  Racket Users list:
>>>>  http://lists.racket-lang.org/users
>>>
>>>
>>> ____________________
>>>  Racket Users list:
>>>  http://lists.racket-lang.org/users
>
>
> ____________________
>  Racket Users list:
>  http://lists.racket-lang.org/users



-- 
--
Jens Axel Søgaard
-------------- next part --------------
A non-text attachment was scrubbed...
Name: string-lang.rkt
Type: application/octet-stream
Size: 3959 bytes
Desc: not available
URL: <http://lists.racket-lang.org/users/archive/attachments/20120512/68878dc2/attachment.obj>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: test-string-lang.rkt
Type: application/octet-stream
Size: 3686 bytes
Desc: not available
URL: <http://lists.racket-lang.org/users/archive/attachments/20120512/68878dc2/attachment-0001.obj>

Posted on the users mailing list.