Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

This article is a Private article. Only a writer and users who know the URL can access it.
Please change open range to public in publish setting if you want to share this article with other users.

若干lispの勉強をしました

Posted at

この記事は SLP-KBIT AdventCalendar2024 4日目の記事です。

経緯

C、C++、Perl、Fortran、Java、COBOL、APLが引き合いに出され、神はこれらの言語を全て知っているが、その上でLispを選んだ、Lispで世界の全てを作った。なぜなら神には締め切りがあって6日間で世界を作らなければならなかったから、というのが要旨だ。

かっこいい...

環境

CLISP 2.49

lispとは

1960年に発表された関数型プログラミング言語。
現在でも利用されているプログラミング言語の中でもかなり古い。
プログラムに()を多用する特徴がある。

出力

[1]> (write-line "Hello world!")
Hello world!
"Hello world!"
[1]> (format t "Hello world!~%")
Hello world!
NIL

四則演算

[1]> (+ 1 1)
2
[2]> (- 2 5)
-3
[3]> (* 0 10)
0
[4]> (/ 6 3)
2
[5]> (/ 10 3) 
10/3

if文

[1]> (if (> 2 4)
  (format t "2 > 4")
  (format t "4 > 2"))
4 > 2
NIL

if文の内容が偽であったため、二行目の(format t "4 > 2")が実行されました。

for文

[1]> (dotimes (x 20)
    (write x))
012345678910111213141516171819

FizzBuzz

[1]> (dotimes (x 20)
    (let (n)
    (setq n (+ x 1))
    (if (and (= 0 (mod n 3)) (= 0 (mod n 5)))
        (write "Fizz Buzz"))
    (if (and (= 0 (mod n 3)) (not (= 0 (mod n 5))))
        (write "Fizz"))
    (if (and (not (= 0 (mod n 3))) (= 0 (mod n 5)))
        (write "Buzz"))
    (if (and (not (= 0 (mod n 3))) (not (= 0 (mod n 5))))
        (write n))
    (write ",")))
1","2",""Fizz"","4",""Buzz"",""Fizz"","7","8",""Fizz"",""Buzz"","11",""Fizz"","13","14",""Fizz Buzz"","16","17",""Fizz"","19",""Buzz"","
NIL

let...変数宣言
setq...代入
mod...余剰
多分もっときれいに書ける。

最後に

まだ全然学習できていないので、間違ってる点があったらすみません。

良ければ他の記事も見てみてください。

参考資料

0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?