LoginSignup
1
1

More than 5 years have passed since last update.

Shenの基礎その1 基本的な型

Posted at

Shenについて書きます。

たぶん何回か記事を分けて書きます。

Shenとは何か?

Shenという、Lisp方言があります。

Shenは強力な型システムや、関数型プログラミング言語をサポートした言語で、ポータブルに設計された言語でつ。

先行記事(日本語)

すでにQiitaなどにで、先行に日本語記事があるので紹介します。Shen 言語で日本語検索で調べると、2件しか出てこないのですが。

Shenの基礎

基本的な型について

シンボル

Shenはquote(')は暗黙的です。たとえば,nobkzと入力すると、暗黙的に、nobkzというシンボルとして、暗黙的にquoteされます。

>> nobkz
nobkz

replに、(tc +)と、入力すれば、型が表示されます。nobkzはシンボル型ですね。

>> (tc +)
true
>> nobkz
nobkz : simbol

数値型

Shenでは、数字を入力すれば、number型です。

>> 1000
1000 : number
>> -0.5
-0.5 : number

四則演算

>> (+ 1 2 3)
6 : number
>> (- 1 2)
-1 : number
>> (* 1 2)
2 : number
>> (/ 10 3)
3.3333333333333335 : number

文字列

“”で括ると、文字列になります。

>> “sample"
"sample" : string

@sで連結したり、いろいろあります。

>> (@s "hello" " " "world")
"hello world" : string
>> (pos "hello" 3)
"l" : string

boolean

trueと書くとシンボルにならずに、booleanです。

>>> true
ture : boolean
>>> false 
false : boolean
>>> (and true false)
false : boolean
>>> (or false true)
true : boolean
>>> (not false)
true : boolean

リスト

リストは[]で括ります。

>> [1 2 (+ 1 2)]
[1 2 3] : (list number)

()は空リストです。

>> ()
[] : (list A)

consもあります。(cons 1 2)はtypeを表示するままだと、type errorを起します。

>>> (cons 1 ())
(1) : (list number)
>>> (cons 1 2)
type error 
>>> (tc -)
false : boolean
>>> (cons 1 2)
[1 | 2] 

また、[]の記法において|も,consと同じです。

>>> [2 | [3 4]]
[2 3 4]

head,tailで、carとcdrと同様な関数です。

>>> head
head : (list A) --> A
>>> (head [1 2 3])
1 : number
>>> tail
tail : (list A) --> (list A)
>>> (tail [1 2 3])
[2 3] : (list number)

Vectors

Vectors型があります。

>> <>
<> : (vector A)
>> (@v a b c <>)
<a b c> : (vector symbol)
>> (limit (@v a b c <>))
3 : number
>> (<-vector (@v a b c d e) 3)
c : symbol
>> (vector-> (@v a b c d e) 3 a)
<a b a d e) : (vector symbol)

Tuples

タプルがあります。

>>> (@p (+ 1 1) a)
(@p 2 a) : (number * symbol)
>> (fst (@p 1 a))
1 : number 
>> (snd (@p 1 b))
b : symbol

lazy

遅延評価の型もあります。

>> (lazy 1)
#<FUNCTION (LAMBDA ()) {1004B8E7CB}> : (lazy number)

関数の型

関数の型は次のような表記になります。

>> (/. X X)
#<FUNCTION (LAMBDA (X)) {10051094CB}> : (A --> A)

とりあえず、基本的な型を記述しました。次回また、空いている日に構文やら、KLambdaの話とかしまつ。

1
1
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
1
1