Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Divisible by 3 or 5, in lisp

Name: Anonymous 2007-08-12 17:20 ID:RyfBwU/E

(lambda (n) (or (= 0 (mod n 3)) (= 0 (mod n 5))))

how on earth do you write this in lisp, I mean better than I have here.. surely it must be possible?

Rube Goldberg devices are definitely acceptable if it makes the final code better.. hope you have some ideas /prog/

Name: Anonymous 2007-08-12 17:41 ID:Mvf+qhCr

(lambda (n)
  (let ((divisible (lambda (k) (= 0 (mod n k)))))
    (or (divisible 3) (divisible 5))))

Name: Anonymous 2007-08-12 17:44 ID:Mvf+qhCr

(define (divisible n k) (= 0 (mod n k)))
(define (divisible-by-list n l) (fold (lambda (k o) (or o (divisible n k))) #f l))

Name: Anonymous 2007-08-12 17:47 ID:Mvf+qhCr

(define (divisible n l)
 (if (pair? l) #f
     (if (= (mod n (car l)) 0) #t
         (divisible n (cdr l)))))

(divisible n '(3 5))

Name: Anonymous 2007-08-12 18:05 ID:RyfBwU/E

>>3
ok this is neat
I think I want to generalize though

divisible = (lambda (n d) (= 0 (mod n d))
f-by-list = (lambda (n l) (fold (lambda (k o) (or o (f n k))) #f 1))
divisble-by-list = (lambda (n l) (fold (lambda (k o) (or o ((lambda (n d) (= 0 (mod n d)) n k))) #f 1))

((lambda (n l) (fold (lambda (k o) (or o ((lambda (n d) (= 0 (mod n d)) n k))) #f 1)) n '(3 5))

hmm

Name: Anonymous 2007-08-12 18:08 ID:UuB0Fh1x

LOL Lisp.
int f(n){(!(n%3) || !(n%5))}

Name: Anonymous 2007-08-12 18:19 ID:+1As6lo3

LOL De Morgan.
int f(n){!(n%3 && n%5)}

Name: Anonymous 2007-08-12 18:23 ID:UuB0Fh1x

>>7
LOL some sort of newfag college undergrad who just finished discrete math. My version is clearly more readable, hence it is superior to yours in every way possible.

Name: Anonymous 2007-08-12 18:38 ID:+1As6lo3

LOL hurt pride

Really, just use haskell for euler, you'll finish the first 50 or so in no time.

Name: Anonymous 2007-08-12 18:48 ID:ZL/ORnBu

>>6,7,8
you both forgot return. fail.

Name: Anonymous 2007-08-12 21:52 ID:RyfBwU/E

>>7
nice, well done

Name: Anonymous 2007-08-12 21:52 ID:RyfBwU/E

>>10
C faggot

Name: Anonymous 2007-08-12 22:04 ID:RyfBwU/E

(not (/= 0 (mod n 3) (mod n 5)))

Name: Anonymous 2007-08-12 22:04 ID:Heaven

>>13
FAIL

Name: Anonymous 2007-08-12 23:42 ID:dvYoKBJB

>>6
no shit dude. what you're doing is essentially
(define (f n) (or (not (divisible n 3)) (not (divisible n 5))))

but you use builtin syntax, that's why it looks shorter.

(spoiler: builtin syntax for that stuff is only useful in the rare cases you need to write silly code examples for a prog forum)

Name: Anonymous 2007-08-12 23:53 ID:RyfBwU/E

MOAN COMPLAIN HOW DO I DO IT BETTER IN LISP THO?

Name: Anonymous 2007-08-13 0:10 ID:WEd4ro21

Name: Anonymous 2007-08-13 0:18 ID:WEd4ro21

>>16
>>4
or something like
(defun f (n l) (find-if (p/ divisible n) l)
if you use the ugly p/ macro I made.
so you call it like this:

(f 1 '(3 5))
or
(f n '(3 5 7 11))
or any list you want. the exact example you asked for:

(lambda (n) (find-if (p/ divisible n) '(3 5)))
without the macro, in plain lisp
(lambda (n) (find-if (lambda (k) (divisible n k)) '(3 5)))

Name: Anonymous 2007-08-13 0:45 ID:3J35KuIg

>>18
WTF.. find-if wont work

Name: Anonymous 2007-08-13 1:04 ID:3J35KuIg

>>18
did you mean every?

Name: Anonymous 2007-08-13 2:55 ID:Heaven

(defun % (d)
 (
lambda (n) (= 0 (mod n d))))


(defun all (&rest predicates)
 (
lambda (x)
  (
every (function identity)
   (
mapcar (function (lambda (p) (funcall p x))) predicates))))


(all (% 3) (% 5))

Name: Anonymous 2007-08-13 3:02 ID:3J35KuIg

>>21

oh wow thanks!


(defun any (&rest predicates)
 (lambda (x)
  (some (function identity)
   (mapcar (function (lambda (p) (funcall p x))) predicates))))


did that so (any (% 3) (% 5)) makes just the function I needed,

can anyone beat that? I don't think so :p

Name: Anonymous 2007-08-13 3:12 ID:UK+mlIcu

>>15
This is what I love about you smug LISP weenies. Always making excuses for your failure of a language. Go count some parenthesis, I'm done talking with your stupid ass.

Name: Anonymous 2007-08-13 3:38 ID:Heaven

Good, now make it print "fizz" and "buzz".

Name: Anonymous 2007-08-13 3:53 ID:n8mj67Rh

yeah i dont know lisp or anything but this thread is hilarious i mean what the fuck is >>21 even supposed to mean and how does it relate to finding if numbers are evenly divisible by 3 or 5

Name: Anonymous 2007-08-13 5:45 ID:Heaven

>>25
Wow. Get the fuck out.

Name: Anonymous 2007-08-13 6:24 ID:WEd4ro21

there are some functions explained in the book On Lisp exactly like the "all" and "any" you proposed. They're called fun (function union) and fint (function intersection).

Name: Anonymous 2007-08-13 7:30 ID:3J35KuIg

>>24
lmao, that wasn't actually the program but hehe

>>25
of course you don't understand it if you don't know lisp... duh..

>>27
I, The OP, will read On Lisp then, thanks.

Name: Anonymous 2007-08-13 8:36 ID:Heaven

>>23
I can actually implement that fail syntax your language has in lisp and use it in lisp.

Name: Anonymous 2007-08-13 8:54 ID:9p+T4lNQ

Wow this shit is like perl

Many ways to do it

Name: Anonymous 2007-08-13 9:21 ID:h0iRTlfF

In Lisp how do i divide by 0 ?

Name: Anonymous 2007-08-13 9:55 ID:1e5AoIKR

>>31
(div 1 ())

Name: Anonymous 2007-08-13 11:26 ID:3J35KuIg

arithmetic error DIVISION-BY-ZERO signalled
Operation was SB-KERNEL::DIVISION, operands (1 0).
   [Condition of type DIVISION-BY-ZERO]

Restarts:
 0: [ABORT] Return to SLIME's top level.
 1: [ABORT] Exit debugger, returning to top level.

Backtrace:
  0: (SB-KERNEL::INTEGER-/-INTEGER 1 0)
  1: (/ 0)
  2: (SB-INT:SIMPLE-EVAL-IN-LEXENV (/ 0) #<NULL-LEXENV>)
  3: (SWANK::EVAL-REGION
      "(/ 0)
     "
      T)
  4: ((LAMBDA ()))
  5: ((LAMBDA (SWANK-BACKEND::FN)) #<CLOSURE (LAMBDA #) {124BC14D}>)

Name: Anonymous 2007-08-13 21:51 ID:3J35KuIg

does anyone else have the answer?

Name: Anonymous 2007-08-13 22:06 ID:Mn9xg8th

Isn't >>1's function pretty much the simplest, most clear expression possible, given the requirements? Could use a bit of indenting though, but.

Name: Anonymous 2007-08-13 22:57 ID:hsgFZZc9

(\n->any ((==0).mod n) [3,5])

(\n->n `mod` 3 == 0 || n `mod` 5 == 0)

cleverly disguised with paranthesis

Name: Anonymous 2007-08-13 23:38 ID:3J35KuIg

>>36
that reminds me, haskell is based on C

Name: Anonymous 2007-08-13 23:56 ID:DCTLBuXh

>>1

(lambda x x (lambda x x) (car (lambda x (cdr (lambda x) x ) x)))))))))))))))))))))))))))))))))))

Name: Anonymous 2007-08-14 5:39 ID:M+StMIHs

>>38
lololol

Name: Anonymous 2007-08-14 7:00 ID:iqrAANyA

divisibleBy3or5 x = divisibleBy x 3 || divisibleBy x 5
    where
    divisibleBy = (== 0) `dot` mod   
    dot = (.).(.)

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List