Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 3 Next »

LISP 5 Basic Functions

 SchemeCommon LispClojure

1. car

carcar, firstfirst
2. cdrcdrcdr, restrest
3. consconsconscons
4. atom atom 
5. eq

eq?

eq=

LISP Special Forms (Syntax)

 schemeCommon LispClojure
cond   
lambda   
define   

Other Basics

 schemeCommon LispClojure
(= 0 0)#tTtrue
(= 0 1)#fNILfalse
 HaskellschemeCommon LispClojure
anonymous function(\x y -> x + y) 3 4
-> 7
((lambda (x y) (+ x y) 3 4)
-> 7 
 

((fn [x y] (+ x y)) 3 4)
-> 7

(#(+ % %2) 3 4)
-> 7 

binding an anonymous function to a variableadd = (\x y -> x + y)
add 3 4
-> 7 
(define add (lambda (x y) (+ x y))
(add 3 4)
-> 7 
 (def add (fn [x y] (+ x y))
(add 3 4)
-> 7 
named function   (defn add [x y] (+ x y))
(add 3 4)
-> 7 
 HaskellschemeCommon LispClojure
headhead [3,1,4,1,5,9,2]
-> 3 
(car '(3 1 4 1 5 9 2))
-> 3 
(car '(3 1 4 1 5 9 2))
-> 3
(first '(3 1 4 1 5 9 2))
-> 3
tailtail [3,1,4,1,5,9,2]
-> [1,4,1,5,9,2]
(cdr '(3 1 4 1 5 9 2))
-> (1 4 1 5 9 2)
(cdr '(3 1 4 1 5 9 2))
-> (1 4 1 5 9 2)
(rest '(3 1 4 1 5 9 2))
-> (1 4 1 5 9 2)
cons3:[1,4,1,5,9,2]
-> [3,1,4,1,5,9,2] 
(cons 3 '(1 4 1 5 9 2))
-> (3 1 4 1 5 9 2) 
(cons 3 '(1 4 1 5 9 2))
-> (3 1 4 1 5 9 2) 
(cons 3 '(1 4 1 5 9 2))
-> (3 1 4 1 5 9 2) 

take

take 5 [3,1,4,1,5,9,2]
-> [3,1,4,1,5]
  (take 5 '(3 1 4 1 5 9 2))
-> (3 1 4 1 5) 
take-whiletakeWhile odd [3,1,4,1,5,9,2]
-> [3,1]
  (take-while odd? '(3 1 4 1 5 9 2))
-> (3 1)
drop-whiledropWhile odd [3,1,4,1,5,9,2]
-> [4,1,5,9,2]
  (drop-while odd? '(3 1 4 1 5 9 2))
-> (4 1 5 9 2)
filterfilter odd 5 [3,1,4,1,5,9,2]
-> [3,1,1,5,9]
  (filter odd? '(3 1 4 1 5 9 2))
-> (3 1 1 5 9)
 HaskellschemeCommon LispClojure
0, 1, 2, ... infinite[0..]
-> [0,1,2,...infinite]
  (range)
-> (0 1 2 3 ... infinite) 
0, 1, 2, 3, 4[0..4]
-> [0,1,2,3,4]
(use srfi-1)
(iota 5)
-> (0 1 2 3 4) 
 

(range 5)
-> (0 1 2 3 4) 

2, 3, 4, 5, 6[2..6]
-> [2,3,4,5,6]
(use srfi-1)
(iota  5 2)
-> (2 3 4 5 6) 
 

(range 2 7)
->  (2 3 4 5 6)

3, 5, 7, 9, 11[3,5..11]
-> [3,5,7,9,11] 
(use srfi-1)
(iota 5 3 2)
-> (3 5 7 9 11) 
 (range 3 12 2)
-> (3 5 7 9 11) 
f (3, 1, 4, 1, 5)    (apply + '(3 1 4 1 5))
-> 14
f(5, f(1, f(4, f(1, f (3, i)))))

foldr (+) 0 [3,1,4,1,5]
-> 14 

x2y x y = x^2 + y
foldr x2y 0 [3,1,4,1,5]
-> 52 

   
f(f(f(f(f(i, 3), 1), 4), 1), 5)x2y x y = x^2 + y
foldl x2y 0 [3,1,4,1,5]
-> 117007494 
  (defn x2y [x y] (+ (* x x) y))
(reduce x2y 0 '(3 1 4 1 5))
-> 117007494 
i, f(i), f(f(i)), f(f(f(i))), ...take 5 $ iterate (2 *) 1
-> [1,2,4,8,16]
  (take 5 (iterate (partial * 2) 1))
-> (1 2 4 8 16) 
show contents of a file   (print (slurp "hoge.clj"))
 ghcigaucheclispclojure
load:l hoge.hs(load "./hoge.scm") (load-file "hoge.clj")
args    

 

 

 

 

  • No labels