Good Morning Racketnam!
Let’s Racket
So in TR to ensure that a function takes to arrays with the same size/shape do I use a contract or is there a way to do it without
array as in math/array ?
yes :thumbsup:
Just checked the matrix implementation. Didn’t learn anything :slightly_smiling_face:
(define-type (Matrix A) (Array A))
from what I understood a matrix is just a 2d array right? and since I do have to deal with some arrays bigger then 2d I thought I better stick to math/array
although in this particular case it would be a 2d array / matrix
but I need them to be the same size
Yes. I just wanted to check whether the Matrix type contained information on the shape. It didn’t.
So a contract sounds a nice approach.
ok :slightly_smiling_face: I’ll give that a try
thanks!
by the way if I want an argument to be a 1/0 Array is (Array (U Zero One))
the best approach? Or is there a number type that’s more adequate?
Don’t know.
by the way in terms of testing TR is it good practice to have test cases for the output types or is that too redundant?
hey once again
can someone help me figure out how to use unfold from srfi/1
I want to unfold a function x times (not sure if I am describing this right)
So you want to generate the list (list (f 0) (f 1) ... (f (- x 1)))
?
#lang racket
(require (only-in srfi/1 unfold))
(define (f x) (* x x))
(unfold (λ (x) (= x 10)) ; last seed value?
f ; seed to value
add1 ; next seed value
0) ; first seed value
hum not quite I think
more like (list (f 0) (f (f 0)) (f (f (f 0))))
etc
till f has been nested f times
ah yes that looks right
now that I ran it
thanks :slightly_smiling_face:
or hang on
no still think not quite what I wanted
some pointed to unfold in another chat
for what I described
but never used it
ok a concrete example
If 0 is the seed, and f is the next seed function then unfold computes that list.
The only tricky thing is for unfold to know when to stop.
I want someting like (f 3 sqr 2)
which gives would give (list (sqr 2) (sqr (sqr 2)) (sqr (sqr (sqr 2))))
where 3 is the depth (?) sqr the function to apply and 2 the initial value
if you get my thought process
it’s basically to run a simulation
So you have to keep track of two values: the number of times f has been applied to it self and the list of previous values.
hum ok, it should be possible to do that with fold right?
unfold
(require (only-in srfi/1 unfold))
(define (f x) (list 'f x))
(struct seed (i x))
(unfold (match-lambda [(seed i x) (= i 10)]) ; stop?
seed-x ; seed to value
(match-lambda [(seed i x) (seed (+ i 1) (f x))]) ; next
(seed 0 0)) ; initial seed
hum ok!
Your example becomes:
(require (only-in srfi/1 unfold))
(define (f x) (sqr x))
(struct seed (i x))
(unfold (match-lambda [(seed i x) (= i 5)]) ; stop?
seed-x ; seed to value
(match-lambda [(seed i x) (seed (+ i 1) (f x))]) ; next
(seed 0 2)) ; initial seed
guess the last part can be wrapped into a function right?
right?
sure
This looks great thanks :slightly_smiling_face:
the seed struct is just syntatic sugar?
Compare a loop:
(let loop ([i 0] [x 2] [xs '()])
(if (= i 5)
(reverse xs)
(loop (+ i 1) (f x) (cons x xs))))
ah ok
looks a bit simpler hehe
and it’s still in functional style no?
I was about to post that :stuck_out_tongue:
yes, still functional
any suggestions for a name hehe, simulate sounds to specific
to wrap it up in a function
loop-n-times ?
or not quite
cool
would that be useful to add in one of the racket libraries?
I wouldn’t be surprised if it’s already there :slightly_smiling_face:
yeah I was wondering that
I looked up nest-list, that syntax doesn’t show up, but maybe by another name ?
I did feel it most be a standard kind function
But it’s useful alright.
I mean it feels a bit like a map, filter, reduce kind of function no?
Yes. Always fun to find new list functions.
hehe whenever I hear of Wolfram Language the languages as weapons cartoon always comes to mind
hum wonder if this is the original link https://www.technotification.com/2017/10/if-programming-languages-were-weapons.html
- Mathematica is a low earth orbit projectile cannon, it could probably do amazing things if only anyone could actually afford one.
Looks alright yes?
should be easy to add types no..?
for TR
Yes. Only one nit: rename expr
to something like seed
. Mathematica is not call by value, so it makes sense for them to use expr
.
hum ok!
but keeping a similar order of args makes sense no?
yes
onething about TR
is the standard to have the separate (: etc)
or to have the types in the function itself?
(define (f [i : Integer] (body))
I don’t know what’s standard. I like to use (: ... )
before the function.
I mean since I tried a little Julia and Haskell my first instinct was to place it in the function itself, but I did notice sometimes it’s waymore compact to place it in (: ...)
anyways for the help @soegaard2 :slightly_smiling_face:
off to lunch
Ok so I think I’ve got the general jist of the typing for the nest-list function, but I have this problem . Type Checker: cannot apply a function with unknown arity;
function
f’ has type Procedure which cannot be applied in: (f x)` which I kind of understand, but not sure what to do about it
plus I think the Any
is probably not great idea
do I have to do some kind of polymorphic function..?
instead
or not really
Replace Procedure
with the type of an Any to Any function.
so (-> Any Any) ..?
or..?
I’ll admit I am a bit out of my depth :stuck_out_tongue:
(but giving it a try anyways)
guess not hehe
> (nest-list sqr 2 3)
. Type Checker: type mismatch
expected: (-> Any Any)
given: (case->
(-> Zero Zero)
(-> One One)
(-> Positive-Byte Positive-Index)
(-> Byte Index)
(-> Positive-Integer Positive-Integer)
(-> Integer Nonnegative-Integer)
(-> Positive-Exact-Rational Positive-Exact-Rational)
(-> Exact-Rational Nonnegative-Exact-Rational)
(-> Flonum Nonnegative-Flonum)
(-> Single-Flonum Nonnegative-Single-Flonum)
(-> Inexact-Real Nonnegative-Inexact-Real)
(-> Real Nonnegative-Real)
(-> Float-Complex Float-Complex)
(-> Single-Flonum-Complex Single-Flonum-Complex)
(-> Inexact-Complex Inexact-Complex)
(-> Exact-Number Exact-Number)
(-> Number Number)) in: sqr
ah ok it’s because sqr is polymorphic?
hum how do I specify the type for accepting a polymorphic function ..?
Well, we know that the input and output of f needs to be the same type as seed.
oh hum but actually what I am thinking of might not have the same type
in fact I was wondering why 2 was in the list
ah no yeah your right
but I’m wondering if that does what I was wanting
probably
Think I got it
does that make sense?
It does. I am wondering how we can get the output type to be (Listof A)
?
Note: You don’t need to give types to i and x. They can be inferred.
ok yeah I felt it was overkill
[xs : (Listof A) '()]
works
Like this yeah? (was giving it a try)
yes
> (nest-list sqr 2 5)
- : (Listof Positive-Integer)
'(2 4 16 256 65536)
cool
Also you can remove : (Listof A)
from (let loop : (Listof A)
nice :slightly_smiling_face:
by the way I came across let: is that of any use in this case?
didn’t quite understand it fully, but I had the impression I could use it
but it’s probably not need here yeah?
I think let:
is from the first version of typed racket.
No reason not to use let.
hum ok!
Thanks for the help again :slightly_smiling_face:
Seems to work alright
I still haven’t quite understood how to use rackunit with typed racket
I mean I understand I have to annotate rackunit from the messages
with require/typed
ah… I just need to change rackunit to typed/rackunit ?? Hehe ok?
@kyle.nweeia has joined the channel
Based on what I learnt today changed some of the stuff I was working on yesterday, does this make sense..? It seems to work fine.
Maybe this is better?
@maxim_jaffe in general I recommend in-range
instead of range
(probably the docs should suggest this)
hum ok! sorry python habit
is that because range outputs and actual list while in-range outputs comething that can be iterated over?
yes — you can iterate over a list too, but there’s no reason to create the intermediate list
you can even just write n
n?
ah!
(for/list [i n] ...)
yes?
hum can’t seem to do it without a (in-range n)
it works for me … (define (prior-samples n priors) (for/list ([i n]) (prior-sample priors)))
ah!
I was missing the []
thanks samth :slightly_smiling_face:
Going to start reading the reference manual tonight. That should get me into racket, as it looks interesting enough.
by the way is there a way to get DrRacket to eat less memory
sometimes it comes all the way up to 1.5 GB without doing run. I understood it continuoslty check your code and reports errors, is that the reason for the memory use?
it’s nice to have the error feedback, but I would be okay just rerunning and get some more memory in exchange
I already have one memory junky called firefox hehe
Cool I didn’t know FrTime was part of the Racket ecosystem
I had read about it in the programming paradigms for dummies chapter of Van Roy
is it possibility to combine something written in FrTime to some TR and regular Racket?
(I saw it was there in the tools options)
@mflatt Given that the cache files produced by expander/run
appear to be the results of calling compile
, what distinguishes them from “real” bytecode files?
@maxim_jaffe You need to check the docs of FrTime.
hehe ok, yeah sorry I should do that :stuck_out_tongue:!
the only thing is they are just a list of functions
there is also a paper somewhere
and some demo programs
yes think I’ll try the demos! I saw there is a thesis?
this one right?
looks interesting
and he gets into some code straight away (wasn’t expecting that)
not sure I got the time to read it for now :stuck_out_tongue:
but really cool
ah is this the paper? https://cs.brown.edu/~sk/Publications/Papers/Published/ck-frtime/paper.pdf
(it’s in postscript, so hope you aren’t on Windows)
Nono, I am in good all Linux (Manjaro Linux)
sorry Firefox, DrRacket and Zotero decided I was having too much fun
and ground my computer to a halt hehe
I still don’t get how much RAM these things consume
I mean I think 128 mb was a party back when I was a kid (1990s) and I remember having browser word and some other things open
on Mac OS 7 or what was it
I got 4gb RAM what the hell :stuck_out_tongue:?
sure it’s not humongous but still
there is actually the pdf version too
@soegaard2 thanks for the report :slightly_smiling_face: that’s really helpful
maybe a link to this particular report could be added in the docs of FrTime?
and to the thesis also
@leo has joined the channel
any idea what I am getting wrong (just manually copied it over from the report)? . match: syntax error in pattern in: ($ date sec min hr day mon yr _ _ _ _)
from the frtime report above
Try (date sec …) instead.
without the $ ? still gives me the same error..!
I think frtime is to old, that the report uses the old match implementation, which had a slightly different syntax.
hum ok! I’ll look it up
Alternatively add (require mzlib/match)
hum just copied the list embedded in the (seconds->date seconds)
but still get a syntax error in pattern
the length matches so not sure what’s going own
think I’ll just look at the demos :stuck_out_tongue:
they should be working no?
hum ok just needed to replace the $ with list
and I forgot the extra info
(seconds->date seconds)
going to try with the original example
returns a (signal (date* …) …)
so probably have to convert it into something else
So something is not quite right.
I mean there is this in the middle (date* 19 9 20 27 7 2018 5 207 #t 3600 0 "WEST")
which is what is supposed to ge t matched in the original script
(value-now (seconds->date seconds))
gives the date
cool
but that doesn’t work
the idea is that clock is the current date
if we use value-now, the value of clock doesn’t update automatically
I think you need to try frtime in an older version of racket.
hum ok!
I am almost sure something has changed.
I saw something about streams in the docs somewhere
oh well!
interesting idea
maybe can look at it some other time
but looks like it’s not what I am thinking
@mflatt is the fact that make-base-namespace
doesn’t work if you use it from a module that doesn’t load racket/base
a bug, or just something that can’t work?
It can’t work
@mflatt can I ask you to take a look at this PR at some point? https://github.com/racket/draw/pull/12
PPA for 7.0 is building… https://launchpad.net/~plt/+archive/ubuntu/racket/+build/15188809
Does Racket have something similar to Clojure’s (source) capability?
@hunter.t.joz what does that do?
Shows you the code for a particular function and/or macro; for example, (source dotimes) would show you the code for dotimes.
No, racket doesn’t keep the source code for things around
@hunter.t.joz fwiw, while @samth’s answer is correct, both DrRacket and racket-mode have functionality to jump to the definition of a particular function
which is probably enough if you just want to find out how something is implemented
also, ,describe some-id
in XREPL (which I believe is loaded by default in modern versions of Racket) will tell you the module some-id
is defined in