Lisp

53 readers
3 users here now

founded 1 year ago
MODERATORS
26
27
28
1
Quicksort in lisp (emanuelpeg.blogspot.com)
submitted 1 year ago by emanuelpeg@alien.top to c/lisp
29
 
 

Problem: On this page, study what Roman numerals are, and then write a function arabic->roman that takes a natural number n in the range 0 < n < 4000 as its input and converts it to a Roman numeral (represented as a string). Additionally, write the reverse function, roman->arabic, which takes a Roman numeral (represented as a string) as its input and converts it to a natural number.

Solution:

#lang racket

(define UNITS '(I II III IV V VI VII VIII IX))
(define TENS '(X XX XXX XL L LX LXX LXXX XC))
(define HUNDREDS '(C CC CCC CD D DC DCC DCCC CM))
(define THOUSANDS '(M MM MMM))
(define VALUES '((I 1) (V 5) (X 10) (L 50) (C 100) (D 500) (M 1000)))

(define (arabic->roman n)
  (define positions (list UNITS TENS HUNDREDS THOUSANDS))
  (define (ar-helper n pos acc)
    (if (= n 0)
        acc 
        (ar-helper (quotient n 10)
                   (+ pos 1)
                   (let ([r (remainder n 10)])
                     (if (zero? r)
                         acc
                         (string-append (symbol->string
                                         (list-ref (list-ref positions pos)
                                                   (- r 1)))
                                        acc))))))       
  (if (<= 1 n 3999)
      (ar-helper n 0 "")
      (error "Error: number out of range!")))


(define (roman->arabic r)
  (define len (string-length r))
  (define (get-value r idx)
    (second (assq (string->symbol (string (string-ref r idx))) VALUES)))
  (define (ra-helper r)
    (let loop ([i 0] [acc 0])
      (if (>= i len)
          acc
          (if (< (+ i 1) len)
              (let ([a (get-value r i)]
                    [b (get-value r (+ i 1))])
                (if (< a b)
                    (loop (+ i 2) (+ acc (- b a)))
                    (loop (+ i 1) (+ acc a))))
              (loop (+ i 1) (+ acc (get-value r i)))))))
  (ra-helper (string-upcase r)))

Now we can test our functions:

> (arabic->roman 39)
"XXXIX"
> (arabic->roman 246)
"CCXLVI"
> (arabic->roman 789)
"DCCLXXXIX"
> (arabic->roman 2421)
"MMCDXXI"
> (arabic->roman 160)
"CLX"
> (arabic->roman 207)
"CCVII"
> (arabic->roman 1009)
"MIX"
> (arabic->roman 1066)
"MLXVI"
> (arabic->roman 3999)
"MMMCMXCIX"
> (arabic->roman 1776)
"MDCCLXXVI"
> (arabic->roman 1918)
"MCMXVIII"
> (arabic->roman 1944)
"MCMXLIV"
> (arabic->roman 2023)
"MMXXIII"
> (roman->arabic "XXXIX")
39
> (roman->arabic "CCXLVI")
246
> (roman->arabic "DCCLXXXIX")
789
> (roman->arabic "MMCDXXI")
2421
> (roman->arabic "CLX")
160
> (roman->arabic "CCVII")
207
> (roman->arabic "MIX")
1009
> (roman->arabic "MLXVI")
1066
> (roman->arabic "MMMCMXCIX")
3999
> (roman->arabic "MDCCLXXVI")
1776
> (roman->arabic "MCMXVIII")
1918
> (roman->arabic "MMXXIII")
2023

30
31
 
 

In the eval function of chapter 4 we have different eval-foo...s, which deal with the special syntax. Why the apply function is not just part of eval function. So we could have some dispatch condition in case analysis such as ...((application? exp) (eval-application exp env))... . Why do we make it harder?

32
33
 
 

These options are sorted with type inference and natural order.

Though it has so many problems, but, you now can enable this problem in scheme-langserver(https://github.com/ufo5260987423/scheme-langserver)!

It now mainly used the r6rs procedures' information, and I annotated them with my homemade DSL. And some trick from gradual typing is also used!

Ok, I don't have a degree on computer science, but this is all what I can do.

34
 
 

Hello,

I'm working on a game in Rust but interested in trying out (SB)CL. That being said I'm worried about GC pauses for the type of game I'm making and would like to create a relatively simple benchmark to gauge the level of inconvenience this would cause me. I'd like to do this upfront since I already have a decent bit of progress on the Rust side. Originally the game was being developed in Godot but it ended up being a little too CPU heavy for GDScript (not GC related as far as I could tell, but they have reference counting over there). Then I migrated to Rust and performance-wise everything is working great, but I've been wanting to try CL for a while now and I can afford a couple of weeks of distraction...

How can I benchmark the GC? I know something I can try is simply calling it at the end of every frame and measuring the time there, but I'm curious about how to time it in its default settings.

I also saw: https://groups.google.com/g/sbcl-devel/c/VAA4SkQo3jM But as someone unfamiliar with SBCL development, is this GC something that we can expect to see within a year or two? I'm eager to try it, seeing as the author of that post stated:

The longest a thread should ever be "paused" (not doing its own work) is about 100 microseconds. "paused" actually means doing work for the GC.

Which sounds fantastic, not even 1ms at its highest.

35
 
 

Hello,

I know this question appears now and then on this group but, because the answer might change and I did not find a satisfactory answer, I repost.

I'm looking for the most popular scheme or a lisp with good libraries for the following task:

Load CSV into database and display the result on a webpage.

Ideally, I would like to be able to use scheme/lisp to write html and js also.

A plus would be to support also greenthreads with actor model. I really like LFE (Erlang) but it does not have a lot of libraries for general purpose programming.

The only ones I exclude is Clojure and Racket. Clojure because I would like to stay away from the JVM and the OOP paradigm that spills on clojure by Java libraries. I still consider it as an option because I know it is probably the most used and modern LISP for now. Racket because, although it seems to have a lot of features, I feel there is a lot of variations because of the language declaration and I feel the packages are too heterogenous. Sometimes also, the libraries are overly complicated. From my point of view...

Subquestion here : what is the most maintained scheme implementation with the most packages ? I know there is Chez, Chicken, Guile, Gambit, LIPS (js). Are they all still maintained with a good active community ?

Thanks

36
37
 
 

hello everyone, I'm a python programmer with around 6 years of experience.

recently i learned haskell and also learned a little about category theory

i also want to learn LISP, I've seen a few tutorials here and there and I'm aware of the syntax. i would like to read a book on LISP.

could you guys recommend me books, anything from intermediate to advance

38
 
 

As the title says, I am looking for a library that can connect to Progres and execute a stored procedure with either input or output parameters. Or even a user defined function.

I've checked the documentation of these three libraries:

  • cl-progres
  • pg-dot-lisp
  • clsql

but there seems to be no mention or example of executing a stored procedure. Is it just not documented? Are there other ways I can do it apart from these three?

39
 
 

I'd like to learn Scheme by doing programming tasks, because reading exposition makes my ADD glazeth over between the 100 self-evident things and the 1 new thing hiding among them.

I think it would be better if I had problems thrown at me, and in the process of solving them I would wonder how to accomplish this or that, and slowly discover Scheme in the process.

Googling doesn't really give me what I had in mind, maybe I'm not searching for the rights terms, but anyway I thought it wouldn't hurt asking too. Thanks in advance for your thoughts.

40
 
 

I have a minimal amount of experience with Lisp and have read the blog posts on how to implement the basic special forms which are needed to define a Lisp. I'm considering trying to write my own Lisp as a scripting language for a platform whose primary language is something that I don't like. Thing is, I don't really want to write code using a minimal set of primitives. I want to write in a full Lisp.

Are there any "bring your own bootstrap" Lisp implementations, which provide a more full-featured language once the basic primitives have been provided? Something that would let me go more quickly from a couple hundred lines of bootstrap code into a more full fledged language, even if the runtime is slow.

41
 
 

I was playing with code generation outside macro being inspired by corrected version of nif

https://letoverlambda.com/index.cl/guest/chap3.html#sec_5

When I replace the usual gensym with my interned-gensym I can skip the eval and have more noob friendly version of the code that I can easily copy to REPL for further experiments with macro code creation. 

When it's done, will I be able to replace with defmacro?

Why the usual gensym in macro is not interned?

Besides the eval is evil, what are other pitfalls of writing code this way?

(defun interned-gensym ()  
  "More suitable variant of gensym for macro experiments"  
  ;; file:~/Programming/sbcl/src/code/symbol.lisp::593  
  (values (intern (format nil "Z~A"
  (let ((old *gensym-counter*))
    (setq *gensym-counter* (1+ old))
    old)))))

;;; nif

(eval (apply (lambda (expr pos zero neg)
  (let ((gexpr (interned-gensym)))
    `(let ((,gexpr ,expr))
    (cond 
      ((plusp ,gexpr) ,pos)
      ((zerop ,gexpr) ,zero)
      (T ,neg)))))
    ;; args for the lambda
    ((- 5 2) :positive :zero :negative)))

42
 
 

Hi i’m looking for a scheme dialect, that can targhet bsds, linux, mac and windows platform with native compilation, there are some?

43
44
 
 

For example, suppose that I have a list/vector of integers named dt, and I want to remove elements with the value 70 there. In Common Lisp it would be:

 (setf dt (remove 70 dt))

My question is, which scenario is actually happening here?

  1. All original elements of dt are erased. After that, all elements of dt are remade entirely from ground up, elements by elements, excluding the removed value that is 70.
  2. The only affected elements are those with the value of 70. Only those elements are modified in any way (in this case removed). All other elements are left untouched at all, except that maybe they ‘change positions’ to fill the place of the removed elements.
  3. The original list/vector with 70s loses any association with the variable dt. Then a new list/vector without 70s is assigned to dt.

45
 
 

Scheme Request for Implementation 250,
"Insertion-ordered hash tables",
by John Cowan,
is now available for discussion.

Its draft and an archive of the ongoing discussion are available at https://srfi.schemers.org/srfi-250/.

You can join the discussion of the draft by filling out the subscription form on that page.

You can contribute a message to the discussion by sending it to srfi-250@srfi.schemers.org.

Here's the abstract:

This SRFI defines an interface to hash tables, which are widely recognized as a fundamental data structure for a wide variety of applications. A hash table is a data structure that:

  • Is disjoint from all other types.
  • Provides a mapping from objects known as keys to corresponding objects known as values.
    • Keys may be any Scheme objects in some kinds of hash tables, but are restricted in other kinds.
    • Values may be any Scheme objects.
  • Provides an equality predicate which defines when a proposed key is the same as an existing key. No table may contain more than one value for a given key.
  • Provides a hash function which maps a candidate key into a non-negative exact integer.
  • Supports mutation as the primary means of setting the contents of a table.
  • Provides key lookup and destructive update in (expected) amortized constant time, provided that a satisfactory hash function is available.
  • Does not guarantee that whole-table operations work in the presence of concurrent mutation of the whole hash table. (Values may be safely mutated.)

Unlike the hash tables of SRFI 125, which is the direct ancestor of this specification, the hash tables described here are ordered by insertion: that is, associations inserted earlier in the history of the hash table appear earlier in the ordering. Advances in the implementations of hash tables, as provided by C++, Python, JavaScript, etc., make the provision of this new facility practical. As a result, the hash tables of this SRFI do not interoperate with the hash tables of SRFI 125, SRFI 126, or existing R6RS implementations.

Regards,

SRFI Editor

46
47
 
 

I believe I will end up with 6-8 libs I can release from commercial web projects. This is quite beneficial for me because its forcing me to decouple the code at the same time.

Anyway, second lib here: https://github.com/vinn2010/Loggie

Stars appreciated if you care about providing me with dopamine.

(this time I added a licence :D)

If anyone missed it, first one here: https://github.com/vinn2010/cl-stackexchange

48
1
Eval-Apply (alien.top)
submitted 1 year ago by OkGroup4261@alien.top to c/lisp
 
 

What is so special about the SICP eval-apply loop? What is so enlightening about it?

49
 
 

When you implement your own programming language, one of the problems is to implement a GUI to play with it. If you compile with WASM, you can solve this issue and a as a bonus you will be able to demonstrate your language across the Internet quite easily.

However, there are some real issues. One is that Web Assembly handle strings as arrays of integers.

If you are interested, I compiled my own language: LispE as a WASM library.

I have written a few blogs about how to do it: https://github.com/naver/lispe/wiki/6.18-Handling-strings-in-WASM-without-burning-yourself

Here is also a guide on how to use this library: https://github.com/naver/lispe/wiki/6.17-A-WebAssembly-version-of-LispE

50
 
 

I used ChatGPT to ingest 10 classic Common Lisp books. The resulting GPT seems even more useful to me than the Hyperspec. In case you're interested:

https://chat.openai.com/g/g-HrUtFVno8-lisp-expert

AI has come a long way baby.

view more: ‹ prev next ›